hax to make jest fast
Turns out jest is _incredibly_ slow at resolving require paths like `require('fbjs/lib/foo')`. Like several milliseconds per require. Really adds up when all our files require `invariant` and `warning`. Here's a temporary hack to make things fast again. Test Plan: ``` npm test src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js ``` has a self-proclaimed runtime of ~8 seconds now instead of ~35 seconds.
Ben Alpert committed
Aug 18, 2015 at 21:31 UTC
a8b6c8292b22ce32cab87f1663d8ff92db868230
1 file changed
+25
-7
scripts/jest/preprocessor.js
+25
-7
@@ -1,5 +1,7 @@
1
'use strict';
2
3
+var path = require('path');
4
+
5
var babel = require('babel');
6
var coffee = require('coffee-script');
7
@@ -19,15 +21,16 @@ var babelPluginDEV = require('fbjs/scripts/babel/dev-expression');
21
var babelPluginModules = require('fbjs/scripts/babel/rewrite-modules');
22
23
module.exports = {
22
- process: function(src, path) {
23
- if (path.match(/\.coffee$/)) {
24
+ process: function(src, filePath) {
25
+ if (filePath.match(/\.coffee$/)) {
26
return coffee.compile(src, {'bare': true});
27
}
26
- if (path.match(/\.ts$/) && !path.match(/\.d\.ts$/)) {
27
- return ts.compile(src, path);
28
+ if (filePath.match(/\.ts$/) && !filePath.match(/\.d\.ts$/)) {
29
+ return ts.compile(src, filePath);
30
}
31
// TODO: make sure this stays in sync with gulpfile
30
- if (!path.match(/\/node_modules\//) && !path.match(/\/third_party\//)) {
32
+ if (!filePath.match(/\/node_modules\//) &&
33
+ !filePath.match(/\/third_party\//)) {
34
var rv = babel.transform(src, {
35
nonStandard: true,
36
blacklist: [
@@ -38,11 +41,26 @@ module.exports = {
41
'es7.trailingFunctionCommas',
42
],
43
plugins: [babelPluginDEV, babelPluginModules],
41
- ignore: ['third_party'],
42
- filename: path,
44
+ filename: filePath,
45
retainLines: true,
46
_moduleMap: moduleMap,
47
}).code;
48
+
49
+ // hax to turn fbjs/lib/foo into /path/to/node_modules/fbjs/lib/foo
50
+ // because jest is slooow with node_modules paths (facebook/jest#465)
51
+ rv = rv.replace(
52
+ /require\('(fbjs\/lib\/.+)'\)/g,
53
+ function(call, arg) {
54
+ return (
55
+ 'require(' +
56
+ JSON.stringify(
57
+ path.join(__dirname, '../../node_modules', arg)
58
+ ) +
59
+ ')'
60
+ );
61
+ }
62
+ );
63
+
64
return rv;
65
}
66
return src;