-
-
Notifications
You must be signed in to change notification settings - Fork 470
/
utils.js
112 lines (93 loc) · 3.15 KB
/
utils.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
// Node v4 requires "use strict" to allow block scoped let & const
"use strict";
var MemoryFS = require("memory-fs");
var realFs = require("fs");
var webpack = require("webpack");
var path = require("path");
var jsdom = require("jsdom");
var assert = require("assert");
var compiler;
var jsdomHtml;
module.exports = {
setup: function(webpackConfig, _jsdomHtml) {
let fs = new MemoryFS();
jsdomHtml = _jsdomHtml;
// Makes webpack resolve style-loader to local folder instead of node_modules
Object.assign(webpackConfig, {
resolveLoader: {
alias: {
"style-loader": path.resolve(__dirname, "../")
}
}
});
compiler = webpack(webpackConfig);
// Tell webpack to use our in-memory FS
compiler.inputFileSystem = fs;
compiler.outputFileSystem = fs;
compiler.resolvers.normal.fileSystem = fs;
compiler.resolvers.context.fileSystem = fs;
["readFileSync", "statSync"].forEach(fn => {
// Preserve the reference to original function
fs["mem" + fn] = fs[fn];
compiler.inputFileSystem[fn] = function(_path) {
// Fallback to real FS if file is not in the memoryFS
if (fs.existsSync(_path)) {
return fs["mem" + fn].apply(fs, arguments);
} else {
return realFs[fn].apply(realFs, arguments);
}
};
});
return fs;
},
/*
* @param {string} expected - Expected value.
* @param {function} done - Async callback from Mocha.
* @param {function} actual - Executed in the context of jsdom window, should return a string to compare to.
*/
runCompilerTest: function(expected, done, actual, selector) {
selector = selector || "head"
compiler.run(function(err, stats) {
if (stats.compilation.errors.length) {
throw new Error(stats.compilation.errors);
}
const bundleJs = stats.compilation.assets["bundle.js"].source();
jsdom.env({
html: jsdomHtml,
src: [bundleJs],
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
done: function(err, window) {
if (typeof actual === 'function') {
assert.equal(actual.apply(window), expected);
} else {
assert.equal(window.document.querySelector(selector).innerHTML.trim(), expected);
}
// free memory associated with the window
window.close();
done();
}
});
});
},
/*
* Runs the test against Webpack compiled source code.
* @param {regex} regexToMatch - regex to match the source code
* @param {regex} regexToNotMatch - regex to NOT match the source code
* @param {function} done - Async callback from Mocha.
*/
runSourceTest: function(regexToMatch, regexToNotMatch, done) {
compiler.run(function(err, stats) {
if (stats.compilation.errors.length) {
throw new Error(stats.compilation.errors);
}
const bundleJs = stats.compilation.assets["bundle.js"].source();
if (regexToMatch) {
assert.equal(regexToMatch.test(bundleJs), true);
}
if (regexToNotMatch) {
assert.equal(regexToNotMatch.test(bundleJs), false);
}
done();
});
}
};