/
fakers.ts
94 lines (82 loc) · 2.39 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 { resolve } from 'path'
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 transpiledTsSource() {
return `
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var upper_1 = __importDefault(require("./upper"));
var lower_1 = __importDefault(require("./lower"));
jest.mock('./upper', function () { return function (s) { return s.toUpperCase(); }; });
describe('hello', function () {
test('my test', function () {
expect(upper_1.default('hello')).toBe('HELLO');
expect(lower_1.default('HELLO')).toBe('hello');
jest.mock('./lower', function () { return function (s) { return s.toLowerCase(); }; });
});
});
`
}
export function htmlSource() {
return `
<div>
<span>some text with \`backtick\`</span>
</div>
`
}
export function typescriptSource() {
return `
import upper from './upper';
import lower from './lower';
jest.mock('./upper', () => (s) => s.toUpperCase());
describe('hello', () => {
test('my test', () => {
expect(upper('hello')).toBe('HELLO');
expect(lower('HELLO')).toBe('hello');
jest.mock('./lower', () => (s) => s.toLowerCase());
});
});
`
}
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 jestConfig<T extends jest.ProjectConfig>(
options?: Partial<jest.InitialOptions | jest.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
}