|
1 | 1 | // @ts-check |
| 2 | +import { pathsToModuleNameMapper } from 'ts-jest'; |
| 3 | +import { getTsconfig } from 'get-tsconfig'; |
| 4 | +import { getJestCachePath } from '../../cache.config.js'; |
2 | 5 |
|
3 | | -const { defaults: tsPreset } = require('ts-jest/presets'); |
4 | | -const { pathsToModuleNameMapper } = require('ts-jest'); |
| 6 | +const tsConfigFile = new URL('./tsconfig.jest.json', import.meta.url).pathname; |
5 | 7 |
|
6 | | -const { |
7 | | - compilerOptions: { paths: tsConfigPaths }, |
8 | | -} = require('../../tsconfig.paths.json'); |
9 | | - |
10 | | -// Take the paths from tsconfig automatically from base tsconfig.json |
11 | | -// @link https://kulshekhar.github.io/ts-jest/docs/paths-mapping |
12 | | -const getTsConfigBasePaths = () => { |
13 | | - return tsConfigPaths |
14 | | - ? pathsToModuleNameMapper(tsConfigPaths, { |
15 | | - prefix: '<rootDir>/tricky-false-path-to-remove-parent/packages', |
| 8 | +/** |
| 9 | + * Transform the tsconfig paths into jest compatible one (support extends) |
| 10 | + * @param {string} tsConfigFile |
| 11 | + */ |
| 12 | +const getTsConfigBasePaths = (tsConfigFile) => { |
| 13 | + const parsedTsConfig = getTsconfig(tsConfigFile); |
| 14 | + if (parsedTsConfig === null) { |
| 15 | + throw new Error(`Cannot find tsconfig file: ${tsConfigFile}`); |
| 16 | + } |
| 17 | + const tsPaths = parsedTsConfig.config.compilerOptions?.paths; |
| 18 | + return tsPaths |
| 19 | + ? pathsToModuleNameMapper(tsPaths, { |
| 20 | + prefix: '<rootDir>/', |
16 | 21 | }) |
17 | 22 | : {}; |
18 | 23 | }; |
19 | 24 |
|
20 | | -/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ |
| 25 | +/** @type {import('ts-jest/dist').InitialOptionsTsJest} */ |
21 | 26 | const config = { |
22 | | - displayName: 'cache-redis/unit', |
23 | | - testRunner: 'jest-circus/runner', |
| 27 | + displayName: `cache-interop:unit`, |
| 28 | + preset: 'ts-jest/presets/default-esm', |
| 29 | + cacheDirectory: getJestCachePath('@soluble/cache-interop'), |
24 | 30 | testEnvironment: 'node', |
| 31 | + extensionsToTreatAsEsm: ['.ts'], |
25 | 32 | verbose: true, |
26 | | - transform: { |
27 | | - ...tsPreset.transform, |
28 | | - }, |
29 | | - rootDir: '../', |
30 | | - |
31 | | - testMatch: ['<rootDir>/cache-redis/src/**/*.test.ts'], |
| 33 | + rootDir: './src', |
| 34 | + testMatch: ['<rootDir>/**/*.{spec,test}.{js,jsx,ts,tsx}'], |
32 | 35 | moduleNameMapper: { |
33 | | - ...getTsConfigBasePaths(), |
| 36 | + ...getTsConfigBasePaths(tsConfigFile), |
34 | 37 | }, |
35 | | - coverageDirectory: '<rootDir>/cache-redis/coverage', |
| 38 | + // false by default, overrides in cli, ie: yarn test:unit --collect-coverage=true |
| 39 | + collectCoverage: false, |
| 40 | + coverageDirectory: '<rootDir>/../coverage', |
36 | 41 | collectCoverageFrom: [ |
37 | | - '<rootDir>/cache-redis/src/**/*.{ts,js}', |
38 | | - '!**/*.test.ts', |
39 | | - '!**/index.ts', |
40 | | - '!**/*.d.ts', |
| 42 | + '<rootDir>/**/*.{ts,tsx,js,jsx}', |
| 43 | + '!**/*.test.{js,ts}', |
| 44 | + '!**/__mock__/*', |
41 | 45 | ], |
42 | 46 | globals: { |
43 | 47 | 'ts-jest': { |
44 | | - diagnostics: true, |
45 | | - tsconfig: './tsconfig.jest.json', |
| 48 | + useESM: true, |
| 49 | + tsconfig: tsConfigFile, |
46 | 50 | }, |
47 | 51 | }, |
48 | 52 | }; |
49 | 53 |
|
50 | | -module.exports = config; |
| 54 | +export default config; |
0 commit comments