Adapt jest transform for node-only files

Summary:
This changes the jest preprocessor so that files targetet at node.js will be run with the node-specific transform.
It also adapts tests that relied on inline requires.

Benefit: packager tests run faster now.

Reviewed By: cpojer

Differential Revision: D3562007

fbshipit-source-id: e06c86d545926a5c546458025f505dca115e7ea8
This commit is contained in:
David Aurelio 2016-07-15 06:25:27 -07:00 committed by Facebook Github Bot 3
parent 31f01c02db
commit ff0eb47dbd
5 changed files with 52 additions and 22 deletions

View File

@ -8,16 +8,27 @@
*/
'use strict';
const babel = require('babel-core');
const babelRegisterOnly = require('../packager/babelRegisterOnly');
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
const path = require('path');
const transformer = require('../packager/transformer.js');
const nodeFiles = RegExp([
'/local-cli/',
'/packager/(?!react-packager/src/Resolver/polyfills/)',
].join('|'));
const nodeOptions = babelRegisterOnly.config([nodeFiles]);
module.exports = {
process(src, file) {
// Don't transform node_modules, except react-tools which includes the
// untransformed copy of React
if (file.match(/node_modules\/(?!react-tools\/)/)) {
return src;
} else if (nodeFiles.test(file)) { // node specific transforms only
return babel.transform(
src, Object.assign({filename: file}, nodeOptions)).code;
}
return transformer.transform(src, file, {inlineRequires: true}).code;

View File

@ -14,10 +14,13 @@ Object.values || require('core-js/fn/object/values');
var _only = [];
module.exports = function(onlyList) {
_only = _only.concat(onlyList);
function registerOnly(onlyList) {
require('babel-register')(config(onlyList));
}
require('babel-register')({
function config(onlyList) {
_only = _only.concat(onlyList);
return {
presets: ['es2015-node'],
plugins: [
'transform-flow-strip-types',
@ -25,6 +28,11 @@ module.exports = function(onlyList) {
'transform-object-rest-spread',
],
only: _only,
retainLines: true,
sourceMaps: 'inline',
});
};
babelrc: false,
};
}
module.exports = exports = registerOnly;
exports.config = config;

View File

@ -14,12 +14,14 @@ jest.mock('../extract-dependencies');
jest.mock('../inline');
jest.mock('../minify');
const {transformCode} = require('..');
const {any, objectContaining} = jasmine;
describe('code transformation worker:', () => {
let transformCode;
let extractDependencies, transform;
beforeEach(() => {
({transformCode} = require('..'));
extractDependencies =
require('../extract-dependencies').mockReturnValue({});
transform = jest.fn();
@ -68,7 +70,7 @@ describe('code transformation worker:', () => {
done();
});
});
it('removes shebang when present', done => {
const shebang = '#!/usr/bin/env node';
const result = {
@ -81,7 +83,7 @@ describe('code transformation worker:', () => {
done();
});
});
it('calls back with any error yielded by the transform', done => {
const error = Error('arbitrary error');
transform.mockImplementation((_, callback) => callback(error));

View File

@ -11,18 +11,18 @@
jest.unmock('../');
jest.mock('path');
const Promise = require('promise');
const Resolver = require('../');
const path = require('path');
let DependencyGraph = jest.fn();
const DependencyGraph = jest.fn();
jest.setMock('node-haste', DependencyGraph);
let Module;
let Polyfill;
describe('Resolver', function() {
let Resolver, path;
beforeEach(function() {
Resolver = require('../');
path = require('path');
DependencyGraph.mockClear();
Module = jest.fn(function() {
this.getName = jest.fn();
@ -131,7 +131,14 @@ describe('Resolver', function() {
).then(function(result) {
expect(result.mainModuleId).toEqual('index');
expect(result.dependencies[result.dependencies.length - 1]).toBe(module);
expect(DependencyGraph.prototype.createPolyfill.mock.calls.map((call) => call[0])).toEqual([
expect(
DependencyGraph
.prototype
.createPolyfill
.mock
.calls
.map((call) => call[0]))
.toEqual([
{ id: 'polyfills/polyfills.js',
file: 'polyfills/polyfills.js',
dependencies: []

View File

@ -14,23 +14,25 @@ jest.setMock('worker-farm', function() { return () => {}; })
.setMock('timers', { setImmediate: (fn) => setTimeout(fn, 0) })
.setMock('uglify-js')
.setMock('crypto')
.setMock('source-map', { SourceMapConsumer: (fn) => {}})
.setMock('source-map', { SourceMapConsumer: function(fn) {}})
.mock('../../Bundler')
.mock('../../AssetServer')
.mock('../../lib/declareOpts')
.mock('node-haste')
.mock('../../Activity');
const Promise = require('promise');
const SourceMapConsumer = require('source-map').SourceMapConsumer;
const Bundler = require('../../Bundler');
const Server = require('../');
const AssetServer = require('../../AssetServer');
let FileWatcher;
describe('processRequest', () => {
let SourceMapConsumer, Bundler, Server, AssetServer, Promise;
beforeEach(() => {
SourceMapConsumer = require('source-map').SourceMapConsumer;
Bundler = require('../../Bundler');
Server = require('../');
AssetServer = require('../../AssetServer');
Promise = require('promise');
});
let server;
const options = {