|
| 1 | +import { getPackageManagerCommand } from '@nx/devkit'; |
| 2 | +import { |
| 3 | + checkFilesExist, |
| 4 | + ensureNxProject, |
| 5 | + listFiles, |
| 6 | + runNxCommandAsync, |
| 7 | + tmpProjPath, |
| 8 | + uniq, |
| 9 | + updateFile, |
| 10 | +} from '@nx/plugin/testing'; |
| 11 | +import { execSync } from 'child_process'; |
| 12 | +import { writeFileSync } from 'fs'; |
| 13 | +import { join } from 'path'; |
| 14 | + |
| 15 | +describe('rspack e2e', () => { |
| 16 | + // Setting up individual workspaces per |
| 17 | + // test can cause e2e runs to take a long time. |
| 18 | + // For this reason, we recommend each suite only |
| 19 | + // consumes 1 workspace. The tests should each operate |
| 20 | + // on a unique project in the workspace, such that they |
| 21 | + // are not dependant on one another. |
| 22 | + beforeAll(() => { |
| 23 | + ensureNxProject('@nx/rspack', 'dist/packages/rspack'); |
| 24 | + }); |
| 25 | + |
| 26 | + afterAll(() => { |
| 27 | + // `nx reset` kills the daemon, and performs |
| 28 | + // some work which can help clean up e2e leftovers |
| 29 | + runNxCommandAsync('reset'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should create rspack root project and additional apps', async () => { |
| 33 | + const project = uniq('myapp'); |
| 34 | + await runNxCommandAsync( |
| 35 | + `generate @nx/rspack:preset ${project} --framework=react --unitTestRunner=jest --e2eTestRunner=cypress` |
| 36 | + ); |
| 37 | + |
| 38 | + // Added this so that the nx-ecosystem-ci tests don't throw jest error |
| 39 | + writeFileSync( |
| 40 | + join(tmpProjPath(), '.babelrc'), |
| 41 | + ` |
| 42 | + { |
| 43 | + "presets": [ |
| 44 | + "@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript", |
| 45 | + [ |
| 46 | + "@nx/react/babel", |
| 47 | + { |
| 48 | + "runtime": "automatic" |
| 49 | + } |
| 50 | + ] |
| 51 | + ], |
| 52 | + "plugins": ["@babel/plugin-transform-runtime"] |
| 53 | + } |
| 54 | + ` |
| 55 | + ); |
| 56 | + |
| 57 | + const pm = getPackageManagerCommand(); |
| 58 | + execSync( |
| 59 | + pm.addDev + |
| 60 | + ' @babel/preset-react @babel/preset-env @babel/preset-typescript', |
| 61 | + { cwd: tmpProjPath() } |
| 62 | + ); |
| 63 | + |
| 64 | + let result = await runNxCommandAsync(`build ${project}`, { |
| 65 | + env: { NODE_ENV: 'production' }, |
| 66 | + }); |
| 67 | + expect(result.stdout).toContain('Successfully ran target build'); |
| 68 | + // Make sure expected files are present. |
| 69 | + expect(listFiles(`dist/${project}`)).toHaveLength(5); |
| 70 | + |
| 71 | + result = await runNxCommandAsync(`test ${project}`); |
| 72 | + expect(result.stdout).toContain('Successfully ran target test'); |
| 73 | + |
| 74 | + // TODO(Colum): re-enable when cypress issue is resolved |
| 75 | + // result = await runNxCommandAsync(`e2e e2e`); |
| 76 | + // expect(result.stdout).toContain('Successfully ran target e2e'); |
| 77 | + |
| 78 | + // Update app and make sure previous dist files are not present. |
| 79 | + updateFile(`src/app/app.tsx`, (content) => { |
| 80 | + return `${content}\nconsole.log('hello'); |
| 81 | + `; |
| 82 | + }); |
| 83 | + result = await runNxCommandAsync(`build ${project}`, { |
| 84 | + env: { NODE_ENV: 'production' }, |
| 85 | + }); |
| 86 | + expect(result.stdout).toContain('Successfully ran target build'); |
| 87 | + expect(listFiles(`dist/${project}`)).toHaveLength(5); // same length as before |
| 88 | + |
| 89 | + // Generate a new app and check that the files are correct |
| 90 | + const app2 = uniq('app2'); |
| 91 | + await runNxCommandAsync( |
| 92 | + `generate @nx/rspack:app ${app2} --framework=react --unitTestRunner=jest --e2eTestRunner=cypress --style=css` |
| 93 | + ); |
| 94 | + checkFilesExist(`${app2}/project.json`, `${app2}-e2e/project.json`); |
| 95 | + |
| 96 | + // Added this so that the nx-ecosystem-ci tests don't throw jest error |
| 97 | + writeFileSync( |
| 98 | + join(tmpProjPath(), app2, '.babelrc'), |
| 99 | + ` |
| 100 | + { |
| 101 | + "presets": [ |
| 102 | + "@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript", |
| 103 | + [ |
| 104 | + "@nx/react/babel", |
| 105 | + { |
| 106 | + "runtime": "automatic" |
| 107 | + } |
| 108 | + ] |
| 109 | + ], |
| 110 | + "plugins": ["@babel/plugin-transform-runtime"] |
| 111 | + } |
| 112 | + ` |
| 113 | + ); |
| 114 | + |
| 115 | + result = await runNxCommandAsync(`build ${app2}`, { |
| 116 | + env: { NODE_ENV: 'production' }, |
| 117 | + }); |
| 118 | + expect(result.stdout).toContain('Successfully ran target build'); |
| 119 | + // Make sure expected files are present. |
| 120 | + expect(listFiles(`dist/${app2}`)).toHaveLength(5); |
| 121 | + |
| 122 | + result = await runNxCommandAsync(`test ${app2}`); |
| 123 | + expect(result.stdout).toContain('Successfully ran target test'); |
| 124 | + |
| 125 | + // TODO(Colum): re-enable when cypress issue is resolved |
| 126 | + // result = await runNxCommandAsync(`e2e ${app2}-e2e`); |
| 127 | + // expect(result.stdout).toContain('Successfully ran target e2e'); |
| 128 | + |
| 129 | + // Generate a Nest app and verify build output |
| 130 | + const app3 = uniq('app3'); |
| 131 | + await runNxCommandAsync( |
| 132 | + `generate @nx/rspack:app ${app3} --framework=nest --unitTestRunner=jest --no-interactive` |
| 133 | + ); |
| 134 | + checkFilesExist(`${app3}/project.json`); |
| 135 | + |
| 136 | + result = await runNxCommandAsync(`build ${app3}`); |
| 137 | + expect(result.stdout).toContain('Successfully ran target build'); |
| 138 | + // Make sure expected files are present. |
| 139 | + expect(listFiles(`dist/${app3}`)).toHaveLength(2); |
| 140 | + |
| 141 | + result = await runNxCommandAsync( |
| 142 | + `build ${app3} --generatePackageJson=true` |
| 143 | + ); |
| 144 | + expect(result.stdout).toContain('Successfully ran target build'); |
| 145 | + // Make sure expected files are present. |
| 146 | + expect(listFiles(`dist/${app3}`)).toHaveLength(4); |
| 147 | + }, 200_000); |
| 148 | +}); |
0 commit comments