Skip to content

Commit

Permalink
fix(testing): jest should handle root jest.preset.cjs (#21746)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Feb 12, 2024
1 parent 9663adc commit 45f229f
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 21 deletions.
59 changes: 59 additions & 0 deletions packages/jest/src/generators/configuration/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Tree,
updateProjectConfiguration,
writeJson,
updateJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { jestConfigObject } from '../../utils/config/functions';
Expand Down Expand Up @@ -386,4 +387,62 @@ describe('jestProject', () => {
`);
});
});

describe(`jest.preset.cjs`, () => {
it(`root jest.preset.cjs existing should force subsequent configs to point to it correctly`, async () => {
// ARRANGE
tree.write(
`jest.preset.cjs`,
`
const nxPreset = require('@nx/jest/preset').default;
module.exports = { ...nxPreset }`
);

// ACT
await configurationGenerator(tree, {
...defaultOptions,
project: 'lib1',
} as JestProjectSchema);

// ASSERT
expect(tree.read('libs/lib1/jest.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"/* eslint-disable */
export default {
displayName: 'lib1',
preset: '../../jest.preset.cjs',
coverageDirectory: '../../coverage/libs/lib1',
};
"
`);
});

it(`root package.json type=module should create jest.preset.cjs and force subsequent configs to point to it correctly`, async () => {
// ARRANGE
updateJson(tree, 'package.json', (pkgJson) => {
pkgJson.type = 'module';
return pkgJson;
});

// ACT
await configurationGenerator(tree, {
...defaultOptions,
project: 'lib1',
} as JestProjectSchema);

// ASSERT
expect(tree.exists('jest.preset.cjs')).toBeTruthy();
expect(tree.read('libs/lib1/jest.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"/* eslint-disable */
export default {
displayName: 'lib1',
preset: '../../jest.preset.cjs',
coverageDirectory: '../../coverage/libs/lib1',
};
"
`);
});
});
});
7 changes: 5 additions & 2 deletions packages/jest/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '@nx/devkit';
import { initGenerator as jsInitGenerator } from '@nx/js';
import { JestPluginOptions } from '../../plugins/plugin';
import { isPresetCjs } from '../../utils/config/is-preset-cjs';

const schemaDefaults = {
setupFile: 'none',
Expand Down Expand Up @@ -84,9 +85,11 @@ export async function configurationGeneratorInternal(
tasks.push(ensureDependencies(tree, options));
}

await createJestConfig(tree, options);
const presetExt = isPresetCjs(tree) ? 'cjs' : 'js';

await createJestConfig(tree, options, presetExt);
checkForTestTarget(tree, options);
createFiles(tree, options);
createFiles(tree, options, presetExt);
updateTsConfig(tree, options);
updateVsCodeRecommendedExtensions(tree);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
<% if(js){ %>module.exports =<% } else{ %>export default<% } %> {
displayName: '<%= project %>',
preset: '<%= offsetFromRoot %>jest.preset.js',
preset: '<%= offsetFromRoot %>jest.preset.<%= presetExt %>',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],<% if(testEnvironment) { %>
testEnvironment: '<%= testEnvironment %>',<% } %>
coverageDirectory: '<%= offsetFromRoot %>coverage/<%= projectRoot %>',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
<% if(js){ %>module.exports =<% } else{ %>export default<% } %> {
displayName: '<%= project %>',
preset: '<%= offsetFromRoot %>jest.preset.js',<% if(setupFile !== 'none') { %>
preset: '<%= offsetFromRoot %>jest.preset.<%= presetExt %>',<% if(setupFile !== 'none') { %>
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],<% } %><% if(testEnvironment) { %>
testEnvironment: '<%= testEnvironment %>',<% } %><% if(skipSerializers){ %>
transform: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
import { join } from 'path';
import { NormalizedJestProjectSchema } from '../schema';

export function createFiles(tree: Tree, options: NormalizedJestProjectSchema) {
export function createFiles(
tree: Tree,
options: NormalizedJestProjectSchema,
presetExt: 'cjs' | 'js'
) {
const projectConfig = readProjectConfiguration(tree, options.project);

const filesFolder =
Expand Down Expand Up @@ -42,6 +46,7 @@ export function createFiles(tree: Tree, options: NormalizedJestProjectSchema) {
rootProject: options.rootProject,
projectRoot: options.rootProject ? options.project : projectConfig.root,
offsetFromRoot: offsetFromRoot(projectConfig.root),
presetExt,
});

if (options.setupFile === 'none') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('createJestConfig', () => {
});

it('should generate files with --js flag', async () => {
await createJestConfig(tree, { js: true });
await createJestConfig(tree, { js: true }, 'js');

expect(tree.exists('jest.config.js')).toBeTruthy();
expect(
Expand All @@ -58,7 +58,7 @@ describe('createJestConfig', () => {
});

it('should generate files ', async () => {
await createJestConfig(tree, {});
await createJestConfig(tree, {}, 'js');

expect(tree.exists('jest.config.ts')).toBeTruthy();
expect(
Expand Down Expand Up @@ -92,21 +92,21 @@ export default {
`;
tree.write('jest.config.ts', expected);

await createJestConfig(tree, {});
await createJestConfig(tree, {}, 'js');

expect(tree.read('jest.config.ts', 'utf-8')).toEqual(expected);
});

it('should make js jest files', async () => {
await createJestConfig(tree, { js: true });
await createJestConfig(tree, { js: true }, 'js');

expect(tree.exists('jest.config.js')).toBeTruthy();
expect(tree.exists('jest.preset.js')).toBeTruthy();
});

describe('root project', () => {
it('should not add a monorepo jest.config.ts to the project', async () => {
await createJestConfig(tree, { rootProject: true });
await createJestConfig(tree, { rootProject: true }, 'js');

expect(tree.exists('jest.config.ts')).toBeFalsy();
});
Expand Down Expand Up @@ -143,7 +143,7 @@ export default {
`
);

await createJestConfig(tree, { rootProject: false });
await createJestConfig(tree, { rootProject: false }, 'js');

expect(tree.exists('jest.config.app.ts')).toBeTruthy();
expect(tree.read('jest.config.app.ts', 'utf-8')).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -210,7 +210,7 @@ module.exports = {
`
);

await createJestConfig(tree, { js: true, rootProject: false });
await createJestConfig(tree, { js: true, rootProject: false }, 'js');

expect(tree.exists('jest.config.app.js')).toBeTruthy();
expect(tree.read('jest.config.js', 'utf-8'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import type { NormalizedJestProjectSchema } from '../schema';

export async function createJestConfig(
tree: Tree,
options: Partial<NormalizedJestProjectSchema>
options: Partial<NormalizedJestProjectSchema>,
presetExt: 'cjs' | 'js'
) {
if (!tree.exists('jest.preset.js')) {
if (!tree.exists(`jest.preset.${presetExt}`)) {
// preset is always js file.
tree.write(
`jest.preset.js`,
`jest.preset.${presetExt}`,
`
const nxPreset = require('@nx/jest/preset').default;
Expand Down
15 changes: 9 additions & 6 deletions packages/jest/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { updatePackageScripts } from '@nx/devkit/src/utils/update-package-scripts';
import { createNodes } from '../../plugins/plugin';
import { jestVersion, nxVersion } from '../../utils/versions';
import { isPresetCjs } from '../../utils/config/is-preset-cjs';
import type { JestInitSchema } from './schema';

function addPlugin(tree: Tree) {
Expand All @@ -34,7 +35,7 @@ function addPlugin(tree: Tree) {
updateNxJson(tree, nxJson);
}

function updateProductionFileSet(tree: Tree) {
function updateProductionFileSet(tree: Tree, presetExt: 'cjs' | 'js') {
const nxJson = readNxJson(tree);

const productionFileSet = nxJson.namedInputs?.production;
Expand All @@ -59,7 +60,7 @@ function updateProductionFileSet(tree: Tree) {
updateNxJson(tree, nxJson);
}

function addJestTargetDefaults(tree: Tree) {
function addJestTargetDefaults(tree: Tree, presetEnv: 'cjs' | 'js') {
const nxJson = readNxJson(tree);

nxJson.targetDefaults ??= {};
Expand All @@ -72,7 +73,7 @@ function addJestTargetDefaults(tree: Tree) {
nxJson.targetDefaults['@nx/jest:jest'].inputs ??= [
'default',
productionFileSet ? '^production' : '^default',
'{workspaceRoot}/jest.preset.js',
`{workspaceRoot}/jest.preset.${presetEnv}`,
];

nxJson.targetDefaults['@nx/jest:jest'].options ??= {
Expand Down Expand Up @@ -111,12 +112,14 @@ export async function jestInitGeneratorInternal(
): Promise<GeneratorCallback> {
options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false';

if (!tree.exists('jest.preset.js')) {
updateProductionFileSet(tree);
const presetExt = isPresetCjs(tree) ? 'cjs' : 'js';

if (!tree.exists('jest.preset.js') && !tree.exists('jest.preset.cjs')) {
updateProductionFileSet(tree, presetExt);
if (options.addPlugin) {
addPlugin(tree);
} else {
addJestTargetDefaults(tree);
addJestTargetDefaults(tree, presetExt);
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/jest/src/utils/config/find-root-jest-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ export function findRootJestPreset(tree: Tree): string | null {
return 'jest.preset.js';
}

if (tree.exists('jest.preset.cjs')) {
return 'jest.preset.cjs';
}

return null;
}
14 changes: 14 additions & 0 deletions packages/jest/src/utils/config/is-preset-cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type Tree, readJson } from '@nx/devkit';

export function isPresetCjs(tree: Tree) {
if (tree.exists('jest.preset.cjs')) {
return true;
}

const rootPkgJson = readJson(tree, 'package.json');
if (rootPkgJson.type && rootPkgJson.type === 'module') {
return true;
}

return false;
}

0 comments on commit 45f229f

Please sign in to comment.