UNPKG

4.03 kBJavaScriptView Raw
1/* Test running for fast-async babel plugin
2 *
3 * Open the file ./test-input.js and compile it using the fast-async plugin and run it.
4 *
5 * It should output 'return: true'
6 */
7
8require('colors');
9var fs = require('fs') ;
10
11try {
12 var babel = require("@babel/core") ;
13} catch (ex) {
14 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.") ;
15 process.exit(-1) ;
16}
17
18try {
19 global.gc() ;
20} catch (ex) {
21 console.log("You get more accurate timings by running with the node option --expose-gc") ;
22 global.gc = function(){} ;
23}
24
25console.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...");
26
27var nodent = require('nodent') ;
28var systemPromise = global.Promise || nodent.EagerThenable() ;
29
30global.Promise = nodent.EagerThenable() ;
31
32var testCode = require('fs').readFileSync(__dirname+'/test-input.js').toString() ;
33
34var eagerName = 'fast-async (nodent-'+nodent.EagerThenable().name+')' ;
35var transformers = {
36 'fast-async (es7-lazy)': {plugins:[[require('../plugin.js'),{runtimePatten:'directive',compiler:{promises:false,es7:true,lazyThenables:true}}]]},
37 'fast-async (spec:true)': {plugins:[[require('../plugin.js'),{runtimePatten:null,compiler:{promises:true,es7:true,noRuntime:true,wrapAwait:true}}]]},
38};
39transformers[eagerName] = {plugins:[[require('../plugin.js'),{runtimePatten:'directive',compiler:{promises:false,es7:true,lazyThenables:false}}]]} ;
40
41
42var needRegenerator = Object.keys(transformers).length ;
43
44var requires ;
45try {
46 requires = ['babel-plugin-transform-async-to-generator','babel-preset-es2015'] ;
47 requires.map(require) ;
48 transformers['transform-async-to-generator'] = {"presets": ["es2015"],plugins:['transform-async-to-generator']} ;
49} catch (ex) {
50 /* not installed */
51 console.log("'"+("npm install "+requires.join(' ')).yellow+"' to compare against babel plugin 'transform-async-to-generator'") ;
52}
53
54try {
55 requires = ['babel-plugin-transform-async-to-module-method','babel-preset-es2015','bluebird','coroutine'] ;
56 requires.map(require) ;
57 transformers['transform-async-to-module-method'] = {"presets": ["es2015"],plugins:[["transform-async-to-module-method",{module: 'bluebird',method: 'coroutine'}]]}
58} catch (ex) {
59 /* not installed */
60 console.log("'"+("npm install "+requires.join(' ')).yellow+"' to compare against babel plugin 'transform-async-to-module-method'") ;
61}
62
63function loadRegenerator(){
64 try {
65 function walkSync(dir,match) {
66 if( dir[dir.length-1] != '/') dir=dir.concat('/')
67 var fs = fs || require('fs'),
68 files = fs.readdirSync(dir);
69 files.forEach(function(file) {
70 var stat = fs.lstatSync(dir + file) ;
71 if (!stat.isSymbolicLink()) {
72 if (file==match)
73 throw dir + file ;
74 if (stat.isDirectory())
75 walkSync(dir + file + '/',match);
76 }
77 });
78 };
79 walkSync('node_modules','regenerator') ;
80 console.log("Couldn't locate regenerator runtime") ;
81 } catch (path) {
82 global.regeneratorRuntime = require("./"+path);
83 console.log("Loaded regenerator runtime from "+path+" ",regeneratorRuntime.toString().yellow) ;
84 global.Promise = systemPromise ;
85 }
86}
87
88var keys = Object.keys(transformers) ;
89(function nextTest(i){
90 try {
91 if (i===needRegenerator)
92 loadRegenerator() ;
93
94 console.log("Transforming with "+keys[i]);
95 var t = babel.transform(testCode, transformers[keys[i]]);
96 var f = new Function("require,resolve,reject",t.code) ;
97
98 global.gc() ;
99 f(require,
100 function(result){
101 console.log(keys[i],result.green) ;
102 next() ;
103 },function(error){
104 console.log(keys[i],(error.stack || error).red) ;
105 next() ;
106 }) ;
107 function next() {
108 i++ ;
109 if (i<keys.length)
110 nextTest(i) ;
111 }
112 } catch (ex) {
113 console.error(ex.stack.red) ;
114 next() ;
115 }
116})(0) ;