/
create-jest-preset.ts
37 lines (29 loc) · 1.04 KB
/
create-jest-preset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import * as TJestConfigPkg from 'jest-config'
import { rootLogger } from '../util/logger'
import { getJestConfigPkg } from './jest-config-resolver'
const logger = rootLogger.child({ namespace: 'jest-preset' })
const jestConfigPkg: typeof TJestConfigPkg = getJestConfigPkg(logger)
const defaults = jestConfigPkg.defaults
export interface TsJestPresets {
transform: Record<string, string>
testMatch: string[] | undefined
moduleFileExtensions: string[] | undefined
}
export interface CreateJestPresetOptions {
allowJs?: boolean
}
export function createJestPreset(
{ allowJs = false }: CreateJestPresetOptions = {},
from?: jest.InitialOptions,
): TsJestPresets {
logger.debug({ allowJs }, 'creating jest presets', allowJs ? 'handling' : 'not handling', 'JavaScript files')
from = { ...defaults, ...from }
return {
transform: {
...from.transform,
[allowJs ? '^.+\\.[tj]sx?$' : '^.+\\.tsx?$']: 'ts-jest',
},
testMatch: from.testMatch || undefined,
moduleFileExtensions: from.moduleFileExtensions || undefined,
}
}