|
1 | 1 | import { Config } from '@jest/types'; |
| 2 | +import path from 'path'; |
| 3 | +import createRequire from 'create-require'; |
2 | 4 |
|
3 | 5 | export type JestConfigOptions = Partial<Config.InitialOptions>; |
4 | 6 |
|
5 | 7 | export function createJestConfig( |
6 | 8 | _: (relativePath: string) => void, |
7 | 9 | rootDir: string |
8 | 10 | ): JestConfigOptions { |
| 11 | + function resolveRelativeTo(file: string, moduleSpecifier: string) { |
| 12 | + const req = createRequire(file); |
| 13 | + try { |
| 14 | + return req.resolve(moduleSpecifier); |
| 15 | + } catch { |
| 16 | + return null; |
| 17 | + } |
| 18 | + } |
| 19 | + function resolveBabelJest() { |
| 20 | + const jestLocation = |
| 21 | + resolveRelativeTo(path.join(rootDir, 'file.js'), 'jest') || |
| 22 | + require.resolve('jest'); |
| 23 | + const jestCoreLocation = resolveRelativeTo(jestLocation, '@jest/core')!; |
| 24 | + const jestConfigLocation = resolveRelativeTo( |
| 25 | + jestCoreLocation, |
| 26 | + 'jest-config' |
| 27 | + )!; |
| 28 | + return ( |
| 29 | + resolveRelativeTo(path.join(rootDir, 'file.js'), 'babel-jest') || |
| 30 | + resolveRelativeTo(jestConfigLocation, 'babel-jest')! |
| 31 | + ); |
| 32 | + } |
9 | 33 | const config: JestConfigOptions = { |
10 | 34 | transform: { |
11 | 35 | '.(ts|tsx)$': require.resolve('ts-jest/dist'), |
12 | | - '.(js|jsx)$': require.resolve('babel-jest'), // jest's default |
| 36 | + '.(js|jsx)$': resolveBabelJest(), // jest's default |
13 | 37 | }, |
14 | 38 | transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'], |
15 | 39 | moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], |
|
0 commit comments