-
Notifications
You must be signed in to change notification settings - Fork 450
/
fakers.ts
81 lines (72 loc) · 2.19 KB
/
fakers.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Config } from '@jest/types'
import { resolve } from 'path'
import { createCompilerInstance } from '../compiler/instance'
import { ConfigSet } from '../config/config-set'
import { BabelConfig, TsJestConfig, TsJestGlobalOptions } from '../types'
import { ImportReasons } from '../util/messages'
export function filePath(relPath: string): string {
return resolve(__dirname, '..', '..', relPath)
}
export const rootDir = filePath('')
export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
return {
isolatedModules: false,
compiler: 'typescript',
transformers: [],
babelConfig: undefined,
tsConfig: undefined,
packageJson: undefined,
stringifyContentPathRegex: undefined,
diagnostics: { ignoreCodes: [], pretty: false, throws: true },
...options,
}
}
export function getJestConfig<T extends Config.ProjectConfig>(
options?: Partial<Config.InitialOptions | Config.ProjectConfig>,
tsJestOptions?: TsJestGlobalOptions,
): T {
const res = {
globals: {},
...options,
} as any
if (tsJestOptions) {
res.globals['ts-jest'] = tsJestOptions
}
return res
}
export function babelConfig<T extends BabelConfig>(options?: BabelConfig): T {
return {
...options,
presets: [...(options && options.presets)],
plugins: [...(options && options.plugins)],
} as any
}
export function importReason(text = '[[BECAUSE]]'): ImportReasons {
return text as any
}
// not really unit-testing here, but it's hard to mock all those values :-D
export function makeCompiler({
jestConfig,
tsJestConfig,
parentConfig,
}: {
jestConfig?: Partial<Config.ProjectConfig>
tsJestConfig?: TsJestGlobalOptions
parentConfig?: TsJestGlobalOptions
} = {}) {
tsJestConfig = { ...tsJestConfig }
tsJestConfig.diagnostics = {
...(tsJestConfig.diagnostics as any),
pretty: false,
}
const testRegex = ['^.+\\.[tj]sx?$']
jestConfig = {
...jestConfig,
testMatch: ['^.+\\.tsx?$'],
testRegex: jestConfig?.testRegex ? [...testRegex, ...jestConfig.testRegex] : testRegex,
setupFiles: [],
setupFilesAfterEnv: [],
}
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), parentConfig)
return createCompilerInstance(cs)
}