From 6f09598dae118d3e02c129cad29fd0a900311e33 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Fri, 31 May 2024 16:07:07 -0400 Subject: [PATCH] chore(misc): review feedback --- packages/jest/src/plugins/plugin.spec.ts | 3 --- .../nest/src/generators/filter/filter.spec.ts | 4 ++-- packages/vite/src/plugins/plugin.spec.ts | 2 -- scripts/unit-test-setup.js | 22 ++++++++++++++++++- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/jest/src/plugins/plugin.spec.ts b/packages/jest/src/plugins/plugin.spec.ts index deb05e3817085c..f2e4a177a1cc3a 100644 --- a/packages/jest/src/plugins/plugin.spec.ts +++ b/packages/jest/src/plugins/plugin.spec.ts @@ -3,7 +3,6 @@ import { join } from 'path'; import { createNodes } from './plugin'; import { TempFs } from 'nx/src/internal-testing-utils/temp-fs'; -import { setWorkspaceRoot } from 'nx/src/utils/workspace-root'; describe('@nx/jest/plugin', () => { let createNodesFunction = createNodes[1]; @@ -26,8 +25,6 @@ describe('@nx/jest/plugin', () => { configFiles: [], }; - setWorkspaceRoot(tempFs.tempDir); - await tempFs.createFiles({ 'proj/jest.config.js': `module.exports = {}`, 'proj/src/unit.spec.ts': '', diff --git a/packages/nest/src/generators/filter/filter.spec.ts b/packages/nest/src/generators/filter/filter.spec.ts index 44b2fec0d9242c..fff38d07e21540 100644 --- a/packages/nest/src/generators/filter/filter.spec.ts +++ b/packages/nest/src/generators/filter/filter.spec.ts @@ -14,10 +14,10 @@ describe('filter generator', () => { beforeEach(() => { tree = createTreeWithNestApplication(project); + jest.clearAllMocks(); }); it('should run successfully', async () => { - await filterGenerator(tree, options); - expect(true).toBe(true); + await expect(filterGenerator(tree, options)).resolves.not.toThrowError(); }); }); diff --git a/packages/vite/src/plugins/plugin.spec.ts b/packages/vite/src/plugins/plugin.spec.ts index a3ef77ec8cd99b..e7c99cf8b1fb0d 100644 --- a/packages/vite/src/plugins/plugin.spec.ts +++ b/packages/vite/src/plugins/plugin.spec.ts @@ -1,7 +1,6 @@ import { CreateNodesContext } from '@nx/devkit'; import { createNodes } from './plugin'; import { TempFs } from 'nx/src/internal-testing-utils/temp-fs'; -import { setWorkspaceRoot } from 'nx/src/utils/workspace-root'; jest.mock('vite', () => ({ resolveConfig: jest.fn().mockImplementation(() => { @@ -47,7 +46,6 @@ describe('@nx/vite/plugin', () => { }, workspaceRoot: tempFs.tempDir, }; - setWorkspaceRoot(tempFs.tempDir); tempFs.createFileSync('index.html', ''); tempFs.createFileSync('package.json', ''); }); diff --git a/scripts/unit-test-setup.js b/scripts/unit-test-setup.js index c1d106ae767160..8436818b05311c 100644 --- a/scripts/unit-test-setup.js +++ b/scripts/unit-test-setup.js @@ -1,7 +1,27 @@ module.exports = () => { + /** + * When the daemon is enabled during unit tests, + * and the daemon is already running, the daemon-client.ts + * code will be used, but it will hit the already running + * daemon which is from the installed version of Nx. + * + * In the vast majority of cases, this is fine. However, + * if a new message type has been added to the daemon in + * the source code, and isn't yet in the installed version, + * any test that hits that codepath will fail. This is because + * the installed version of the daemon doesn't know how to + * handle the new message type. + * + * To prevent this, we disable the daemon during unit tests. + */ process.env.NX_DAEMON = 'false'; - // Prevents tests from relying on the Nx repo project graph + /** + * When `createProjectGraphAsync` is called during tests, + * if its not mocked, it will return the Nx repo's project + * graph. We don't want any unit tests to depend on the structure + * of the Nx repo, so we mock it to return an empty project graph. + */ jest.doMock('@nx/devkit', () => ({ ...jest.requireActual('@nx/devkit'), createProjectGraphAsync: jest.fn().mockImplementation(async () => {