Skip to content

Commit

Permalink
feat(nx-verdaccio): add skipInstall and post script
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Dec 9, 2024
1 parent c957a0a commit f2c0c86
Show file tree
Hide file tree
Showing 17 changed files with 417 additions and 25 deletions.
69 changes: 69 additions & 0 deletions e2e/nx-verdaccio-e2e/test/target-env-setup.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { Tree } from '@nx/devkit';
import { join } from 'node:path';
import { afterEach, expect } from 'vitest';
import {
addJsLibToWorkspace,
materializeTree,
registerPluginInWorkspace,
} from '@push-based/test-nx-utils';
import { updateProjectConfiguration } from 'nx/src/generators/utils/project-configuration';
import { executeProcess, teardownTestFolder } from '@push-based/test-utils';
import { TARGET_ENVIRONMENT_SETUP } from '@push-based/nx-verdaccio';

describe('nx-verdaccio plugin nxv-env-setup target', () => {
let tree: Tree;
const projectA = 'lib-a';
const projectAE2e = `${projectA}-e2e`;
const e2eProjectARoot = join('projects', projectAE2e);
const baseDir = `tmp/environments/${process.env['NX_TASK_TARGET_PROJECT']}/__test__/target-env-setup`;

beforeEach(async () => {
tree = await addJsLibToWorkspace(projectA);
await addJsLibToWorkspace(projectAE2e, tree);
updateProjectConfiguration(tree, projectAE2e, {
root: e2eProjectARoot,
projectType: 'application',
});
updateProjectConfiguration(tree, projectAE2e, {
root: e2eProjectARoot,
projectType: 'application',
targets: {
e2e: {
command: "echo 'Run E2E tests'",
},
},
});
registerPluginInWorkspace(tree, {
plugin: '@push-based/nx-verdaccio',
options: {
environments: {
targetNames: ['e2e'],
},
},
});
await materializeTree(tree, baseDir);
const { stdout } = await executeProcess({
command: 'npm',
args: ['install', 'verdaccio'],
cwd: baseDir,
});
});

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

it('should have caching enabled by default', async () => {
const { stdout } = await executeProcess({
command: 'nx',
args: [
'run',
`${projectAE2e}:${TARGET_ENVIRONMENT_SETUP}`,
'--with-deps',
'--skip-nx-cache',
],
cwd: baseDir,
});
expect(stdout).toContain('Environment setup completed');
});
});
30 changes: 30 additions & 0 deletions examples/e2e/cli-custom-install-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}"]
}
]
}
}
]
}
23 changes: 23 additions & 0 deletions examples/e2e/cli-custom-install-e2e/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "cli-custom-install-e2e",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "projects/cli-custom-install-e2e/test",
"projectType": "application",
"tags": ["type:e2e", "type:e2e-vi", "type:example"],
"implicitDependencies": ["cli"],
"targets": {
"lint": {},
"nxv-env-setup": {
"cache": false,
"options": {
"skipInstall": true,
"keepServerRunning": true,
"postScript": "npx tsx --tsconfig examples/e2e/cli-custom-install-e2e/tsconfig.spec.json examples/e2e/cli-custom-install-e2e/setup/exec-global-setup.ts"
}
},
"e2e": {
"executor": "@nx/vite:test",
"inputs": ["default", "^production"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import setup from './global.setup.ts';

(async () => await setup())();
74 changes: 74 additions & 0 deletions examples/e2e/cli-custom-install-e2e/setup/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
DEFAULT_TEST_FIXTURE_DIST,
executeProcess,
getTestEnvironmentRoot,
objectToCliArgs,
teardownTestFolder,
} from '@push-based/test-utils';
import { join } from 'node:path';
import { copyFile, mkdir } from 'node:fs/promises';

export async function setup(
repoPath: string,
repoName: string,
projectName: string
) {
// setup nx environment for e2e tests
await executeProcess({
command: 'npx',
args: objectToCliArgs({
_: ['--yes', 'create-nx-workspace'],
name: repoPath,
preset: 'ts-standalone',
ci: 'skip',
interactive: false,
}),
verbose: true,
});
await mkdir(
join(
getTestEnvironmentRoot(projectName),
DEFAULT_TEST_FIXTURE_DIST,
repoName
),
{ recursive: true }
);
await copyFile(
join(getTestEnvironmentRoot(projectName), '.npmrc'),
join(repoPath, '.npmrc')
);

await executeProcess({
command: 'npm',
args: objectToCliArgs({
_: ['install', '@push-based/cli'],
save: true,
}),
cwd: repoPath,
});

const testPath = join(repoPath, 'users.json');
await mkdir(dirname(testPath), { recursive: true });
await writeFile(
testPath,
JSON.stringify([{ name: 'Michael' }, { name: 'Alice' }])
);
const json = JSON.parse(
(await readFile(join(repoPath, 'project.json'))).toString()
);
await writeFile(
join(repoPath, 'project.json'),
JSON.stringify({
...json,
targets: {
...json.targets,
sort: {
executor: '@push-based/cli:sort',
options: {
filePath: 'users.json',
},
},
},
})
);
}
29 changes: 29 additions & 0 deletions examples/e2e/cli-custom-install-e2e/setup/vitest.global.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
DEFAULT_TEST_FIXTURE_DIST,
executeProcess,
getTestEnvironmentRoot,
objectToCliArgs,
teardownTestFolder,
} from '@push-based/test-utils';
import { join } from 'node:path';
import { copyFile, mkdir } from 'node:fs/promises';

const projectName = 'cli-custom-install-e2e';
const repoName = 'nx-ts-repo';
const repoPath = join(
getTestEnvironmentRoot(projectName),
DEFAULT_TEST_FIXTURE_DIST,
repoName
);

export default async function vitestGlobalSetup() {
// clean
teardownTestFolder(repoPath);

// setup nx environment for e2e tests
setup(repoPath, repoName, projectName);

return () => {
return teardownTestFolder(repoPath);
};
}
41 changes: 41 additions & 0 deletions examples/e2e/cli-custom-install-e2e/test/nx-target-sort.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { basename, dirname, join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { readFile } from 'node:fs/promises';
import {
executeProcess,
objectToCliArgs,
getTestEnvironmentRoot,
DEFAULT_TEST_FIXTURE_DIST,
} from '@push-based/test-utils';

describe('Nx target - sort', () => {
const projectName = 'cli-custom-install-e2e';
const envRoot = getTestEnvironmentRoot(projectName);
const repoRoot = join(envRoot, DEFAULT_TEST_FIXTURE_DIST, 'nx-ts-repo');

it('should execute Nx target in nx@v19 setup', async () => {
const testPath = join(repoRoot, 'users.json');
await mkdir(dirname(testPath), { recursive: true });
await writeFile(
testPath,
JSON.stringify([{ name: 'Michael' }, { name: 'Alice' }])
);
const { code } = await executeProcess({
command: 'npx',
args: objectToCliArgs({
_: ['nx', 'sort'],
filePath: testPath,
}),
cwd: repoRoot,
verbose: true,
});

expect(code).toBe(0); /**/
/*
const content = (await readFile(testPath)).toString();
expect(JSON.parse(content)).toStrictEqual([
{ name: 'Alice' },
{ name: 'Michael' },
]);*/
});
});
19 changes: 19 additions & 0 deletions examples/e2e/cli-custom-install-e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "esnext",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.spec.json"
}
]
}
27 changes: 27 additions & 0 deletions examples/e2e/cli-custom-install-e2e/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"types": [
"vitest/globals",
"vitest/importMeta",
"vite/client",
"node",
"vitest"
]
},
"include": [
"vite.config.ts",
"vitest.config.ts",
"setup/**/*.ts",
"test/**/*.test.ts",
"test/**/*.spec.ts",
"test/**/*.test.tsx",
"test/**/*.spec.tsx",
"test/**/*.test.js",
"test/**/*.spec.js",
"test/**/*.test.jsx",
"test/**/*.spec.jsx",
"test/**/*.d.ts"
]
}
27 changes: 27 additions & 0 deletions examples/e2e/cli-custom-install-e2e/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineConfig } from 'vite';

import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';

export default defineConfig({
root: __dirname,
cacheDir: '../../../node_modules/.vite/projects/cli-custom-install-e2e',

plugins: [nxViteTsPaths()],

// Uncomment this if you are using workers.
// worker: {
// plugins: [ nxViteTsPaths() ],
// },

test: {
globals: true,
cache: { dir: '../../../node_modules/.vitest' },
environment: 'node',
include: ['test/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: '../../../coverage/projects/cli-custom-install-e2e',
provider: 'v8',
},
},
});
Loading

0 comments on commit f2c0c86

Please sign in to comment.