Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testing): add getJestProjectsAsync to support inferred targets #21897

Merged
merged 2 commits into from Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/jest/index.ts
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
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
@@ -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
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()
});`);
});
});
});
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);
}
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
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
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