From fa72908440f55c9a813af20f2b4e70286068c14c Mon Sep 17 00:00:00 2001 From: Austin Fahsl Date: Tue, 3 Jan 2023 11:46:46 -0700 Subject: [PATCH] chore(watch): add e2e tests for projects in a nx workspace --- e2e/watch/src/watch-with-nx.spec.ts | 122 ++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 e2e/watch/src/watch-with-nx.spec.ts diff --git a/e2e/watch/src/watch-with-nx.spec.ts b/e2e/watch/src/watch-with-nx.spec.ts new file mode 100644 index 00000000000..59b57c82559 --- /dev/null +++ b/e2e/watch/src/watch-with-nx.spec.ts @@ -0,0 +1,122 @@ +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-with-nx", () => { + let fixture: Fixture; + + beforeEach(async () => { + fixture = await Fixture.create({ + e2eRoot: process.env.E2E_ROOT, + name: "lerna-watch-with-nx", + 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.updateJson("packages/package-b/package.json", (json) => ({ + ...json, + name: "@unknown-scope/package-b", + })); + await fixture.lerna("create package-c -y"); + await fixture.updateJson("packages/package-c/package.json", (json) => ({ + ...json, + name: "@scope/package-c", + })); + + await fixture.addNxJsonToWorkspace(); + await fixture.updateJson("nx.json", (json) => ({ + ...json, + npmScope: "scope", + })); + + await fixture.createInitialGitCommit(); + }); + + afterAll(() => fixture.destroy()); + + it("should watch all packages by default, removing nx.json's 'npmScope' from package 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(); + + // package-c will be printed without its scope since Nx automatically removes it when substituting $LERNA_PACKAGE_NAME + expect(output.combinedOutput).toMatchInlineSnapshot(` + lerna notice cli v999.9.9-e2e.0 + lerna verb rootPath /tmp/lerna-e2e/lerna-watch-with-nx/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","@unknown-scope/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":"@unknown-scope/package-b","LERNA_FILE_CHANGES":"packages/package-b/my-file.txt"}] + + @unknown-scope/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 + + + `); + }); +});