Skip to content

Commit

Permalink
feat(testing): add getJestProjectsAsync to support inferred targets
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Feb 23, 2024
1 parent 21e7648 commit 93f6dee
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 43 deletions.
1 change: 1 addition & 0 deletions packages/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export { jestConfigObjectAst } from './src/utils/config/functions';
export { jestInitGenerator } from './src/generators/init/init';
export {
getJestProjects,
getJestProjectsAsync,
getNestedJestProjects,
} from './src/utils/config/get-jest-projects';
5 changes: 3 additions & 2 deletions packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"dependencies": {
"@jest/reporters": "^29.4.1",
"@jest/test-result": "^29.4.1",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js",
"@phenomnomnominal/tsquery": "~5.0.1",
"chalk": "^4.1.0",
"identity-obj-proxy": "3.0.0",
Expand All @@ -45,8 +47,7 @@
"minimatch": "9.0.3",
"resolve.exports": "1.1.0",
"tslib": "^2.3.0",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js"
"yargs-parser": "21.1.1"
},
"publishConfig": {
"access": "public"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createJestConfig should generate files 1`] = `
"import { getJestProjects } from '@nx/jest';
"import { getJestProjectsAsync } from '@nx/jest';
export default {
projects: getJestProjects()
};"
export default async () => ({
projects: await getJestProjectsAsync()
});"
`;

exports[`createJestConfig should generate files 2`] = `
Expand All @@ -15,11 +15,11 @@ module.exports = { ...nxPreset }"
`;

exports[`createJestConfig should generate files with --js flag 1`] = `
"const { getJestProjects } = require('@nx/jest');
"const { getJestProjectsAsync } = require('@nx/jest');
module.exports = {
projects: getJestProjects()
};"
module.exports = async () => ({
projects: await getJestProjectsAsync()
});"
`;

exports[`createJestConfig should generate files with --js flag 2`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ export default {
"
`);
expect(tree.read('jest.config.ts', 'utf-8'))
.toEqual(`import { getJestProjects } from '@nx/jest';
.toEqual(`import { getJestProjectsAsync } from '@nx/jest';
export default {
projects: getJestProjects()
};`);
export default async () => ({
projects: await getJestProjectsAsync()
});`);
expect(readProjectConfiguration(tree, 'my-project').targets.test)
.toMatchInlineSnapshot(`
{
Expand Down Expand Up @@ -214,11 +214,11 @@ module.exports = {

expect(tree.exists('jest.config.app.js')).toBeTruthy();
expect(tree.read('jest.config.js', 'utf-8'))
.toEqual(`const { getJestProjects } = require('@nx/jest');
.toEqual(`const { getJestProjectsAsync } = require('@nx/jest');
module.exports = {
projects: getJestProjects()
};`);
module.exports = async () => ({
projects: await getJestProjectsAsync()
});`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ export async function createJestConfig(
function generateGlobalConfig(tree: Tree, isJS: boolean) {
const contents = isJS
? stripIndents`
const { getJestProjects } = require('@nx/jest');
const { getJestProjectsAsync } = require('@nx/jest');
module.exports = {
projects: getJestProjects()
};`
module.exports = async () => ({
projects: await getJestProjectsAsync()
});`
: stripIndents`
import { getJestProjects } from '@nx/jest';
import { getJestProjectsAsync } from '@nx/jest';
export default {
projects: getJestProjects()
};`;
export default async () => ({
projects: await getJestProjectsAsync()
});`;
tree.write(`jest.config.${isJS ? 'js' : 'ts'}`, contents);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import { readProjectConfiguration, Tree } from '@nx/devkit';

function isUsingUtilityFunction(host: Tree) {
const rootConfig = findRootJestConfig(host);
if (!rootConfig) {
return false;
}

const rootConfigContent = host.read(rootConfig, 'utf-8');

return (
rootConfig && host.read(rootConfig).toString().includes('getJestProjects()')
rootConfigContent.includes('getJestProjects()') ||
rootConfigContent.includes('getJestProjectsAsync()')
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('@nx/jest/plugin', () => {

beforeEach(async () => {
tempFs = new TempFs('test');
process.chdir(tempFs.tempDir);
context = {
nxJsonConfiguration: {
namedInputs: {
Expand Down
13 changes: 12 additions & 1 deletion packages/jest/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { dirname, join, relative, resolve } from 'path';

import { readTargetDefaultsForTarget } from 'nx/src/project-graph/utils/project-configuration-utils';
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readdirSync } from 'fs';
import { existsSync, readdirSync, readFileSync } from 'fs';
import { readConfig } from 'jest-config';
import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory';
import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes';
Expand Down Expand Up @@ -82,6 +82,17 @@ export const createNodes: CreateNodes<JestPluginOptions> = [
}
}

const jestConfigContent = readFileSync(
resolve(context.workspaceRoot, configFilePath),
'utf-8'
);
if (jestConfigContent.includes('getJestProjectsAsync()')) {
// The `getJestProjectsAsync` function uses the project graph, which leads to a
// circular dependency. We can skip this since it's no intended to be used for
// an Nx project.
return {};
}

options = normalizeOptions(options);

const hash = calculateHashForCreateNodes(projectRoot, options, context);
Expand Down
Loading

0 comments on commit 93f6dee

Please sign in to comment.