Skip to content

Commit 10fb324

Browse files
authored
chore(testing): add deprecated comment for getJestProjects (#28178)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent a637f9e commit 10fb324

File tree

5 files changed

+169
-2
lines changed

5 files changed

+169
-2
lines changed

packages/jest/migrations.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
"version": "17.1.0-beta.2",
55
"description": "Move jest executor options to nx.json targetDefaults",
66
"implementation": "./src/migrations/update-17-1-0/move-options-to-target-defaults"
7+
},
8+
"replace-getJestProjects-with-getJestProjectsAsync": {
9+
"cli": "nx",
10+
"version": "20.0.0-beta.5",
11+
"description": "replace getJestProjects with getJestProjectsAsync",
12+
"implementation": "./src/migrations/update-20-0-0/replace-getJestProjects-with-getJestProjectsAsync"
713
}
814
},
915
"packageJsonUpdates": {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Tree } from '@nx/devkit';
2+
import { createTree } from '@nx/devkit/testing';
3+
import update from './replace-getJestProjects-with-getJestProjectsAsync';
4+
5+
describe('replace-getJestProjects-with-getJestProjectsAsync', () => {
6+
let tree: Tree;
7+
8+
beforeEach(() => {
9+
tree = createTree();
10+
});
11+
12+
it('should replace getJestProjects with getJestProjectsAsync', async () => {
13+
tree.write(
14+
'jest.config.ts',
15+
`
16+
const { getJestProjects } = require('@nx/jest');
17+
18+
module.exports = {
19+
projects: getJestProjects(),
20+
};
21+
`
22+
);
23+
await update(tree);
24+
const updatedJestConfig = tree.read('jest.config.ts')?.toString();
25+
expect(updatedJestConfig).toMatchInlineSnapshot(`
26+
"
27+
const { getJestProjectsAsync } = require('@nx/jest');
28+
29+
export default async () => ({
30+
projects: await getJestProjectsAsync(),
31+
});
32+
"
33+
`);
34+
});
35+
36+
it('should replace getJestProjects with getJestProjectsAsync with additonal properties', async () => {
37+
tree.write(
38+
'jest.config.ts',
39+
`
40+
const { getJestProjects } = require('@nx/jest');
41+
42+
module.exports = {
43+
projects: getJestProjects(),
44+
filename: __filename,
45+
env: process.env,
46+
dirname: __dirname
47+
};
48+
`
49+
);
50+
await update(tree);
51+
const updatedJestConfig = tree.read('jest.config.ts')?.toString();
52+
expect(updatedJestConfig).toMatchInlineSnapshot(`
53+
"
54+
const { getJestProjectsAsync } = require('@nx/jest');
55+
56+
export default async () => ({
57+
projects: await getJestProjectsAsync(),
58+
filename: __filename,
59+
env: process.env,
60+
dirname: __dirname
61+
});
62+
"
63+
`);
64+
});
65+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// go through the jest.config files
2+
// see if it imports from @nx/jest and if it uses getJestProjects
3+
// replace getJestProjects with getJestProjectsAsync
4+
5+
import { globAsync, Tree } from '@nx/devkit';
6+
import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript';
7+
import { BinaryExpression, ExpressionStatement } from 'typescript';
8+
9+
let tsModule: typeof import('typescript');
10+
11+
export default async function update(tree: Tree) {
12+
if (!tsModule) {
13+
tsModule = ensureTypescript();
14+
}
15+
16+
const jestConfigPaths = await globAsync(tree, [
17+
'**/jest.config.{cjs,mjs,js,cts,mts,ts}',
18+
]);
19+
jestConfigPaths.forEach((jestConfigPath) => {
20+
const oldContent = tree.read(jestConfigPath).toString();
21+
if (oldContent?.includes('projects: getJestProjects()')) {
22+
let sourceFile = tsModule.createSourceFile(
23+
jestConfigPath,
24+
oldContent,
25+
tsModule.ScriptTarget.Latest,
26+
true
27+
);
28+
29+
// find the import statement for @nx/jest
30+
const importStatement = sourceFile.statements.find(
31+
(statement) =>
32+
tsModule.isVariableStatement(statement) &&
33+
statement.declarationList.declarations.some(
34+
(declaration) =>
35+
tsModule.isCallExpression(declaration.initializer) &&
36+
tsModule.isIdentifier(declaration.initializer.expression) &&
37+
declaration.initializer.expression.escapedText === 'require' &&
38+
tsModule.isStringLiteral(declaration.initializer.arguments[0]) &&
39+
declaration.initializer.arguments[0].text === '@nx/jest'
40+
)
41+
);
42+
if (importStatement) {
43+
// find export statement with `projects: getJestProjects()`
44+
let exportStatement = sourceFile.statements.find(
45+
(statement) =>
46+
tsModule.isExpressionStatement(statement) &&
47+
tsModule.isBinaryExpression(statement.expression) &&
48+
tsModule.isPropertyAccessExpression(statement.expression.left) &&
49+
tsModule.isObjectLiteralExpression(statement.expression.right) &&
50+
statement.expression.operatorToken.kind ===
51+
tsModule.SyntaxKind.EqualsToken &&
52+
tsModule.isIdentifier(statement.expression.left.expression) &&
53+
statement.expression.left.expression.escapedText === 'module' &&
54+
tsModule.isIdentifier(statement.expression.left.name) &&
55+
statement.expression.left.name.escapedText === 'exports' &&
56+
statement.expression.right.properties.some(
57+
(property) =>
58+
tsModule.isPropertyAssignment(property) &&
59+
tsModule.isIdentifier(property.name) &&
60+
property.name.escapedText === 'projects' &&
61+
tsModule.isCallExpression(property.initializer) &&
62+
tsModule.isIdentifier(property.initializer.expression) &&
63+
property.initializer.expression.escapedText ===
64+
'getJestProjects'
65+
)
66+
) as ExpressionStatement;
67+
68+
if (exportStatement) {
69+
// replace getJestProjects with getJestProjectsAsync in export statement
70+
const rightExpression = (
71+
exportStatement.expression as BinaryExpression
72+
).right.getText();
73+
const newExpression = rightExpression.replace(
74+
'getJestProjects()',
75+
'await getJestProjectsAsync()'
76+
);
77+
const newStatement = `export default async () => (${newExpression});`;
78+
let newContent = oldContent.replace(
79+
exportStatement.getText(),
80+
newStatement
81+
);
82+
83+
// replace getJestProjects with getJestProjectsAsync in import statement
84+
newContent = newContent.replace(
85+
'getJestProjects',
86+
'getJestProjectsAsync'
87+
);
88+
89+
tree.write(jestConfigPath, newContent);
90+
}
91+
}
92+
}
93+
});
94+
}

packages/jest/src/utils/config/get-jest-projects.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ function getJestConfigProjectPath(projectJestConfigPath: string): string {
1212
}
1313

1414
/**
15+
* TODO(v21): Remove this function
16+
* @deprecated To get projects use {@link getJestProjectsAsync} instead. This will be removed in v21.
1517
* Get a list of paths to all the jest config files
1618
* using the Nx Jest executor.
1719
*

packages/nx/src/project-graph/file-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ function defaultReadFileAtRevision(
135135
}
136136

137137
/**
138-
* TODO(v20): Remove this function
139-
* @deprecated To get projects use {@link retrieveProjectConfigurations} instead. This will be removed in v20.
138+
* TODO(v21): Remove this function
139+
* @deprecated To get projects use {@link retrieveProjectConfigurations} instead. This will be removed in v21.
140140
*/
141141
export function readWorkspaceConfig(opts: {
142142
format: 'angularCli' | 'nx';

0 commit comments

Comments
 (0)