Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add skip install #68

Merged
merged 15 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .env
Empty file.
7 changes: 7 additions & 0 deletions e2e/nx-verdaccio-e2e/fixtures/basic-nx-workspace.ts
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';
18 changes: 18 additions & 0 deletions e2e/nx-verdaccio-e2e/setup/exec-global-setup.ts
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 });
})();
164 changes: 164 additions & 0 deletions e2e/nx-verdaccio-e2e/setup/setup.ts
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,
},
},
};
}
3 changes: 1 addition & 2 deletions e2e/nx-verdaccio-e2e/test/plugin-create-nodes.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
});

afterEach(async () => {
await teardownTestFolder(baseDir);
// await teardownTestFolder(baseDir);
});

it('should add package targets to library project', async () => {
Expand Down Expand Up @@ -274,7 +274,6 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
}),
})
);

});

it('should NOT add environment targets to project without targetName e2e', async () => {
Expand Down
1 change: 1 addition & 0 deletions e2e/nx-verdaccio-e2e/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"include": [
"vite.config.ts",
"vitest.config.ts",
"setup/**/*.ts",
"test/**/*.test.ts",
"test/**/*.spec.ts",
"test/**/*.test.tsx",
Expand Down
30 changes: 30 additions & 0 deletions examples/e2e/cli-post-script-e2e/.eslintrc.json
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}"]
}
]
}
}
]
}
89 changes: 89 additions & 0 deletions examples/e2e/cli-post-script-e2e/README.md
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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"name": "Michael"
},
{
"name": "Alice"
}
]
20 changes: 20 additions & 0 deletions examples/e2e/cli-post-script-e2e/project.json
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"]
}
}
}
1 change: 1 addition & 0 deletions examples/e2e/cli-post-script-e2e/setup/config.ts
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 examples/e2e/cli-post-script-e2e/setup/exec-global-setup.ts
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 });
})();
Loading
Loading