|
| 1 | +import { |
| 2 | + checkFilesExist, |
| 3 | + ensureNxProject, |
| 4 | + readFile, |
| 5 | + renameFile, |
| 6 | + runNxCommandAsync, |
| 7 | + uniq, |
| 8 | + updateFile, |
| 9 | +} from '@nrwl/nx-plugin/testing'; |
| 10 | + |
| 11 | +import { |
| 12 | + runCommandUntil, |
| 13 | + promisifiedTreeKill, |
| 14 | + killPort, |
| 15 | + removeFile, |
| 16 | +} from '@qwikifiers/e2e/utils'; |
| 17 | + |
| 18 | +describe('qwikNxVite plugin e2e', () => { |
| 19 | + // Setting up individual workspaces per |
| 20 | + // test can cause e2e runs to take a long time. |
| 21 | + // For this reason, we recommend each suite only |
| 22 | + // consumes 1 workspace. The tests should each operate |
| 23 | + // on a unique project in the workspace, such that they |
| 24 | + // are not dependant on one another. |
| 25 | + beforeAll(() => { |
| 26 | + ensureNxProject('qwik-nx', 'dist/packages/qwik-nx'); |
| 27 | + }); |
| 28 | + |
| 29 | + afterAll(async () => { |
| 30 | + // `nx reset` kills the daemon, and performs |
| 31 | + // some work which can help clean up e2e leftovers |
| 32 | + await runNxCommandAsync('reset'); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('should be able to import components from libraries', () => { |
| 36 | + let project: string; |
| 37 | + let libName: string; |
| 38 | + beforeAll(async () => { |
| 39 | + project = uniq('qwik-nx'); |
| 40 | + libName = uniq('qwik-nx'); |
| 41 | + await runNxCommandAsync( |
| 42 | + `generate qwik-nx:app ${project} --no-interactive` |
| 43 | + ); |
| 44 | + await runNxCommandAsync( |
| 45 | + `generate qwik-nx:library ${libName} --unitTestRunner=none --no-interactive` |
| 46 | + ); |
| 47 | + |
| 48 | + // move header component into the library |
| 49 | + |
| 50 | + // update import in layout.tsx |
| 51 | + const layoutFilePath = `apps/${project}/src/routes/layout.tsx`; |
| 52 | + let layoutFile = readFile(layoutFilePath); |
| 53 | + layoutFile = layoutFile.replace( |
| 54 | + `import Header from '../components/header/header';`, |
| 55 | + `import { Header } from '@proj/${libName}';` |
| 56 | + ); |
| 57 | + updateFile(layoutFilePath, layoutFile); |
| 58 | + |
| 59 | + // move header component files |
| 60 | + const headerFolderOldPath = `apps/${project}/src/components/header`; |
| 61 | + const headerFolderNewPath = `libs/${libName}/src/lib`; |
| 62 | + removeFile(`${headerFolderNewPath}/${libName}.tsx`); |
| 63 | + removeFile(`${headerFolderNewPath}/${libName}.css`); |
| 64 | + renameFile( |
| 65 | + `${headerFolderOldPath}/header.tsx`, |
| 66 | + `${headerFolderNewPath}/header.tsx` |
| 67 | + ); |
| 68 | + renameFile( |
| 69 | + `${headerFolderOldPath}/header.css`, |
| 70 | + `${headerFolderNewPath}/header.css` |
| 71 | + ); |
| 72 | + updateFile( |
| 73 | + `libs/${libName}/src/index.ts`, |
| 74 | + `export * from './lib/header';` |
| 75 | + ); |
| 76 | + |
| 77 | + // remove icon from header.tsx |
| 78 | + let headerTsx = readFile(`${headerFolderNewPath}/header.tsx`); |
| 79 | + headerTsx = headerTsx.replace( |
| 80 | + `import { QwikLogo } from '../icons/qwik';`, |
| 81 | + '' |
| 82 | + ); |
| 83 | + headerTsx = headerTsx.replace(`<QwikLogo />`, ''); |
| 84 | + headerTsx = headerTsx.replace( |
| 85 | + 'export default component$(() => {', |
| 86 | + 'export const Header = component$(() => {' |
| 87 | + ); |
| 88 | + updateFile(`${headerFolderNewPath}/header.tsx`, headerTsx); |
| 89 | + }, 200000); |
| 90 | + |
| 91 | + it('should be able to successfully build the application', async () => { |
| 92 | + const result = await runNxCommandAsync(`build-ssr ${project}`); |
| 93 | + expect(result.stdout).toContain( |
| 94 | + `Successfully ran target build-ssr for project ${project}` |
| 95 | + ); |
| 96 | + expect(() => |
| 97 | + checkFilesExist(`dist/apps/${project}/client/q-manifest.json`) |
| 98 | + ).not.toThrow(); |
| 99 | + expect(() => |
| 100 | + checkFilesExist(`dist/apps/${project}/server/entry.preview.mjs`) |
| 101 | + ).not.toThrow(); |
| 102 | + }, 200000); |
| 103 | + |
| 104 | + it('should serve application in preview mode with custom port', async () => { |
| 105 | + const port = 4212; |
| 106 | + const p = await runCommandUntil( |
| 107 | + `run ${project}:preview --port=${port}`, |
| 108 | + (output) => { |
| 109 | + return output.includes('Local:') && output.includes(`:${port}`); |
| 110 | + } |
| 111 | + ); |
| 112 | + try { |
| 113 | + await promisifiedTreeKill(p.pid, 'SIGKILL'); |
| 114 | + await killPort(port); |
| 115 | + } catch { |
| 116 | + // ignore |
| 117 | + } |
| 118 | + }, 200000); |
| 119 | + }); |
| 120 | +}); |
0 commit comments