forked from jestjs/jest
/
index.ts
171 lines (151 loc) · 5.12 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type {
BabelFileResult,
TransformOptions as BabelTransformOptions,
} from '@babel/core';
import {makeProjectConfig} from '@jest/test-utils';
import type {SyncTransformer, TransformOptions} from '@jest/transform';
import babelJest, {createTransformer} from '../index';
import {loadPartialConfig} from '../loadBabelConfig';
jest.mock('../loadBabelConfig', () => {
const actual =
jest.requireActual<typeof import('@babel/core')>('@babel/core');
return {
loadPartialConfig: jest.fn<typeof actual.loadPartialConfig>((...args) =>
actual.loadPartialConfig(...args),
),
loadPartialConfigAsync: jest.fn<typeof actual.loadPartialConfigAsync>(
(...args) => actual.loadPartialConfigAsync(...args),
),
};
});
const defaultBabelJestTransformer =
babelJest.createTransformer() as SyncTransformer<BabelTransformOptions>;
//Mock data for all the tests
const sourceString = `
const sum = (a, b) => a+b;
const difference = (a, b) => a-b;
const customMultiply = (obj, mul) => {
const {a, ...rest} = obj;
return a * mul;
}
customMultiply({a: 32, dummy: "test"}, 2);
`;
beforeEach(() => {
jest.clearAllMocks();
});
test('Returns source string with inline maps when no transformOptions is passed', () => {
const result = defaultBabelJestTransformer.process(
sourceString,
'dummy_path.js',
{
cacheFS: new Map<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
} as TransformOptions<BabelTransformOptions>,
);
expect(typeof result).toBe('object');
expect(result.code).toBeDefined();
expect(result.map).toBeDefined();
expect(result.code).toMatch('//# sourceMappingURL');
expect(result.code).toMatch('customMultiply');
expect((result as BabelFileResult).map!.sources).toEqual(['dummy_path.js']);
expect(
JSON.stringify((result as BabelFileResult).map!.sourcesContent),
).toMatch('customMultiply');
});
test('Returns source string with inline maps when no transformOptions is passed async', async () => {
const result = await defaultBabelJestTransformer.processAsync!(
sourceString,
'dummy_path.js',
{
cacheFS: new Map<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
} as TransformOptions<BabelTransformOptions>,
);
expect(typeof result).toBe('object');
expect(result.code).toBeDefined();
expect(result.map).toBeDefined();
expect(result.code).toMatch('//# sourceMappingURL');
expect(result.code).toMatch('customMultiply');
const {map} = result;
expect(map).toBeTruthy();
expect(typeof map).not.toBe('string');
if (map == null || typeof map === 'string') {
throw new Error('dead code');
}
expect(map.sources).toEqual(['dummy_path.js']);
expect(JSON.stringify(map.sourcesContent)).toMatch('customMultiply');
});
describe('caller option correctly merges from defaults and options', () => {
test.each([
[
{supportsDynamicImport: true, supportsStaticESM: true},
{supportsDynamicImport: true, supportsStaticESM: true},
],
[
{supportsDynamicImport: false, supportsStaticESM: false},
{supportsDynamicImport: false, supportsStaticESM: false},
],
[
{supportsStaticESM: false},
{supportsDynamicImport: false, supportsStaticESM: false},
],
[
{supportsDynamicImport: true},
{supportsDynamicImport: true, supportsStaticESM: false},
],
])('%j -> %j', (input, output) => {
defaultBabelJestTransformer.process(sourceString, 'dummy_path.js', {
cacheFS: new Map<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
...input,
} as TransformOptions<BabelTransformOptions>);
expect(loadPartialConfig).toHaveBeenCalledTimes(1);
expect(loadPartialConfig).toHaveBeenCalledWith(
expect.objectContaining({
caller: {
name: 'babel-jest',
...output,
supportsExportNamespaceFrom: false,
supportsTopLevelAwait: false,
},
}),
);
});
});
test('can pass null to createTransformer', async () => {
const transformer = await createTransformer();
transformer.process(sourceString, 'dummy_path.js', {
cacheFS: new Map<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
} as TransformOptions<BabelTransformOptions>);
expect(loadPartialConfig).toHaveBeenCalledTimes(1);
expect(loadPartialConfig).toHaveBeenCalledWith(
expect.objectContaining({
caller: {
name: 'babel-jest',
supportsDynamicImport: false,
supportsExportNamespaceFrom: false,
supportsStaticESM: false,
supportsTopLevelAwait: false,
},
}),
);
});