-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwocha.js
executable file
·187 lines (159 loc) · 4.51 KB
/
wocha.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var fs = require('fs');
// var jsdom = require('jsdom');
var Mocha = require('mocha');
var webpack = require('webpack');
var testConfig = require('./webpack.test.config');
var wochaWebpackDescription = [
'Run mocha Tests in ES6 and with webpack!\n',
'To quickly run all the tests under a directory,',
'create a file called wocha.webpack.js and save it to a specific directory.\n',
'wocha.webpack.js',
' var context = require.context(\'.\', true, /\.bc-spec\.js/);',
' context.keys().forEach(context);',
' module.exports = context;\n',
'If saved to /ex/dir, this will run all ex/dir test files ending in .bc-spec.js:',
'$ node -f ex/dir/wocha.webpack.js'
].join('\n');
var argv = require('yargs')
.usage('Usage: node $0 --file [path-to-entry] [options]')
.example('node $0 -f dir/webpack.wocha.js')
.describe('w', 'watch entry file and dependencies')
.alias('w', 'watch')
.demand('f')
.describe('f', 'entry file')
.alias('f', 'file')
.epilog(wochaWebpackDescription)
.argv;
/**
************************
* Get args
************************
*/
var entry = argv.file;
var watch = argv.watch;
/**
************************
* Build Webpack Config
************************
*/
testConfig.entry = './' + entry;
testConfig.target = 'node';
testConfig.output = {
path: entryTransform(entry).outputpath,
filename: entryTransform(entry).filename,
libraryTarget: 'commonjs2'
}
console.log(testConfig)
var compiler = webpack(testConfig);
/**
************************
* Build jsdom
************************
*/
// we have to set up jsdom outside mocha,
// the first time React is required (webpack? etc), it caches document
// jsdomSetup();
global.expect = require('chai').expect;
/**
***********************
* Run once, or run watcher
************************
*/
if (watch){
var watchOptions = {};
compiler.watch(watchOptions, afterBuild);
} else {
compiler.run(afterBuild);
}
function afterBuild(err, stats){
if (err){
console.log(err);
}
console.log('\n');
console.log(stats.toString({
colors:true,
chunks:false
}));
console.log(' +', stats.toJson().modules.length,'hidden modules');
runTests();
}
function runTests(){
var mocha = new Mocha();
mocha.suite.on('pre-require', function(context, file){
uncache(file);
});
var file = entryTransform(entry).output;
mocha.addFile(file);
mocha.run();
}
/**
**********************************************
**************** UTILITIES *****************
**********************************************
*/
function propagateToGlobal (window, blacklist) {
for (var key in window) {
if (!window.hasOwnProperty(key)) continue;
if (~blacklist.indexOf(key)) continue
if (key in global) continue;
global[key] = window[key];
}
}
function jsdomSetup(){
var blacklist = Object.keys(global);
blacklist.push('constructor');
var doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
var win = doc.defaultView;
global.document = doc;
global.window = win;
propagateToGlobal(win, blacklist);
global.navigator = {
userAgent: 'node.js'
};
}
/**
***********************
* file path util
************************
*/
function entryTransform(entry){
var filename = entry.split('/').slice(-1)[0];
var path = entry.split('/').slice(0,-1).join('/');
return {
outputpath: 'temp-wocha/' + path,
path: path,
filename: filename,
output: 'temp-wocha/' + entry
};
}
/**
***********************
* Clear node require cache,
* so that Mocha can re-run tests on watch
************************
*/
/**
* Removes a module from the cache.
* Based on this snippet: http://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate/14801711#14801711
* @param {string} moduleName
*/
function uncache(moduleName) {
searchCache(moduleName, function(mod) {
// dont clear jsdom or jsdom related require cache!!!
if (/jsdom/.test(mod.filename)){ return; }
delete require.cache[mod.id];
});
};
/**
* Runs over the cache to search for all the cached files.
*/
var searchCache = function(moduleName, callback) {
var module = require.resolve(moduleName);
// Check if the module has been resolved and found within the cache.
if (module && ((module = require.cache[module]) !== undefined)) {
(function run(module) {
module.children.forEach(run);
callback(module);
})(module);
}
};