forked from jestjs/jest
/
index.ts
126 lines (114 loc) 路 3.15 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
/**
* 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 {makeProjectConfig} from '@jest/test-utils';
import babelJest = require('../index');
import {loadPartialConfig} from '../loadBabelConfig';
jest.mock('../loadBabelConfig', () => {
const actual = jest.requireActual('@babel/core');
return {
loadPartialConfig: jest.fn((...args) => actual.loadPartialConfig(...args)),
};
});
//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 = babelJest.process(
sourceString,
'dummy_path.js',
makeProjectConfig(),
) as any;
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.map!.sources).toEqual(['dummy_path.js']);
expect(JSON.stringify(result.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) => {
babelJest.process(sourceString, 'dummy_path.js', makeProjectConfig(), {
instrument: false,
...input,
});
expect(loadPartialConfig).toHaveBeenCalledTimes(1);
expect(loadPartialConfig).toHaveBeenCalledWith(
expect.objectContaining({
caller: {
name: 'babel-jest',
...output,
supportsExportNamespaceFrom: false,
supportsTopLevelAwait: false,
},
}),
);
});
});
test('can pass null to createTransformer', () => {
const transformer = babelJest.createTransformer(null);
transformer.process(sourceString, 'dummy_path.js', makeProjectConfig(), {
instrument: false,
});
expect(loadPartialConfig).toHaveBeenCalledTimes(1);
expect(loadPartialConfig).toHaveBeenCalledWith(
expect.objectContaining({
caller: {
name: 'babel-jest',
supportsDynamicImport: false,
supportsExportNamespaceFrom: false,
supportsStaticESM: false,
supportsTopLevelAwait: false,
},
}),
);
});