-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
882 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { join } from 'node:path'; | ||
|
||
export const REPO_NAME = 'nx-ts-repo'; | ||
export const envRoot = `tmp/environments/${process.env['NX_TASK_TARGET_PROJECT']}`; | ||
export const workspaceRoot = join(envRoot, '__test__', REPO_NAME); | ||
export const projectName = 'pkg'; | ||
export const e2eProjectName = 'pkg-e2e'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { setup } from './setup.ts'; | ||
import { REPO_NAME } from '../fixtures/basic-nx-workspace'; | ||
import { join } from 'node:path'; | ||
import { | ||
DEFAULT_TEST_FIXTURE_DIST, | ||
getTestEnvironmentRoot, | ||
teardownTestFolder, | ||
} from '@push-based/test-utils'; | ||
|
||
(async () => { | ||
const projectName = process.env['NX_TASK_TARGET_PROJECT']; | ||
const envRoot = getTestEnvironmentRoot(projectName); | ||
const repoPath = join(envRoot, DEFAULT_TEST_FIXTURE_DIST, REPO_NAME); | ||
|
||
// clean up previous runs | ||
await teardownTestFolder(repoPath); | ||
await setup({ envRoot: repoPath, repoName: REPO_NAME, projectName }); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { | ||
DEFAULT_TEST_FIXTURE_DIST, | ||
executeProcess, | ||
getTestEnvironmentRoot, | ||
objectToCliArgs, | ||
updateJson, | ||
} from '@push-based/test-utils'; | ||
import { dirname, join } from 'node:path'; | ||
import { copyFile, mkdir } from 'node:fs/promises'; | ||
import { | ||
logger, | ||
NxJsonConfiguration, | ||
PluginConfiguration, | ||
TargetConfiguration, | ||
} from '@nx/devkit'; | ||
import { PackageJson } from 'nx/src/utils/package-json'; | ||
|
||
export async function setup({ | ||
envRoot, | ||
projectName, | ||
repoName, | ||
}: { | ||
envRoot: string; | ||
repoName: string; | ||
projectName: string; | ||
}) { | ||
await mkdir(envRoot, { recursive: true }); | ||
// setup nx environment for e2e tests | ||
logger.info(`Created nx workspace under ${envRoot}`); | ||
await executeProcess({ | ||
command: 'npx', | ||
args: objectToCliArgs({ | ||
_: ['--yes', '--quiet', 'create-nx-workspace'], | ||
name: repoName, | ||
preset: 'ts', | ||
ci: 'skip', | ||
e2eTestRunner: 'none', | ||
interactive: false, | ||
}), | ||
verbose: true, | ||
cwd: dirname(envRoot), | ||
}); | ||
|
||
logger.info(`Add project & target`); | ||
await executeProcess({ | ||
command: 'nx', | ||
args: objectToCliArgs({ | ||
_: ['generate', '@nx/js:library', 'pkg'], | ||
directory: 'packages/pkg', | ||
bundler: 'tsc', | ||
unitTestRunner: 'none', | ||
linter: 'none', | ||
interactive: false, | ||
}), | ||
verbose: true, | ||
cwd: envRoot, | ||
}); | ||
await updateJson<PackageJson>( | ||
join(envRoot, 'packages', 'pkg', 'package.json'), | ||
(json) => ({ | ||
...json, | ||
nx: { | ||
...json?.nx, | ||
targets: { | ||
...json?.nx?.targets, | ||
build: { | ||
options: { | ||
outputPath: ['dist/pkg'], | ||
}, | ||
command: 'echo "lib"', | ||
}, | ||
}, | ||
}, | ||
}) | ||
); | ||
|
||
logger.info(`Add e2e project & target`); | ||
await executeProcess({ | ||
command: 'nx', | ||
args: objectToCliArgs({ | ||
_: ['generate', '@nx/js:library', 'pkg-e2e'], | ||
directory: 'packages/pkg-e2e', | ||
bundler: 'tsc', | ||
unitTestRunner: 'none', | ||
linter: 'none', | ||
interactive: false, | ||
}), | ||
verbose: true, | ||
cwd: envRoot, | ||
}); | ||
await updateJson<PackageJson>( | ||
join(envRoot, 'packages', 'pkg-e2e', 'package.json'), | ||
(json) => | ||
updatePackageJsonNxTargets(json, { | ||
...json?.nx?.targets, | ||
e2e: { | ||
command: 'echo "e2e"', | ||
}, | ||
}) | ||
); | ||
|
||
logger.info(`Install @push-based/nx-verdaccio`); | ||
await mkdir( | ||
join( | ||
getTestEnvironmentRoot(projectName), | ||
DEFAULT_TEST_FIXTURE_DIST, | ||
repoName | ||
), | ||
{ recursive: true } | ||
); | ||
await copyFile( | ||
join(getTestEnvironmentRoot(projectName), '.npmrc'), | ||
join(envRoot, '.npmrc') | ||
); | ||
await executeProcess({ | ||
command: 'npm', | ||
args: objectToCliArgs({ | ||
_: ['install', '@push-based/nx-verdaccio'], | ||
save: true, | ||
}), | ||
cwd: envRoot, | ||
verbose: true, | ||
}); | ||
} | ||
|
||
export async function registerNxVerdaccioPlugin(envRoot: string) { | ||
logger.info(`register nx-verdaccio plugin`); | ||
await updateJson<NxJsonConfiguration>(join(envRoot, 'nx.json'), (json) => | ||
registerPluginInNxJson(json, { | ||
plugin: '@push-based/nx-verdaccio', | ||
options: { | ||
environments: { | ||
targetNames: ['e2e'], | ||
}, | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
function registerPluginInNxJson( | ||
nxJson: NxJsonConfiguration, | ||
pluginConfig: PluginConfiguration | ||
) { | ||
return { | ||
...nxJson, | ||
plugins: [...(nxJson?.plugins ?? []), pluginConfig], | ||
}; | ||
} | ||
|
||
function updatePackageJsonNxTargets( | ||
pkgJson: PackageJson, | ||
targets: Record<string, TargetConfiguration> | ||
) { | ||
return { | ||
...pkgJson, | ||
nx: { | ||
...pkgJson?.nx, | ||
targets: { | ||
...pkgJson?.nx?.targets, | ||
...targets, | ||
}, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.base.json", "../../../.eslintrc.base.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/dependency-checks": [ | ||
"error", | ||
{ | ||
"ignoredFiles": ["{projectRoot}/vite.config.{js,ts,mjs,mts}"] | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Nx Verdaccio Example - Custom Test Environment Setup 🧪 | ||
|
||
This example demonstrates how to set up a custom test environment for projects with specific tags. | ||
It creates a Nx workspace, installs the package dependencies after the Nx setup is done, and runs the E2E tests for the project. | ||
|
||
## Plugin Configuration | ||
|
||
**nx.json** | ||
|
||
```json | ||
{ | ||
"plugins": [ | ||
{ | ||
"plugin": "@push-based/nx-verdaccio", | ||
"options": { | ||
"environments": { | ||
"targetNames": ["e2e"] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Project Configuration | ||
|
||
**projects/utils/project.json** | ||
|
||
```jsonc | ||
{ | ||
"implicitDependencies": ["cli"], | ||
"targets": { | ||
"nxv-env-setup": { | ||
"options": { | ||
"skipInstall": true, | ||
"postScript": "npx tsx --tsconfig examples/e2e/cli-post-script-e2e/tsconfig.spec.json examples/e2e/cli-post-script-e2e/setup/exec-global-setup.ts" | ||
} | ||
}, | ||
"e2e": { | ||
// test provides data | ||
} | ||
} | ||
} | ||
``` | ||
|
||
**projects/utils/setup/exec-global-setup.ts** | ||
|
||
```ts | ||
export async function setup({ userconfig, envRoot, projectName, repoName }: { envRoot: string; repoName: string; userconfig: string; projectName: string }) { | ||
// setup nx environment for e2e tests | ||
await execSync(`npx --yes create-nx-workspace@latest --preset=ts-standalone --ci=skip --no-interactive --name=${repoName}`, { | ||
cwd: dirname(envRoot), | ||
}); | ||
// install cli | ||
await execSync(`npm install @push-based/cli`, { | ||
cwd: envRoot, | ||
}); | ||
|
||
// update project.json with target | ||
const json = JSON.parse((await readFile(join(envRoot, 'project.json'))).toString()); | ||
await writeFile( | ||
join(envRoot, 'project.json'), | ||
JSON.stringify({ | ||
...json, | ||
targets: { | ||
...json.targets, | ||
sort: { | ||
command: 'npx cli sort --filePath=users.json', | ||
}, | ||
}, | ||
}) | ||
); | ||
|
||
// copy data to test | ||
await cp(join('examples', 'e2e', 'cli-post-scripts', 'fixtures', 'small-data'), envRoot, { recursive: true }); | ||
} | ||
``` | ||
|
||
## Running the Example | ||
|
||
```bash | ||
nx run cli-post-script-e2e:nxv-e2e | ||
``` | ||
|
||
## Inspect the test environment setup | ||
|
||
```bash | ||
nx run cli-post-script-e2e:nxv-env-setup --keepServerRunning --verboes | ||
``` |
8 changes: 8 additions & 0 deletions
8
examples/e2e/cli-post-script-e2e/fixtures/small-data/users.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
"name": "Michael" | ||
}, | ||
{ | ||
"name": "Alice" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "cli-post-script-e2e", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "projects/cli-post-script-e2e/test", | ||
"projectType": "application", | ||
"tags": ["type:e2e", "type:e2e-vi", "type:example"], | ||
"implicitDependencies": ["cli"], | ||
"targets": { | ||
"nxv-env-setup": { | ||
"options": { | ||
"skipInstall": true, | ||
"postScript": "npx tsx --tsconfig examples/e2e/cli-post-script-e2e/tsconfig.spec.json examples/e2e/cli-post-script-e2e/setup/exec-global-setup.ts" | ||
} | ||
}, | ||
"e2e": { | ||
"executor": "@nx/vite:test", | ||
"inputs": ["default", "^production"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const REPO_NAME = 'nx-ts-repo'; |
18 changes: 18 additions & 0 deletions
18
examples/e2e/cli-post-script-e2e/setup/exec-global-setup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { setup } from './setup.ts'; | ||
import { REPO_NAME } from './config.ts'; | ||
import { join } from 'node:path'; | ||
import { | ||
DEFAULT_TEST_FIXTURE_DIST, | ||
getTestEnvironmentRoot, | ||
teardownTestFolder, | ||
} from '@push-based/test-utils'; | ||
|
||
export const projectName = 'cli-post-script-e2e'; | ||
const envRoot = getTestEnvironmentRoot(projectName); | ||
export const repoPath = join(envRoot, DEFAULT_TEST_FIXTURE_DIST, REPO_NAME); | ||
|
||
(async () => { | ||
// clean up previous runs | ||
await teardownTestFolder(repoPath); | ||
await setup({ envRoot: repoPath, repoName: REPO_NAME, projectName }); | ||
})(); |
Oops, something went wrong.