Skip to content

Commit

Permalink
chore(watch): add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Dec 28, 2022
1 parent 0c93194 commit 16a4df9
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 0 deletions.
18 changes: 18 additions & 0 deletions e2e/watch/.eslintrc.json
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
18 changes: 18 additions & 0 deletions e2e/watch/jest.config.ts
@@ -0,0 +1,18 @@
/* eslint-disable */
export default {
displayName: "e2e-watch",
preset: "../../jest.preset.js",
globals: {
"ts-jest": {
tsconfig: "<rootDir>/tsconfig.spec.json",
},
},
transform: {
"^.+\\.[tj]s$": "ts-jest",
},
moduleFileExtensions: ["ts", "js", "html"],
coverageDirectory: "../../coverage/e2e/watch",
maxWorkers: 1,
testTimeout: 60000,
setupFiles: ["<rootDir>/src/test-setup.ts"],
};
52 changes: 52 additions & 0 deletions e2e/watch/project.json
@@ -0,0 +1,52 @@
{
"name": "e2e-watch",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"tags": [],
"targets": {
"e2e": {
"executor": "nx:run-commands",
"options": {
"commands": [
{
"command": "npm run e2e-start-local-registry"
},
{
"command": "npm run e2e-build-package-publish"
},
{
"command": "E2E_ROOT=$(npx ts-node scripts/set-e2e-root.ts) nx run-e2e-tests e2e-watch"
}
],
"parallel": false
}
},
"run-e2e-tests-process": {
"executor": "nx:run-commands",
"options": {
"commands": [
{
"command": "E2E_ROOT=$(npx ts-node scripts/set-e2e-root.ts) nx run-e2e-tests e2e-watch",
"description": "This additional wrapper target exists so that we can ensure that the e2e tests run in a dedicated process with enough memory"
}
],
"parallel": false
}
},
"run-e2e-tests": {
"executor": "@nrwl/jest:jest",
"options": {
"jestConfig": "e2e/watch/jest.config.ts",
"passWithNoTests": true,
"runInBand": true
},
"outputs": ["{workspaceRoot}/coverage/e2e/watch"]
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["e2e/watch/**/*.ts"]
}
}
}
}
1 change: 1 addition & 0 deletions e2e/watch/src/test-setup.ts
@@ -0,0 +1 @@
jest.retryTimes(3);
282 changes: 282 additions & 0 deletions e2e/watch/src/watch.spec.ts
@@ -0,0 +1,282 @@
import { Fixture, normalizeEnvironment, wait } from "@lerna/e2e-utils";
import { createFile } from "fs-extra";

expect.addSnapshotSerializer({
serialize(str) {
return normalizeEnvironment(str);
},
test(val) {
return val != null && typeof val === "string";
},
});

