mirror of https://github.com/status-im/metro.git
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:
parent
9d6414281e
commit
3263f18a6e
|
@ -14,10 +14,13 @@ Object.values || require('core-js/fn/object/values');
|
||||||
|
|
||||||
var _only = [];
|
var _only = [];
|
||||||
|
|
||||||
module.exports = function(onlyList) {
|
function registerOnly(onlyList) {
|
||||||
_only = _only.concat(onlyList);
|
require('babel-register')(config(onlyList));
|
||||||
|
}
|
||||||
|
|
||||||
require('babel-register')({
|
function config(onlyList) {
|
||||||
|
_only = _only.concat(onlyList);
|
||||||
|
return {
|
||||||
presets: ['es2015-node'],
|
presets: ['es2015-node'],
|
||||||
plugins: [
|
plugins: [
|
||||||
'transform-flow-strip-types',
|
'transform-flow-strip-types',
|
||||||
|
@ -25,6 +28,11 @@ module.exports = function(onlyList) {
|
||||||
'transform-object-rest-spread',
|
'transform-object-rest-spread',
|
||||||
],
|
],
|
||||||
only: _only,
|
only: _only,
|
||||||
|
retainLines: true,
|
||||||
sourceMaps: 'inline',
|
sourceMaps: 'inline',
|
||||||
});
|
babelrc: false,
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = exports = registerOnly;
|
||||||
|
exports.config = config;
|
||||||
|
|
|
@ -14,12 +14,14 @@ jest.mock('../extract-dependencies');
|
||||||
jest.mock('../inline');
|
jest.mock('../inline');
|
||||||
jest.mock('../minify');
|
jest.mock('../minify');
|
||||||
|
|
||||||
const {transformCode} = require('..');
|
|
||||||
const {any, objectContaining} = jasmine;
|
const {any, objectContaining} = jasmine;
|
||||||
|
|
||||||
describe('code transformation worker:', () => {
|
describe('code transformation worker:', () => {
|
||||||
|
let transformCode;
|
||||||
|
|
||||||
let extractDependencies, transform;
|
let extractDependencies, transform;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
({transformCode} = require('..'));
|
||||||
extractDependencies =
|
extractDependencies =
|
||||||
require('../extract-dependencies').mockReturnValue({});
|
require('../extract-dependencies').mockReturnValue({});
|
||||||
transform = jest.fn();
|
transform = jest.fn();
|
||||||
|
@ -68,7 +70,7 @@ describe('code transformation worker:', () => {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('removes shebang when present', done => {
|
it('removes shebang when present', done => {
|
||||||
const shebang = '#!/usr/bin/env node';
|
const shebang = '#!/usr/bin/env node';
|
||||||
const result = {
|
const result = {
|
||||||
|
@ -81,7 +83,7 @@ describe('code transformation worker:', () => {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls back with any error yielded by the transform', done => {
|
it('calls back with any error yielded by the transform', done => {
|
||||||
const error = Error('arbitrary error');
|
const error = Error('arbitrary error');
|
||||||
transform.mockImplementation((_, callback) => callback(error));
|
transform.mockImplementation((_, callback) => callback(error));
|
||||||
|
|
|
@ -11,18 +11,18 @@
|
||||||
jest.unmock('../');
|
jest.unmock('../');
|
||||||
jest.mock('path');
|
jest.mock('path');
|
||||||
|
|
||||||
const Promise = require('promise');
|
|
||||||
const Resolver = require('../');
|
|
||||||
|
|
||||||
const path = require('path');
|
const DependencyGraph = jest.fn();
|
||||||
|
|
||||||
let DependencyGraph = jest.fn();
|
|
||||||
jest.setMock('node-haste', DependencyGraph);
|
jest.setMock('node-haste', DependencyGraph);
|
||||||
let Module;
|
let Module;
|
||||||
let Polyfill;
|
let Polyfill;
|
||||||
|
|
||||||
describe('Resolver', function() {
|
describe('Resolver', function() {
|
||||||
|
let Resolver, path;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
Resolver = require('../');
|
||||||
|
path = require('path');
|
||||||
DependencyGraph.mockClear();
|
DependencyGraph.mockClear();
|
||||||
Module = jest.fn(function() {
|
Module = jest.fn(function() {
|
||||||
this.getName = jest.fn();
|
this.getName = jest.fn();
|
||||||
|
@ -131,7 +131,14 @@ describe('Resolver', function() {
|
||||||
).then(function(result) {
|
).then(function(result) {
|
||||||
expect(result.mainModuleId).toEqual('index');
|
expect(result.mainModuleId).toEqual('index');
|
||||||
expect(result.dependencies[result.dependencies.length - 1]).toBe(module);
|
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',
|
{ id: 'polyfills/polyfills.js',
|
||||||
file: 'polyfills/polyfills.js',
|
file: 'polyfills/polyfills.js',
|
||||||
dependencies: []
|
dependencies: []
|
||||||
|
|
|
@ -14,23 +14,25 @@ jest.setMock('worker-farm', function() { return () => {}; })
|
||||||
.setMock('timers', { setImmediate: (fn) => setTimeout(fn, 0) })
|
.setMock('timers', { setImmediate: (fn) => setTimeout(fn, 0) })
|
||||||
.setMock('uglify-js')
|
.setMock('uglify-js')
|
||||||
.setMock('crypto')
|
.setMock('crypto')
|
||||||
.setMock('source-map', { SourceMapConsumer: (fn) => {}})
|
.setMock('source-map', { SourceMapConsumer: function(fn) {}})
|
||||||
.mock('../../Bundler')
|
.mock('../../Bundler')
|
||||||
.mock('../../AssetServer')
|
.mock('../../AssetServer')
|
||||||
.mock('../../lib/declareOpts')
|
.mock('../../lib/declareOpts')
|
||||||
.mock('node-haste')
|
.mock('node-haste')
|
||||||
.mock('../../Activity');
|
.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;
|
let FileWatcher;
|
||||||
|
|
||||||
describe('processRequest', () => {
|
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;
|
let server;
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
|
|
Loading…
Reference in New Issue