/
fakers.ts
94 lines (82 loc) · 2.74 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
82
83
84
85
86
87
88
89
90
91
92
93
94
import type { Config } from '@jest/types'
import type { Logger } from 'bs-logger'
import { resolve } from 'path'
import { createCompilerInstance } from '../compiler/instance'
import { ConfigSet } from '../config/config-set'
import type { BabelConfig, TsCompiler, TsJestGlobalOptions } from '../types'
import type { ImportReasons } from '../utils/messages'
export function filePath(relPath: string): string {
return resolve(__dirname, '..', '..', relPath)
}
export const rootDir = filePath('')
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
}
export const defaultResolve = (path: string): string => `resolved:${path}`
export function createConfigSet({
jestConfig,
tsJestConfig,
logger, // don't change this key name, otherwise mock logging won't work
resolve = defaultResolve,
...others
}: {
jestConfig?: Partial<Config.ProjectConfig>
tsJestConfig?: TsJestGlobalOptions
logger?: Logger
resolve?: ((path: string) => string) | null
[key: string]: any
} = {}): ConfigSet {
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), logger)
if (resolve) {
cs.resolvePath = resolve
}
Object.keys(others).forEach((key) => {
Object.defineProperty(cs, key, { value: others[key] })
})
return cs
}
// 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
} = {}): TsCompiler {
tsJestConfig = { ...tsJestConfig }
tsJestConfig.diagnostics = {
...(tsJestConfig.diagnostics as any),
pretty: false,
}
const defaultTestRegex = ['(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.[jt]sx?$']
const defaultTestMatch = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)']
jestConfig = {
...jestConfig,
testMatch: jestConfig?.testMatch ? [...jestConfig.testMatch, ...defaultTestMatch] : defaultTestMatch,
testRegex: jestConfig?.testRegex ? [...defaultTestRegex, ...jestConfig.testRegex] : defaultTestRegex,
}
const cs = createConfigSet({ jestConfig, tsJestConfig, parentConfig, resolve: null })
return createCompilerInstance(cs)
}