diff --git a/e2e/node/src/node.test.ts b/e2e/node/src/node.test.ts index e34416f235..81fa8d3520 100644 --- a/e2e/node/src/node.test.ts +++ b/e2e/node/src/node.test.ts @@ -217,6 +217,23 @@ module.exports = { } }, 60000); + it("should exclude 'test' target from e2e project that uses jest", async () => { + const appName = uniq('nodeapp'); + + runCLI( + `generate @nx/node:app ${appName} --project-name-and-root-format=as-provided --no-interactive` + ); + + const nxJson = JSON.parse(readFile('nx.json')); + expect(nxJson.plugins).toBeDefined(); + + const jestPlugin = nxJson.plugins.find( + (p) => p.plugin === '@nx/jest/plugin' + ); + expect(jestPlugin).toBeDefined(); + expect(jestPlugin.exclude).toContain(`${appName}-e2e/**/*`); + }); + it('should be able to generate an express application', async () => { const nodeapp = uniq('nodeapp'); const originalEnvPort = process.env.PORT; diff --git a/packages/node/src/generators/e2e-project/e2e-project.ts b/packages/node/src/generators/e2e-project/e2e-project.ts index b5b79420cd..29896ac177 100644 --- a/packages/node/src/generators/e2e-project/e2e-project.ts +++ b/packages/node/src/generators/e2e-project/e2e-project.ts @@ -11,6 +11,7 @@ import { readProjectConfiguration, runTasksInSerial, Tree, + updateJson, } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { Linter, lintProjectGenerator } from '@nx/eslint'; @@ -57,9 +58,37 @@ export async function e2eProjectGeneratorInternal( jestConfig: `${options.e2eProjectRoot}/jest.config.ts`, passWithNoTests: true, }, + dependsOn: [`${options.project}:build`], }, }, }); + // TODO(@nicholas): Find a better way to get build target + + // We remove the 'test' target from the e2e project because it is not needed + // The 'e2e' target is the one that should run the tests for the e2e project + const nxJson = readNxJson(host); + const hasPlugin = nxJson.plugins?.some((p) => { + if (typeof p !== 'string' && p.plugin === '@nx/jest/plugin') { + return true; + } + }); + + if (hasPlugin) { + updateJson(host, 'nx.json', (json) => { + return { + ...json, + plugins: json.plugins?.map((p) => { + if (typeof p !== 'string' && p.plugin === '@nx/jest/plugin') { + return { + ...p, + exclude: [...(p.exclude || []), `${options.e2eProjectRoot}/**/*`], + }; + } + return p; + }), + }; + }); + } if (options.projectType === 'server') { generateFiles(