-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.js
110 lines (98 loc) · 3.62 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Test running for fast-async babel plugin
*
* Open the file ./test-input.js and compile it using the fast-async plugin and run it.
*
* It should output 'return: true'
*/
require('colors');
var fs = require('fs') ;
try {
var babel = require("babel-core") ;
} catch (ex) {
console.log("#### To run the tests you need to "+"npm install babel-core".yellow+" (run 'npm i' in this directory). This additional module is only required for testing, not deployment.") ;
process.exit(-1) ;
}
try {
global.gc() ;
} catch (ex) {
console.log("You get more accurate timings by running with the node option --expose-gc") ;
global.gc = function(){} ;
}
console.log("\nNB:The timings here are only indicative. GC and poor sampling generate variable results. More detailed performance tests can be found in "+"nodent".cyan+"\nStarting tests...");
try {
global.Promise = global.Promise || require('bluebird') ;
} catch (ex) {
global.Promise = global.Promise || require('nodent').Thenable ;
}
var testCode = require('fs').readFileSync(__dirname+'/test-input.js').toString() ;
var transformers = {
'fast-async (es7-lazy)':{plugins:[[require('../plugin.js'),{runtimePatten:'directive',env:{dontMapStackTraces:true},compiler:{promises:false,es7:true,lazyThenables:true}}]]},
'fast-async (promises)':{plugins:[[require('../plugin.js'),{runtimePatten:'directive',env:{dontMapStackTraces:true},compiler:{promises:true}}]]}
};
var requires ;
try {
requires = ['babel-plugin-transform-async-to-generator','babel-preset-es2015'] ;
requires.map(require) ;
transformers['transform-async-to-generator'] = {"presets": ["es2015"],plugins:['transform-async-to-generator']} ;
} catch (ex) {
/* not installed */
console.log("'"+("npm install "+requires.join(' ')).yellow+"' to compare against babel plugin 'transform-async-to-generator'") ;
}
try {
requires = ['babel-plugin-transform-async-to-module-method','babel-preset-es2015','bluebird','coroutine'] ;
requires.map(require) ;
transformers['transform-async-to-module-method'] = {"presets": ["es2015"],plugins:[["transform-async-to-module-method",{module: 'bluebird',method: 'coroutine'}]]}
} catch (ex) {
/* not installed */
console.log("'"+("npm install "+requires.join(' ')).yellow+"' to compare against babel plugin 'transform-async-to-module-method'") ;
}
function loadRegenerator(){
try {
function walkSync(dir,match) {
if( dir[dir.length-1] != '/') dir=dir.concat('/')
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
files.forEach(function(file) {
var stat = fs.lstatSync(dir + file) ;
if (!stat.isSymbolicLink()) {
if (file==match)
throw dir + file ;
if (stat.isDirectory())
walkSync(dir + file + '/',match);
}
});
};
walkSync('node_modules','regenerator') ;
console.log("Couldn't locate regenerator runtime") ;
} catch (path) {
global.regeneratorRuntime = require("./"+path);
console.log("Loaded regenerator runtime from "+path+" ",regeneratorRuntime.toString().yellow) ;
}
}
var keys = Object.keys(transformers) ;
(function nextTest(i){
try {
if (i===2)
loadRegenerator() ;
console.log("Transforming with "+keys[i]);
var t = babel.transform(testCode, transformers[keys[i]]);
var f = new Function("require,resolve,reject",t.code) ;
global.gc() ;
f(require,
function(result){
console.log(keys[i],result.green) ;
next() ;
},function(error){
console.log(keys[i],(error.stack || error).red) ;
next() ;
}) ;
function next() {
i++ ;
if (i<keys.length)
nextTest(i) ;
}
} catch (ex) {
console.error(ex.stack.red) ;
next() ;
}
})(0) ;