Skip to content

Commit

Permalink
setup npm env
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 1, 2024
1 parent c872ea9 commit 6890726
Show file tree
Hide file tree
Showing 21 changed files with 11,442 additions and 290 deletions.
30 changes: 30 additions & 0 deletions e2e/cli-e2e-env/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"extends": ["../../.eslintrc.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}"]
}
]
}
}
]
}
3 changes: 3 additions & 0 deletions e2e/cli-e2e-env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# cli-e2e-env

End-to-end tests for the `cli` library.
17 changes: 17 additions & 0 deletions e2e/cli-e2e-env/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "cli-e2e-env",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "projects/cli-e2e-env/test",
"projectType": "library",
"tags": ["npm-env"],
"targets": {
"e2e": {
"executor": "@nx/vite:test",
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
"options": {
"reportsDirectory": "../../coverage/projects/cli-e2e-env"
}
}
}
}
68 changes: 68 additions & 0 deletions e2e/cli-e2e-env/setup/global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { executeProcess } from '@org/test-utils';
import { NpmTestEnvResult, startNpmEnv } from '../../../tools/utils/env';
import { join, relative } from 'node:path';
import { rm } from 'node:fs/promises';

const isVerbose: boolean = process.env.NX_VERBOSE_LOGGING === 'true' ?? false;
const teardownEnv: boolean = process.env.E2E_TEARDOWN_ENV !== 'false';

let activeRegistry: NpmTestEnvResult;
const projectName = process.env['NX_TASK_TARGET_PROJECT'];
let stopRegistry;

export async function setup() {
// start registry
activeRegistry = await startNpmEnv({
projectName,
workspaceRoot: join('tmp', 'e2e', projectName),
targetName: 'start-verdaccio',
verbose: isVerbose,
clear: true,
});

const { registry, workspaceRoot, stop } = activeRegistry;
stopRegistry = stop;

const { url } = registry;
const userconfig = join(workspaceRoot, '.npmrc');
// package publish all projects
await executeProcess({
command: 'nx',
args: ['run-many', '-t=npm-publish', `--userconfig=${userconfig}`],
observer: {
onStdout: (stdout) => {
if (isVerbose) {
console.info(stdout);
}
},
onStderr: (stdout) => {
if (isVerbose) {
console.error(stdout);
}
},
},
});

// package install all projects to test env folder
await executeProcess({
command: 'nx',
args: ['run-many', '-t=npm-install', `--prefix=${workspaceRoot}`],
observer: {
onStdout: (stdout) => {
if (isVerbose) {
console.info(stdout);
}
},
onStderr: (stdout) => {
if (isVerbose) {
console.error(stdout);
}
},
},
});
}

export async function teardown() {
stopRegistry();
// teardownEnv && await rm(activeRegistry.workspaceRoot, {recursive: true, force: true});
}
32 changes: 32 additions & 0 deletions e2e/cli-e2e-env/test/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { dirname, join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { execSync } from 'node:child_process';

describe('CLI command - sort', () => {
const baseDir = 'tmp/cli-e2e-env/sort';

afterEach(async () => {
// await rm(baseDir, {recursive: true, force: true});
});

it('should execute CLI command sort when param file is given', async () => {
const testPath = join(baseDir, 'file-sort', 'users.json');
await mkdir(dirname(testPath), { recursive: true });
await writeFile(
testPath,
JSON.stringify([{ name: 'Michael' }, { name: 'Alice' }])
);

expect(() => execSync(`npx @org/cli sort --file="${testPath}"`)).toThrow(
'Command failed: npx @org/cli sort --file="tmp/cli/sort/file-sort/users.json"'
);
/*
const content = (await readFile(testPath)).toString();
expect(JSON.parse(content)).toEqual([
{name: 'Alice'},
{name: 'Michael'},
]);
*/
});
});
19 changes: 19 additions & 0 deletions e2e/cli-e2e-env/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"
}
]
}
26 changes: 26 additions & 0 deletions e2e/cli-e2e-env/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": [
"vitest/globals",
"vitest/importMeta",
"vite/client",
"node",
"vitest"
]
},
"include": [
"vite.config.ts",
"vitest.config.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"
]
}
28 changes: 28 additions & 0 deletions e2e/cli-e2e-env/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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',

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'],
globalSetup: ['./setup/global-setup.ts'],
coverage: {
reportsDirectory: '../../coverage/projects/cli',
provider: 'v8',
},
},
});
41 changes: 21 additions & 20 deletions e2e/cli-e2e-original/setup/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
import { join } from 'node:path';
import { executeProcess } from '@org/test-utils';
import { startVerdaccioServer } from '../../../tools/utils/registry';

import {join} from "node:path";
import {executeProcess} from "test-utils";
import {startVerdaccioServer} from "../../../tools/utils/registry";

const isVerbose: boolean = true;// process.env.NX_VERBOSE_LOGGING === 'true' ?? false;
const isVerbose: boolean = true; // process.env.NX_VERBOSE_LOGGING === 'true' ?? false;
let stopRegistry;

export async function setup() {

// start registry
stopRegistry = await startVerdaccioServer({
targetName: 'local-registry',
storage: join('tmp', 'cli-source', 'local-registry', 'storage'),
verbose: isVerbose,
port: '4873'
port: '4873',
});


// package publish all projects
await executeProcess({
command: 'nx',
args: ['run-many', '-t=nx-release-publish', '--registry=http://localhost:4873'],
args: [
'run-many',
'-t=nx-release-publish',
'--registry=http://localhost:4873',
],
observer: {
onStdout: stdout => {
onStdout: (stdout) => {
if (isVerbose) {
console.info(stdout)
console.info(stdout);
}
},
onStderr: stdout => {
onStderr: (stdout) => {
if (isVerbose) {
console.error(stdout)
console.error(stdout);
}
}
},
},
});
// package install all projects
await executeProcess({
command: 'nx',
args: ['run-many', '-t=npm-install', '--registry=http://localhost:4873'],
observer: {
onStdout: stdout => {
onStdout: (stdout) => {
if (isVerbose) {
console.info(stdout)
console.info(stdout);
}
},
onStderr: stdout => {
onStderr: (stdout) => {
if (isVerbose) {
console.error(stdout)
console.error(stdout);
}
}
},
},
});
}

export async function teardown() {
// stop registry
stopRegistry()
stopRegistry();
}
59 changes: 0 additions & 59 deletions e2e/cli-e2e/setup/global-setup.ts

This file was deleted.

Loading

0 comments on commit 6890726

Please sign in to comment.