forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel-plugin-jest-native-globals.js
122 lines (114 loc) · 4.15 KB
/
babel-plugin-jest-native-globals.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
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
/**
* 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.
*/
'use strict';
// This plugin exists to make sure that we use a `Promise` that has not been messed with by user code.
// Might consider extending this to other globals as well in the future
module.exports = ({template}) => {
const promiseDeclaration = template(`
var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
`);
const symbolDeclaration = template(`
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
`);
const nowDeclaration = template(`
var jestNow = global[Symbol.for('jest-native-now')] || global.Date.now;
`);
const fsReadFileDeclaration = template(`
var jestReadFile = global[Symbol.for('jest-native-read-file')] || fs.readFileSync;
`);
const fsWriteFileDeclaration = template(`
var jestWriteFile = global[Symbol.for('jest-native-write-file')] || fs.writeFileSync;
`);
const fsExistsFileDeclaration = template(`
var jestExistsFile = global[Symbol.for('jest-native-exists-file')] || fs.existsSync;
`);
return {
name: 'jest-native-globals',
visitor: {
ReferencedIdentifier(path, state) {
if (path.node.name === 'Promise' && !state.jestInjectedPromise) {
state.jestInjectedPromise = true;
path
.findParent(p => p.isProgram())
.unshiftContainer('body', promiseDeclaration());
path
.findParent(p => p.isProgram())
.unshiftContainer('body', symbolDeclaration());
}
if (path.node.name === 'Symbol' && !state.jestInjectedSymbol) {
state.jestInjectedSymbol = true;
path
.findParent(p => p.isProgram())
.unshiftContainer('body', symbolDeclaration());
}
if (
path.node.name === 'Date' &&
path.parent.property &&
path.parent.property.name === 'now'
) {
if (!state.jestInjectedNow) {
state.jestInjectedNow = true;
path
.findParent(p => p.isProgram())
.unshiftContainer('body', nowDeclaration());
path
.findParent(p => p.isProgram())
.unshiftContainer('body', symbolDeclaration());
}
path.parentPath.replaceWithSourceString('jestNow');
}
if (
path.node.name === 'fs' &&
path.parent.property &&
['readFileSync', 'writeFileSync', 'existsSync'].includes(
path.parent.property.name
)
) {
if (
!state.jestInjectedRead &&
path.parent.property.name === 'readFileSync'
) {
state.jestInjectedRead = true;
path
.findParent(p => p.isProgram())
.unshiftContainer('body', fsReadFileDeclaration());
path
.findParent(p => p.isProgram())
.unshiftContainer('body', symbolDeclaration());
path.parentPath.replaceWithSourceString('jestReadFile');
}
if (
!state.jestInjectedWrite &&
path.parent.property.name === 'writeFileSync'
) {
state.jestInjectedWrite = true;
path
.findParent(p => p.isProgram())
.unshiftContainer('body', fsWriteFileDeclaration());
path
.findParent(p => p.isProgram())
.unshiftContainer('body', symbolDeclaration());
path.parentPath.replaceWithSourceString('jestWriteFile');
}
if (
!state.jestInjectedExists &&
path.parent.property.name === 'existsSync'
) {
state.jestInjectedExists = true;
path
.findParent(p => p.isProgram())
.unshiftContainer('body', fsExistsFileDeclaration());
path
.findParent(p => p.isProgram())
.unshiftContainer('body', symbolDeclaration());
path.parentPath.replaceWithSourceString('jestExistsFile');
}
}
},
},
};
};