1 |
|
2 | "use strict";
|
3 |
|
4 | var MemoryFS = require("memory-fs");
|
5 | var realFs = require("fs");
|
6 | var webpack = require("webpack");
|
7 | var path = require("path");
|
8 | var jsdom = require("jsdom");
|
9 |
|
10 | var assert = require("assert");
|
11 |
|
12 | var compiler;
|
13 | var jsdomHtml;
|
14 |
|
15 | module.exports = {
|
16 | setup: function(webpackConfig, _jsdomHtml) {
|
17 | let fs = new MemoryFS();
|
18 |
|
19 | jsdomHtml = _jsdomHtml;
|
20 |
|
21 |
|
22 | Object.assign(webpackConfig, {
|
23 | resolveLoader: {
|
24 | alias: {
|
25 | "style-loader": path.resolve(__dirname, "../")
|
26 | }
|
27 | }
|
28 | });
|
29 |
|
30 | compiler = webpack(webpackConfig);
|
31 |
|
32 |
|
33 | compiler.inputFileSystem = fs;
|
34 | compiler.outputFileSystem = fs;
|
35 | compiler.resolvers.normal.fileSystem = fs;
|
36 | compiler.resolvers.context.fileSystem = fs;
|
37 |
|
38 | ["readFileSync", "statSync"].forEach(fn => {
|
39 |
|
40 | fs["mem" + fn] = fs[fn];
|
41 |
|
42 | compiler.inputFileSystem[fn] = function(_path) {
|
43 |
|
44 | if (fs.existsSync(_path)) {
|
45 | return fs["mem" + fn].apply(fs, arguments);
|
46 | } else {
|
47 | return realFs[fn].apply(realFs, arguments);
|
48 | }
|
49 | };
|
50 | });
|
51 |
|
52 | return fs;
|
53 | },
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | runCompilerTest: function(expected, done, actual, selector) {
|
61 | selector = selector || "head"
|
62 | compiler.run(function(err, stats) {
|
63 | if (stats.compilation.errors.length) {
|
64 | throw new Error(stats.compilation.errors);
|
65 | }
|
66 |
|
67 | const bundleJs = stats.compilation.assets["bundle.js"].source();
|
68 |
|
69 | jsdom.env({
|
70 | html: jsdomHtml,
|
71 | src: [bundleJs],
|
72 | virtualConsole: jsdom.createVirtualConsole().sendTo(console),
|
73 | done: function(err, window) {
|
74 | if (typeof actual === 'function') {
|
75 | assert.equal(actual.apply(window), expected);
|
76 | } else {
|
77 | assert.equal(window.document.querySelector(selector).innerHTML.trim(), expected);
|
78 | }
|
79 |
|
80 | window.close();
|
81 |
|
82 | done();
|
83 | }
|
84 | });
|
85 | });
|
86 | }
|
87 | };
|