|
| 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 | +} |
0 commit comments