-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
index.ts
77 lines (65 loc) · 1.98 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
/**
* 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 {Config} from '@jest/types';
import babelJest from '../index';
//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);
`;
const mockConfig = {
moduleFileExtensions: [],
};
test(`Returns source string with inline maps when no transformOptions is passed`, () => {
const result = babelJest.process(
sourceString,
'dummy_path.js',
(mockConfig as unknown) as Config.ProjectConfig,
) 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');
});
test('getCacheKey does not depend on the rootDir', () => {
const getCacheKeyArgs = rootDir => {
const config = {
cwd: rootDir,
rootDir,
};
return [
'// Some code',
`${rootDir}/foo/bar.js`,
JSON.stringify(config),
{config, instrument: true, rootDir: config.rootDir},
];
};
jest.mock('@babel/core');
require('@babel/core').loadPartialConfig = options => ({
babelrc: `${options.cwd}/foo/.babelrc`,
config: `${options.cwd}/babel.config.js`,
options: {
cwd: options.cwd,
someOtherOption: `${options.cwd}/foo`,
},
});
const cacheKeyForRoot1 = babelJest.getCacheKey(
...getCacheKeyArgs('/root1/dir'),
);
const cacheKeyForRoot2 = babelJest.getCacheKey(
...getCacheKeyArgs('/root2/dir'),
);
expect(cacheKeyForRoot1).toEqual(cacheKeyForRoot2);
});