Skip to content

Commit

Permalink
Use babel-jest for Jest itself; update setup.
Browse files Browse the repository at this point in the history
* Added a transform.js test.
* Relax jest.mock babel transform restrictions.
* Updated package.json and .npmignore for all packages.
  • Loading branch information
cpojer committed Jul 7, 2016
1 parent bba6415 commit d50b02e
Show file tree
Hide file tree
Showing 61 changed files with 342 additions and 131 deletions.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
"syntax-trailing-function-commas",
"transform-flow-strip-types"
],
"retainLines": true
}
4 changes: 2 additions & 2 deletions integration_tests/__tests__/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const originalTestPath = path.resolve(
__dirname,
'../snapshot/__tests__/snapshot-test.js'
);
const originalTestContent = fs.readFileSync(originalTestPath, 'utf-8');
const originalTestContent = fs.readFileSync(originalTestPath, 'utf8');
const copyOfTestPath = originalTestPath.replace('.js', '_copy.js');

const snapshotEscapeDir =
Expand All @@ -49,7 +49,7 @@ const fileExists = filePath => {
const getSnapshotOfCopy = () => {
const exports = Object.create(null);
// eslint-disable-next-line no-eval
eval(fs.readFileSync(snapshotOfCopy, 'utf-8'));
eval(fs.readFileSync(snapshotOfCopy, 'utf8'));
return exports;
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"clean": "rm -rf ./packages/*/node_modules; npm run build-clean",
"lint": "eslint .",
"postinstall": "node ./scripts/postinstall.js && node ./scripts/build.js",
"publish": "npm run build-clean && npm run build && lerna publish",
"t": "node ./scripts/test.js",
"test": "npm run typecheck && npm run lint && npm run build && npm run t",
"typecheck": "flow check",
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-jest/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
2 changes: 1 addition & 1 deletion packages/babel-plugin-jest-hoist/.babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["es2015", {plugins: ["./"]}]
"presets": ["es2015", {"plugins": ["./"]}]
}
5 changes: 3 additions & 2 deletions packages/babel-plugin-jest-hoist/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__tests__/
__test_modules__/
**/__mocks__/**
**/__tests__/**
src
2 changes: 1 addition & 1 deletion packages/babel-plugin-jest-hoist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"react": "^0.14.0"
},
"jest": {
"rootDir": "./build",
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
Expand Down
18 changes: 12 additions & 6 deletions packages/babel-plugin-jest-hoist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ function invariant(condition, message) {
}
}

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

if (!found) {
invariant(
scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name],
'The second argument of `jest.mock()` is not allowed to ' +
'reference any outside variables.\n' +
(scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name]) ||
/^mock/.test(name),
'The module factory of `jest.mock()` is not allowed to ' +
'reference any out-of-scope variables.\n' +
'Invalid variable access: ' + name + '\n' +
'Whitelisted objects: ' +
Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + '.',
Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + '.\n' +
'Note: This is a precaution to guard against uninitialized mock ' +
'variables. If it is ensured that the mock is required lazily, ' +
'variable names prefixed with `mock` are permitted.',
);
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-preset-jest/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
4 changes: 2 additions & 2 deletions packages/jest-changed-files/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__mocks__/
__tests__/
**/__mocks__/**
**/__tests__/**
src
3 changes: 2 additions & 1 deletion packages/jest-changed-files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"rimraf": "^2.5.2"
},
"jest": {
"rootDir": "./build",
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-cli/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__tests__
__mocks__
**/__mocks__/**
**/__tests__/**
src
5 changes: 3 additions & 2 deletions packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
"test": "./bin/jest.js"
},
"jest": {
"rootDir": "./build",
"testEnvironment": "jest-environment-node"
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
"bugs": {
"url": "https://github.com/facebook/jest/issues"
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-config/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__tests__
__mocks__
**/__mocks__/**
**/__tests__/**
src
1 change: 1 addition & 0 deletions packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"jest": {
"rootDir": "./build",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "jest-environment-node"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-environment-jsdom/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__tests__
__mocks__
**/__mocks__/**
**/__tests__/**
src
4 changes: 2 additions & 2 deletions packages/jest-environment-node/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__tests__
__mocks__
**/__mocks__/**
**/__tests__/**
src
3 changes: 2 additions & 1 deletion packages/jest-environment-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"jest-util": "^13.2.2"
},
"jest": {
"rootDir": "./build",
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-haste-map/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__tests__
__mocks__
**/__mocks__/**
**/__tests__/**
src
3 changes: 2 additions & 1 deletion packages/jest-haste-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"worker-farm": "^1.3.1"
},
"jest": {
"rootDir": "./build",
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
"scripts": {
Expand Down
34 changes: 17 additions & 17 deletions packages/jest-haste-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jest.mock('../crawlers/watchman', () =>
jest.fn((roots, extensions, ignore, data) => {
data.clocks = mockClocks;

const list = changedFiles || mockFs;
const list = mockChangedFiles || mockFs;
for (const file in list) {
if (new RegExp(roots.join('|')).test(file) && !ignore(file)) {
if (list[file]) {
Expand All @@ -52,18 +52,18 @@ jest.mock('../crawlers/watchman', () =>
);

const cacheFilePath = '/cache-file';
let H;
let HasteMap;
let changedFiles;
let consoleWarn;
let defaultConfig;
let fs;
let H;
let HasteMap;
let mockChangedFiles;
let mockClocks;
let mockFs;
let object;
let readFileSync;
let writeFileSync;
let workerFarmMock;
let writeFileSync;

describe('HasteMap', () => {

Expand Down Expand Up @@ -108,18 +108,18 @@ describe('HasteMap', () => {
'/vegetables': 'c:fake-clock:2',
});

changedFiles = null;
mockChangedFiles = null;

fs = require('graceful-fs');
readFileSync = fs.readFileSync;
writeFileSync = fs.writeFileSync;
fs.readFileSync = jest.fn((path, options) => {
expect(options).toBe('utf-8');
expect(options).toBe('utf8');

// A file change can be triggered by writing into the
// changedFiles object.
if (changedFiles && (path in changedFiles)) {
return changedFiles[path];
// mockChangedFiles object.
if (mockChangedFiles && (path in mockChangedFiles)) {
return mockChangedFiles[path];
}

if (mockFs[path]) {
Expand All @@ -129,7 +129,7 @@ describe('HasteMap', () => {
throw new Error(`Cannot read path '${path}'.`);
});
fs.writeFileSync = jest.fn((path, data, options) => {
expect(options).toBe('utf-8');
expect(options).toBe('utf8');
mockFs[path] = data;
});

Expand Down Expand Up @@ -354,7 +354,7 @@ describe('HasteMap', () => {
fs.readFileSync.mockClear();

// Explicitly mock that no files have changed.
changedFiles = Object.create(null);
mockChangedFiles = Object.create(null);

// Watchman would give us different clocks.
mockClocks = object({
Expand All @@ -364,7 +364,7 @@ describe('HasteMap', () => {

return new HasteMap(defaultConfig).build().then(data => {
expect(fs.readFileSync.mock.calls.length).toBe(1);
expect(fs.readFileSync).toBeCalledWith(cacheFilePath, 'utf-8');
expect(fs.readFileSync).toBeCalledWith(cacheFilePath, 'utf8');

expect(data.clocks).toEqual(mockClocks);
expect(data.files).toEqual(initialData.files);
Expand All @@ -378,7 +378,7 @@ describe('HasteMap', () => {
fs.readFileSync.mockClear();

// Let's assume one JS file has changed.
changedFiles = object({
mockChangedFiles = object({
'/fruits/banana.js': [
'/**',
' * @providesModule Kiwi', // Identity crisis.
Expand All @@ -396,8 +396,8 @@ describe('HasteMap', () => {
return new HasteMap(defaultConfig).build().then(data => {
expect(fs.readFileSync.mock.calls.length).toBe(2);

expect(fs.readFileSync).toBeCalledWith(cacheFilePath, 'utf-8');
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf-8');
expect(fs.readFileSync).toBeCalledWith(cacheFilePath, 'utf8');
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');

expect(data.clocks).toEqual(mockClocks);

Expand All @@ -421,7 +421,7 @@ describe('HasteMap', () => {

// Let's assume one JS file was removed.
delete mockFs['/fruits/banana.js'];
changedFiles = object({
mockChangedFiles = object({
'/fruits/banana.js': null,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/__tests__/worker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('worker', () => {
fs = require('graceful-fs');
readFileSync = fs.readFileSync;
fs.readFileSync = jest.fn((path, options) => {
expect(options).toBe('utf-8');
expect(options).toBe('utf8');

if (mockFs[path]) {
return mockFs[path];
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class HasteMap {
* 1. read data from the cache or create an empty structure.
*/
read(): HasteMapObject {
return this._parse(fs.readFileSync(this._cachePath, 'utf-8'));
return this._parse(fs.readFileSync(this._cachePath, 'utf8'));
}

/**
Expand Down Expand Up @@ -368,7 +368,7 @@ class HasteMap {
* 4. serialize the new `HasteMap` in a cache file.
*/
_persist(hasteMap: HasteMapObject): HasteMapObject {
fs.writeFileSync(this._cachePath, JSON.stringify(hasteMap), 'utf-8');
fs.writeFileSync(this._cachePath, JSON.stringify(hasteMap), 'utf8');
return hasteMap;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const formatError = (error: string|Error): Error => {
module.exports = (data: WorkerMessage, callback: WorkerCallback): void => {
try {
const filePath = data.filePath;
const content = fs.readFileSync(filePath, 'utf-8');
const content = fs.readFileSync(filePath, 'utf8');
let module;
let id;
let dependencies;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-jasmine1/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__tests__
__mocks__
**/__mocks__/**
**/__tests__/**
src
3 changes: 2 additions & 1 deletion packages/jest-jasmine1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"jest-util": "^13.2.2"
},
"jest": {
"rootDir": "./build",
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-jasmine2/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__tests__
__mocks__
**/__mocks__/**
**/__tests__/**
src
3 changes: 2 additions & 1 deletion packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"jest-util": "^13.2.2"
},
"jest": {
"rootDir": "./build",
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-mock/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__tests__
__mocks__
**/__mocks__/**
**/__tests__/**
src
3 changes: 2 additions & 1 deletion packages/jest-mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"license": "BSD-3-Clause",
"main": "build/index.js",
"jest": {
"rootDir": "./build",
"rootDir": "./src",
"scriptPreprocessor": "../../babel-jest",
"testEnvironment": "node"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-repl/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__mocks__/
__tests__/
**/__mocks__/**
**/__tests__/**
src
Loading

0 comments on commit d50b02e

Please sign in to comment.