-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest_transform.js
41 lines (32 loc) · 1.18 KB
/
jest_transform.js
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
'use strict';
const swcJest = require('@swc/jest');
const lImport = 'import '.length;
const lGlobals = '//@jest/globals";\n"use strict";\n'.length;
const lFrom = ' from "'.length;
module.exports = {
createTransformer(opts) {
const swcTransformer = swcJest.createTransformer(opts);
return {
...swcTransformer,
process(sourceText, sourcePath, options) {
const globalsImport = sourceText.indexOf('@jest/globals');
if (globalsImport < 0) {
return swcTransformer.process(sourceText, sourcePath, options);
}
const lineStart = sourceText
.slice(0, globalsImport)
.lastIndexOf('import');
const preamble = sourceText.slice(0, lineStart);
const imports = sourceText.slice(
lineStart + lImport,
globalsImport - lFrom,
);
sourceText = `${preamble}//${sourceText.slice(lineStart)}`;
const result = swcTransformer.process(sourceText, sourcePath, options);
const code = result.code.slice(globalsImport + lGlobals);
result.code = `${preamble}"use strict";\nconst ${imports} = require("@jest/globals");\n${code}`;
return result;
},
};
},
};