Skip to content

Commit 25b8369

Browse files
authored
Use vm.Script and refactor coverage caching. (#1239)
* Use `vm.Script`. * Rewrite basic coverage support in Jest. * Move `vm.Script` creation and caching into `transform`. * Copy all jest packages into example folders. This ensures we are using the latest version of every package. * Cache the wrapped code. * Use babel-jest for Jest itself; update setup. * Added a transform.js test. * Relax jest.mock babel transform restrictions. * Updated package.json and .npmignore for all packages. * Make watch script resilient to deleted files.
1 parent ca1a290 commit 25b8369

File tree

76 files changed

+586
-426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+586
-426
lines changed

.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"plugins": [
3+
"syntax-trailing-function-commas",
4+
"transform-flow-strip-types"
5+
],
6+
"retainLines": true
7+
}

examples/react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dependencies": {
3-
"react": "~0.14.0",
4-
"react-dom": "~0.14.0"
3+
"react": "~15.2.0",
4+
"react-dom": "~15.2.0"
55
},
66
"devDependencies": {
77
"babel-jest": "*",

integration_tests/__tests__/snapshot-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const originalTestPath = path.resolve(
2626
__dirname,
2727
'../snapshot/__tests__/snapshot-test.js'
2828
);
29-
const originalTestContent = fs.readFileSync(originalTestPath, 'utf-8');
29+
const originalTestContent = fs.readFileSync(originalTestPath, 'utf8');
3030
const copyOfTestPath = originalTestPath.replace('.js', '_copy.js');
3131

3232
const snapshotEscapeDir =
@@ -49,7 +49,7 @@ const fileExists = filePath => {
4949
const getSnapshotOfCopy = () => {
5050
const exports = Object.create(null);
5151
// eslint-disable-next-line no-eval
52-
eval(fs.readFileSync(snapshotOfCopy, 'utf-8'));
52+
eval(fs.readFileSync(snapshotOfCopy, 'utf8'));
5353
return exports;
5454
};
5555

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"clean": "rm -rf ./packages/*/node_modules; npm run build-clean",
2525
"lint": "eslint .",
2626
"postinstall": "node ./scripts/postinstall.js && node ./scripts/build.js",
27+
"publish": "npm run build-clean && npm run build && lerna publish",
2728
"t": "node ./scripts/test.js",
2829
"test": "npm run typecheck && npm run lint && npm run build && npm run t",
2930
"typecheck": "flow check",

packages/babel-jest/.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/__mocks__/**
2+
**/__tests__/**
3+
src
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["es2015", {plugins: ["./"]}]
2+
"presets": ["es2015", {"plugins": ["./"]}]
33
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
__tests__/
2-
__test_modules__/
1+
**/__mocks__/**
2+
**/__tests__/**
3+
src

packages/babel-plugin-jest-hoist/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"react": "^0.14.0"
1313
},
1414
"jest": {
15-
"rootDir": "./build",
15+
"rootDir": "./src",
1616
"scriptPreprocessor": "../../babel-jest",
1717
"testEnvironment": "node"
1818
},

packages/babel-plugin-jest-hoist/src/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ function invariant(condition, message) {
1414
}
1515
}
1616

17-
// We allow `jest`, `require`, all default Node.js globals and all ES2015
18-
// built-ins to be used inside of a `jest.mock` factory.
17+
// We allow `jest`, `expect`, `require`, all default Node.js globals and all
18+
// ES2015 built-ins to be used inside of a `jest.mock` factory.
19+
// We also allow variables prefixed with `mock` as an escape-hatch.
1920
const WHITELISTED_IDENTIFIERS = {
2021
jest: true,
22+
expect: true,
2123
require: true,
2224
Infinity: true,
2325
NaN: true,
@@ -103,12 +105,16 @@ FUNCTIONS.mock = args => {
103105

104106
if (!found) {
105107
invariant(
106-
scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name],
107-
'The second argument of `jest.mock()` is not allowed to ' +
108-
'reference any outside variables.\n' +
108+
(scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name]) ||
109+
/^mock/.test(name),
110+
'The module factory of `jest.mock()` is not allowed to ' +
111+
'reference any out-of-scope variables.\n' +
109112
'Invalid variable access: ' + name + '\n' +
110113
'Whitelisted objects: ' +
111-
Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + '.',
114+
Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + '.\n' +
115+
'Note: This is a precaution to guard against uninitialized mock ' +
116+
'variables. If it is ensured that the mock is required lazily, ' +
117+
'variable names prefixed with `mock` are permitted.',
112118
);
113119
}
114120
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/__mocks__/**
2+
**/__tests__/**
3+
src

0 commit comments

Comments
 (0)