From 919e288e028265a03d7b8ea74f78f4cfc4534ac5 Mon Sep 17 00:00:00 2001 From: Tine Kondo Date: Sat, 14 Dec 2024 23:09:38 +0000 Subject: [PATCH] test: fix `e2e` tests --- .../tests/create-nx-flutter.spec.ts | 3 +- e2e/nx-flutter-e2e/tests/nx-flutter.spec.ts | 36 +++++++++++-------- e2e/nx-ktor-e2e/tests/create-nx-ktor.spec.ts | 3 +- e2e/nx-ktor-e2e/tests/nx-ktor.spec.ts | 4 +-- e2e/nx-melos-e2e/tests/nx-melos.spec.ts | 2 +- .../tests/create-nx-micronaut.spec.ts | 3 +- .../tests/nx-micronaut.spec.ts | 4 +-- .../tests/create-nx-quarkus.spec.ts | 3 +- e2e/nx-quarkus-e2e/tests/nx-quarkus.spec.ts | 4 +-- .../tests/create-nx-spring-boot.spec.ts | 10 +++--- .../tests/nx-spring-boot.spec.ts | 4 +-- packages/common/testing/e2e/index.ts | 2 +- tools/scripts/start-local-registry.ts | 8 ++--- tools/scripts/utils.ts | 13 +++++++ 14 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 tools/scripts/utils.ts diff --git a/e2e/nx-flutter-e2e/tests/create-nx-flutter.spec.ts b/e2e/nx-flutter-e2e/tests/create-nx-flutter.spec.ts index e7479eb8..7c05d7bd 100644 --- a/e2e/nx-flutter-e2e/tests/create-nx-flutter.spec.ts +++ b/e2e/nx-flutter-e2e/tests/create-nx-flutter.spec.ts @@ -24,7 +24,7 @@ describe('create-nx-flutter', () => { `('should be installed with Nx Wrapper=$useNxWrapper', ({ useNxWrapper }) => { projectDirectory = createCLITestProject( 'create-nx-flutter', - `--prjName=flutapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --useGitHub=false --no-interactive` + `--directory=flutapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --useGitHub=false --no-interactive` ); // npm ls will fail if the package is not installed properly @@ -33,6 +33,7 @@ describe('create-nx-flutter', () => { ? join(projectDirectory, '/.nx/installation') : projectDirectory, stdio: 'inherit', + env: process.env, }); expect(hasNxWrapper(projectDirectory)).toEqual(useNxWrapper); diff --git a/e2e/nx-flutter-e2e/tests/nx-flutter.spec.ts b/e2e/nx-flutter-e2e/tests/nx-flutter.spec.ts index a60f5e0a..47da1edf 100644 --- a/e2e/nx-flutter-e2e/tests/nx-flutter.spec.ts +++ b/e2e/nx-flutter-e2e/tests/nx-flutter.spec.ts @@ -1,12 +1,16 @@ +import { uniq } from '@nx/plugin/testing'; import { - uniq, -} from '@nx/plugin/testing'; -import { checkFilesExist, createTestProject, readJson, runNxCommandAsync } from '@nxrocks/common/testing'; + checkFilesExist, + createTestProject, + readJson, + runNxCommandAsync, +} from '@nxrocks/common/testing'; import { execSync } from 'child_process'; import { rmSync } from 'fs-extra'; //jest.mock('enquirer'); // we mock 'enquirer' to bypass the interactive prompt import * as enquirer from 'enquirer'; +import { getPackageManagerCommand } from '@nx/devkit'; describe('nx-flutter e2e', () => { let projectDirectory: string; @@ -16,24 +20,28 @@ describe('nx-flutter e2e', () => { // The plugin has been built and published to a local registry in the jest globalSetup // Install the plugin built with the latest source code into the test repo - execSync(`npm install @nxrocks/nx-flutter@0.0.0-e2e`, { - cwd: projectDirectory, - stdio: 'inherit', - env: process.env, - }); + execSync( + `${getPackageManagerCommand().addDev} @nxrocks/nx-flutter@0.0.0-e2e`, + { + cwd: projectDirectory, + stdio: 'inherit', + env: process.env, + } + ); }); afterAll(() => { // Cleanup the test project - projectDirectory && rmSync(projectDirectory, { - recursive: true, - force: true, - }); + projectDirectory && + rmSync(projectDirectory, { + recursive: true, + force: true, + }); }); it('should be installed', () => { // npm ls will fail if the package is not installed properly - execSync('npm ls @nxrocks/nx-flutter', { + execSync(`${getPackageManagerCommand().list} @nxrocks/nx-flutter`, { cwd: projectDirectory, stdio: 'inherit', }); @@ -154,7 +162,7 @@ describe('nx-flutter e2e', () => { const appName = uniq('nx-flutter'); await runNxCommandAsync( - `generate @nxrocks/nx-flutter:create ${appName} --directory subdir --no-interactive` + `generate @nxrocks/nx-flutter:new --directory subdir/${appName} --no-interactive` ); expect(() => checkFilesExist(`subdir/${appName}/pubspec.yaml`) diff --git a/e2e/nx-ktor-e2e/tests/create-nx-ktor.spec.ts b/e2e/nx-ktor-e2e/tests/create-nx-ktor.spec.ts index 38d62f6c..280f9638 100644 --- a/e2e/nx-ktor-e2e/tests/create-nx-ktor.spec.ts +++ b/e2e/nx-ktor-e2e/tests/create-nx-ktor.spec.ts @@ -24,7 +24,7 @@ describe('create-nx-ktor', () => { `('should be installed with Nx Wrapper=$useNxWrapper', ({ useNxWrapper }) => { projectDirectory = createCLITestProject( 'create-nx-ktor', - `--prjName=bootapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --no-interactive --verbose=true` + `--directory=ktapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --useGitHub=false --no-interactive --verbose=true` ); // npm ls will fail if the package is not installed properly @@ -33,6 +33,7 @@ describe('create-nx-ktor', () => { ? join(projectDirectory, '/.nx/installation') : projectDirectory, stdio: 'inherit', + env: process.env, }); expect(hasNxWrapper(projectDirectory)).toEqual(useNxWrapper); diff --git a/e2e/nx-ktor-e2e/tests/nx-ktor.spec.ts b/e2e/nx-ktor-e2e/tests/nx-ktor.spec.ts index 7eb7a4fd..2d41f7b9 100644 --- a/e2e/nx-ktor-e2e/tests/nx-ktor.spec.ts +++ b/e2e/nx-ktor-e2e/tests/nx-ktor.spec.ts @@ -21,7 +21,7 @@ describe('nx-ktor e2e', () => { // The plugin has been built and published to a local registry in the jest globalSetup // Install the plugin built with the latest source code into the test repo execSync( - `${getPackageManagerCommand().install} @nxrocks/nx-ktor@0.0.0-e2e`, + `${getPackageManagerCommand().addDev} @nxrocks/nx-ktor@0.0.0-e2e`, { cwd: projectDirectory, stdio: 'inherit', @@ -78,7 +78,7 @@ describe('nx-ktor e2e', () => { it('should create src in the specified directory', async () => { const prjName = uniq('nx-ktor'); await runNxCommandAsync( - `generate @nxrocks/nx-ktor:new ${prjName} --directory subdir --no-interactive` + `generate @nxrocks/nx-ktor:new --directory subdir/${prjName} --no-interactive` ); expect(() => checkFilesExist( diff --git a/e2e/nx-melos-e2e/tests/nx-melos.spec.ts b/e2e/nx-melos-e2e/tests/nx-melos.spec.ts index 8e27cd86..764f9661 100644 --- a/e2e/nx-melos-e2e/tests/nx-melos.spec.ts +++ b/e2e/nx-melos-e2e/tests/nx-melos.spec.ts @@ -16,7 +16,7 @@ describe('nx-melos e2e', () => { // The plugin has been built and published to a local registry in the jest globalSetup // Install the plugin built with the latest source code into the test repo execSync( - `${getPackageManagerCommand().install} @nxrocks/nx-melos@0.0.0-e2e`, + `${getPackageManagerCommand().addDev} @nxrocks/nx-melos@0.0.0-e2e`, { cwd: projectDirectory, stdio: 'inherit', diff --git a/e2e/nx-micronaut-e2e/tests/create-nx-micronaut.spec.ts b/e2e/nx-micronaut-e2e/tests/create-nx-micronaut.spec.ts index 437c05f1..8641f176 100644 --- a/e2e/nx-micronaut-e2e/tests/create-nx-micronaut.spec.ts +++ b/e2e/nx-micronaut-e2e/tests/create-nx-micronaut.spec.ts @@ -24,7 +24,7 @@ describe('create-nx-micronaut', () => { `('should be installed with Nx Wrapper=$useNxWrapper', ({ useNxWrapper }) => { projectDirectory = createCLITestProject( 'create-nx-micronaut', - `--prjName=bootapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --useGitHub=false --no-interactive` + `--directory=mnapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --useGitHub=false --no-interactive` ); // npm ls will fail if the package is not installed properly @@ -33,6 +33,7 @@ describe('create-nx-micronaut', () => { ? join(projectDirectory, '/.nx/installation') : projectDirectory, stdio: 'inherit', + env: process.env, }); expect(hasNxWrapper(projectDirectory)).toEqual(useNxWrapper); diff --git a/e2e/nx-micronaut-e2e/tests/nx-micronaut.spec.ts b/e2e/nx-micronaut-e2e/tests/nx-micronaut.spec.ts index d930efd5..5f7c09b3 100644 --- a/e2e/nx-micronaut-e2e/tests/nx-micronaut.spec.ts +++ b/e2e/nx-micronaut-e2e/tests/nx-micronaut.spec.ts @@ -22,7 +22,7 @@ describe('nx-micronaut e2e', () => { // The plugin has been built and published to a local registry in the jest globalSetup // Install the plugin built with the latest source code into the test repo execSync( - `${getPackageManagerCommand().install} @nxrocks/nx-micronaut@0.0.0-e2e`, + `${getPackageManagerCommand().addDev} @nxrocks/nx-micronaut@0.0.0-e2e`, { cwd: projectDirectory, stdio: 'inherit', @@ -206,7 +206,7 @@ describe('nx-micronaut e2e', () => { it('should create src in the specified directory', async () => { const prjName = uniq('nx-micronaut'); await runNxCommandAsync( - `generate @nxrocks/nx-micronaut:new ${prjName} --directory subdir --no-interactive` + `generate @nxrocks/nx-micronaut:new --directory subdir/${prjName} --no-interactive` ); expect(() => checkFilesExist( diff --git a/e2e/nx-quarkus-e2e/tests/create-nx-quarkus.spec.ts b/e2e/nx-quarkus-e2e/tests/create-nx-quarkus.spec.ts index c57c50a4..6d96fca3 100644 --- a/e2e/nx-quarkus-e2e/tests/create-nx-quarkus.spec.ts +++ b/e2e/nx-quarkus-e2e/tests/create-nx-quarkus.spec.ts @@ -24,7 +24,7 @@ describe('create-nx-quarkus', () => { `('should be installed with Nx Wrapper=$useNxWrapper', ({ useNxWrapper }) => { projectDirectory = createCLITestProject( 'create-nx-quarkus', - `--prjName=bootapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --useGitHub=false --no-interactive` + `--directory=qkapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --useGitHub=false --no-interactive` ); // npm ls will fail if the package is not installed properly @@ -33,6 +33,7 @@ describe('create-nx-quarkus', () => { ? join(projectDirectory, '/.nx/installation') : projectDirectory, stdio: 'inherit', + env: process.env, }); expect(hasNxWrapper(projectDirectory)).toEqual(useNxWrapper); diff --git a/e2e/nx-quarkus-e2e/tests/nx-quarkus.spec.ts b/e2e/nx-quarkus-e2e/tests/nx-quarkus.spec.ts index 68f0b4dc..ac6b1286 100644 --- a/e2e/nx-quarkus-e2e/tests/nx-quarkus.spec.ts +++ b/e2e/nx-quarkus-e2e/tests/nx-quarkus.spec.ts @@ -23,7 +23,7 @@ describe('nx-quarkus e2e', () => { // The plugin has been built and published to a local registry in the jest globalSetup // Install the plugin built with the latest source code into the test repo execSync( - `${getPackageManagerCommand().add} @nxrocks/nx-quarkus@0.0.0-e2e`, + `${getPackageManagerCommand().addDev} @nxrocks/nx-quarkus@0.0.0-e2e`, { cwd: projectDirectory, stdio: 'inherit', @@ -222,7 +222,7 @@ describe('nx-quarkus e2e', () => { const prjName = uniq('nx-quarkus'); await runNxCommandAsync( - `generate @nxrocks/nx-quarkus:new ${prjName} --projectType ${projectType} --directory subdir --no-interactive` + `generate @nxrocks/nx-quarkus:new --directory subdir/${prjName} --projectType ${projectType} --no-interactive` ); expect(() => checkFilesExist( diff --git a/e2e/nx-spring-boot-e2e/tests/create-nx-spring-boot.spec.ts b/e2e/nx-spring-boot-e2e/tests/create-nx-spring-boot.spec.ts index c7999888..a6b126e2 100644 --- a/e2e/nx-spring-boot-e2e/tests/create-nx-spring-boot.spec.ts +++ b/e2e/nx-spring-boot-e2e/tests/create-nx-spring-boot.spec.ts @@ -17,14 +17,11 @@ describe('create-nx-spring-boot', () => { }); }); - it.each` - useNxWrapper - ${true} - ${false} - `('should be installed with Nx Wrapper=$useNxWrapper', ({ useNxWrapper }) => { + it('should be installed with Nx Wrapper=$useNxWrapper', () => { + const useNxWrapper = false; projectDirectory = createCLITestProject( 'create-nx-spring-boot', - `--prjName=bootapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --useGitHub=false --no-interactive` + `--directory=bootapp --useNxWrapper=${useNxWrapper} --nxCloud=skip --useGitHub=false --no-interactive` ); // npm ls will fail if the package is not installed properly @@ -33,6 +30,7 @@ describe('create-nx-spring-boot', () => { ? join(projectDirectory, '/.nx/installation') : projectDirectory, stdio: 'inherit', + env: process.env, }); expect(hasNxWrapper(projectDirectory)).toEqual(useNxWrapper); diff --git a/e2e/nx-spring-boot-e2e/tests/nx-spring-boot.spec.ts b/e2e/nx-spring-boot-e2e/tests/nx-spring-boot.spec.ts index 5d1b9623..670dda95 100644 --- a/e2e/nx-spring-boot-e2e/tests/nx-spring-boot.spec.ts +++ b/e2e/nx-spring-boot-e2e/tests/nx-spring-boot.spec.ts @@ -23,7 +23,7 @@ describe('nx-spring-boot e2e', () => { // The plugin has been built and published to a local registry in the jest globalSetup // Install the plugin built with the latest source code into the test repo execSync( - `${getPackageManagerCommand().install} @nxrocks/nx-spring-boot@0.0.0-e2e`, + `${getPackageManagerCommand().addDev} @nxrocks/nx-spring-boot@0.0.0-e2e`, { cwd: projectDirectory, stdio: 'inherit', @@ -291,7 +291,7 @@ describe('nx-spring-boot e2e', () => { const prjName = uniq('nx-spring-boot'); await runNxCommandAsync( - `generate @nxrocks/nx-spring-boot:new ${prjName} --projectType ${projectType} --directory subdir --no-interactive` + `generate @nxrocks/nx-spring-boot:new --directory subdir/${prjName} --projectType ${projectType} --no-interactive` ); expect(() => checkFilesExist( diff --git a/packages/common/testing/e2e/index.ts b/packages/common/testing/e2e/index.ts index 359c6112..d62e2d12 100644 --- a/packages/common/testing/e2e/index.ts +++ b/packages/common/testing/e2e/index.ts @@ -86,7 +86,7 @@ export function createCLITestProject( execSync( `${ getPackageManagerCommand(pkgManager).dlx - }${confirm} ${createPkgName}@${createPkgVersion} ${projectName} ${extraArgs}`, + }${confirm} ${createPkgName}@${createPkgVersion} ${projectName} ${extraArgs} --presetVersion=${createPkgVersion}`, { cwd: dirname(projectDirectory), stdio: 'inherit', diff --git a/tools/scripts/start-local-registry.ts b/tools/scripts/start-local-registry.ts index 9ade050c..92574520 100644 --- a/tools/scripts/start-local-registry.ts +++ b/tools/scripts/start-local-registry.ts @@ -13,8 +13,7 @@ function isFolderEmptySync(folderPath: string): boolean { const relevantFiles = files.filter(file => file !== '.verdaccio-db.json'); return relevantFiles.length === 0; } catch (error) { - console.error('Error checking folder:', error); - throw error; + return true; // we consider the folder empty if we cannot read it } } @@ -67,9 +66,10 @@ export async function startLocalRelease(isVerbose=false) { }, verbose: isVerbose, }); - await releasePublish({ + + return await releasePublish({ tag: 'e2e', firstRelease: true, verbose: isVerbose, }); -} \ No newline at end of file +} diff --git a/tools/scripts/utils.ts b/tools/scripts/utils.ts new file mode 100644 index 00000000..078c0630 --- /dev/null +++ b/tools/scripts/utils.ts @@ -0,0 +1,13 @@ +import { workspaceRoot } from "@nx/devkit"; +import { execFileSync } from "child_process"; + + +const nx = require.resolve('nx'); + +export const execNx = (args: string[]) => + execFileSync(nx, args, { + env: process.env, + stdio: 'inherit', + maxBuffer: 1024 * 1024 * 10, + cwd: workspaceRoot, + });