|
1 | 1 | import { readFileSync, readdirSync } from 'node:fs'; |
2 | 2 | import path from 'node:path'; |
| 3 | +import * as ts from 'typescript'; |
3 | 4 |
|
4 | 5 | const TEST_FILE_PATTERN = /\.(test|spec)\.[jt]sx?$/; |
5 | | -const MOCK_MODULE_PATTERN = /\bmock\.module\s*\(/; |
6 | 6 |
|
7 | 7 | export interface FrontendTestRun { |
8 | 8 | label: string; |
@@ -36,8 +36,49 @@ export function toPosixRelative(filePath: string, cwd = process.cwd()): string { |
36 | 36 | return path.relative(cwd, filePath).split(path.sep).join(path.posix.sep); |
37 | 37 | } |
38 | 38 |
|
| 39 | +function getScriptKind(filePath: string): ts.ScriptKind { |
| 40 | + switch (path.extname(filePath).toLowerCase()) { |
| 41 | + case '.tsx': |
| 42 | + return ts.ScriptKind.TSX; |
| 43 | + case '.jsx': |
| 44 | + return ts.ScriptKind.JSX; |
| 45 | + case '.js': |
| 46 | + return ts.ScriptKind.JS; |
| 47 | + default: |
| 48 | + return ts.ScriptKind.TS; |
| 49 | + } |
| 50 | +} |
| 51 | + |
39 | 52 | export function usesMockModule(filePath: string): boolean { |
40 | | - return MOCK_MODULE_PATTERN.test(readFileSync(filePath, 'utf8')); |
| 53 | + const sourceFile = ts.createSourceFile( |
| 54 | + filePath, |
| 55 | + readFileSync(filePath, 'utf8'), |
| 56 | + ts.ScriptTarget.Latest, |
| 57 | + false, |
| 58 | + getScriptKind(filePath), |
| 59 | + ); |
| 60 | + |
| 61 | + let found = false; |
| 62 | + |
| 63 | + const visit = (node: ts.Node): void => { |
| 64 | + if (found) return; |
| 65 | + |
| 66 | + if ( |
| 67 | + ts.isCallExpression(node) && |
| 68 | + ts.isPropertyAccessExpression(node.expression) && |
| 69 | + ts.isIdentifier(node.expression.expression) && |
| 70 | + node.expression.expression.text === 'mock' && |
| 71 | + node.expression.name.text === 'module' |
| 72 | + ) { |
| 73 | + found = true; |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + ts.forEachChild(node, visit); |
| 78 | + }; |
| 79 | + |
| 80 | + visit(sourceFile); |
| 81 | + return found; |
41 | 82 | } |
42 | 83 |
|
43 | 84 | export function planFrontendTestRuns( |
|
0 commit comments