describe("lerna-watch", () => {
let fixture: Fixture;

beforeEach(async () => {
fixture = await Fixture.create({
e2eRoot: process.env.E2E_ROOT,
name: "lerna-watch",
packageManager: "npm",
initializeGit: true,
runLernaInit: true,
installDependencies: true,
});

await fixture.lerna("create package-a -y");
await fixture.lerna("create package-b --dependencies package-a -y");
await fixture.lerna("create package-c -y");

await fixture.createInitialGitCommit();
});

afterAll(() => fixture.destroy());

it("should watch all packages by default", async () => {
const getWatchResult = await fixture.lernaWatch('-- "echo watch triggered"');

await createFile(fixture.getWorkspacePath("packages/package-a/my-file.txt"));
await wait(200);

await createFile(fixture.getWorkspacePath("packages/package-b/my-file.txt"));
await wait(200);

await createFile(fixture.getWorkspacePath("packages/package-c/my-file.txt"));
await wait(200);

const output = await getWatchResult();

expect(output.combinedOutput).toMatchInlineSnapshot(`
lerna notice cli v999.9.9-e2e.0
lerna verb rootPath /tmp/lerna-e2e/lerna-watch/lerna-workspace
lerna info watch Executing command "echo watch triggered" on changes in 3 packages.
> NX running with args: {"command":"echo watch triggered","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-a","package-b","package-c"],"verbose":true}
> NX starting watch process
> NX watch process waiting...
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"","LERNA_FILE_CHANGES":"packages/package-a/my-file.txt"}]
watch triggered
> NX running complete, processing the next batch
> NX no more commands to process
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"","LERNA_FILE_CHANGES":"packages/package-b/my-file.txt"}]
watch triggered
> NX running complete, processing the next batch
> NX no more commands to process
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"","LERNA_FILE_CHANGES":"packages/package-c/my-file.txt"}]
watch triggered
> NX running complete, processing the next batch
> NX no more commands to process
`);
});

describe("with --scope", () => {
it("should only watch only specified packages", async () => {
const getWatchResult = await fixture.lernaWatch(
'--scope="package-a" --scope="package-c" -- "echo watch triggered"'
);

await createFile(fixture.getWorkspacePath("packages/package-a/my-file.txt"));
await wait(200);

await createFile(fixture.getWorkspacePath("packages/package-b/my-file.txt"));
await wait(200);

await createFile(fixture.getWorkspacePath("packages/package-c/my-file.txt"));
await wait(200);

const output = await getWatchResult();

expect(output.combinedOutput).toMatchInlineSnapshot(`
lerna notice cli v999.9.9-e2e.0
lerna verb rootPath /tmp/lerna-e2e/lerna-watch/lerna-workspace
lerna notice filter including ["package-a","package-c"]
lerna info filter [ 'package-a', 'package-c' ]
lerna info watch Executing command "echo watch triggered" on changes in 2 packages.
> NX running with args: {"command":"echo watch triggered","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-a","package-c"],"verbose":true}
> NX starting watch process
> NX watch process waiting...
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"","LERNA_FILE_CHANGES":"packages/package-a/my-file.txt"}]
watch triggered
> NX running complete, processing the next batch
> NX no more commands to process
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"","LERNA_FILE_CHANGES":"packages/package-c/my-file.txt"}]
watch triggered
> NX running complete, processing the next batch
> NX no more commands to process
`);
});
describe("and --include-dependencies", () => {
it("should watch one package and its dependencies", async () => {
const getWatchResult = await fixture.lernaWatch(
'--scope="package-b" --include-dependencies -- "echo watch triggered"'
);

await createFile(fixture.getWorkspacePath("packages/package-a/my-file.txt"));
await wait(200);

await createFile(fixture.getWorkspacePath("packages/package-b/my-file.txt"));
await wait(200);

await createFile(fixture.getWorkspacePath("packages/package-c/my-file.txt"));
await wait(200);

const output = await getWatchResult();

expect(output.combinedOutput).toMatchInlineSnapshot(`
lerna notice cli v999.9.9-e2e.0
lerna verb rootPath /tmp/lerna-e2e/lerna-watch/lerna-workspace
lerna notice filter including "package-b"
lerna notice filter including dependencies
lerna info filter [ 'package-b' ]
lerna info watch Executing command "echo watch triggered" on changes in 2 packages.
> NX running with args: {"command":"echo watch triggered","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-b","package-a"],"verbose":true}
> NX starting watch process
> NX watch process waiting...
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"","LERNA_FILE_CHANGES":"packages/package-a/my-file.txt"}]
watch triggered
> NX running complete, processing the next batch
> NX no more commands to process
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"","LERNA_FILE_CHANGES":"packages/package-b/my-file.txt"}]
watch triggered
> NX running complete, processing the next batch
> NX no more commands to process
`);
});
});
});

it("should replace package name and changed file names", async () => {
fixture.updateJson("lerna.json", (json) => ({
...json,
command: {
watch: {
// This workaround is necessary to prevent $LERNA_PACKAGE_NAME and $LERNA_FILE_CHANGES
// from being replaced by `child_process.spawn`. This is only needed for the e2e tests.
// This test case can be reproduced manually by running:
// `npx lerna watch -- echo $LERNA_PACKAGE_NAME: $LERNA_FILE_CHANGES`
command: "echo $LERNA_PACKAGE_NAME: $LERNA_FILE_CHANGES",
},
},
}));
const getWatchResult = await fixture.lernaWatch("");

await createFile(fixture.getWorkspacePath("packages/package-a/my-file.txt"));
await wait(200);

await createFile(fixture.getWorkspacePath("packages/package-b/my-file.txt"));
await wait(200);

await createFile(fixture.getWorkspacePath("packages/package-c/my-file.txt"));
await wait(200);

const output = await getWatchResult();

expect(output.combinedOutput).toMatchInlineSnapshot(`
lerna notice cli v999.9.9-e2e.0
lerna verb rootPath /tmp/lerna-e2e/lerna-watch/lerna-workspace
lerna info watch Executing command "echo $LERNA_PACKAGE_NAME: $LERNA_FILE_CHANGES" on changes in 3 packages.
> NX running with args: {"command":"echo $LERNA_PACKAGE_NAME: $LERNA_FILE_CHANGES","projectNameEnvName":"LERNA_PACKAGE_NAME","fileChangesEnvName":"LERNA_FILE_CHANGES","includeDependentProjects":false,"projects":["package-a","package-b","package-c"],"verbose":true}
> NX starting watch process
> NX watch process waiting...
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"package-a","LERNA_FILE_CHANGES":"packages/package-a/my-file.txt"}]
package-a: packages/package-a/my-file.txt
> NX running complete, processing the next batch
> NX no more commands to process
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"package-b","LERNA_FILE_CHANGES":"packages/package-b/my-file.txt"}]
package-b: packages/package-b/my-file.txt
> NX running complete, processing the next batch
> NX no more commands to process
> NX about to run commands with these environments: [{"LERNA_PACKAGE_NAME":"package-c","LERNA_FILE_CHANGES":"packages/package-c/my-file.txt"}]
package-c: packages/package-c/my-file.txt
> NX running complete, processing the next batch
> NX no more commands to process
`);
});
});
16 changes: 16 additions & 0 deletions e2e/watch/tsconfig.json
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}

0 comments on commit 16a4df9

Please sign in to comment.