-
-
Notifications
You must be signed in to change notification settings - Fork 423
/
index.test.js
333 lines (310 loc) · 14.5 KB
/
index.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"use strict";
require("should");
const path = require("path");
const webpack = require("webpack");
const fs = require("fs");
const merge = require("webpack-merge");
const customImporter = require("./tools/customImporter.js");
const customFunctions = require("./tools/customFunctions.js");
const pathToSassLoader = require.resolve("../lib/loader.js");
const testLoader = require("./tools/testLoader");
const sassLoader = require(pathToSassLoader);
const mockRequire = require("mock-require");
const CR = /\r/g;
const syntaxStyles = ["scss", "sass"];
const pathToErrorFileNotFound = path.resolve(__dirname, "./scss/error-file-not-found.scss");
const pathToErrorFileNotFound2 = path.resolve(__dirname, "./scss/error-file-not-found-2.scss");
const pathToErrorFile = path.resolve(__dirname, "./scss/error.scss");
const pathToErrorImport = path.resolve(__dirname, "./scss/error-import.scss");
const loaderContextMock = {
async: Function.prototype,
cacheable: Function.prototype,
dependency: Function.prototype
};
Object.defineProperty(loaderContextMock, "options", {
set() { },
get() {
throw new Error("webpack options are not allowed to be accessed anymore.");
}
});
syntaxStyles.forEach(ext => {
function execTest(testId, loaderOptions, webpackOptions) {
return new Promise((resolve, reject) => {
const baseConfig = merge({
entry: path.join(__dirname, ext, testId + "." + ext),
output: {
filename: "bundle." + ext + ".js"
},
module: {
rules: [{
test: new RegExp(`\\.${ ext }$`),
use: [
{ loader: "raw-loader" },
{ loader: pathToSassLoader, options: loaderOptions }
]
}]
}
}, webpackOptions);
runWebpack(baseConfig, (err) => err ? reject(err) : resolve());
}).then(() => {
const actualCss = readBundle("bundle." + ext + ".js");
const expectedCss = readCss(ext, testId);
// writing the actual css to output-dir for better debugging
// fs.writeFileSync(path.join(__dirname, "output", `${ testId }.${ ext }.css`), actualCss, "utf8");
actualCss.should.eql(expectedCss);
});
}
describe(`sass-loader (${ ext })`, () => {
describe("basic", () => {
it("should compile simple sass without errors", () => execTest("language"));
});
describe("imports", () => {
it("should resolve imports correctly", () => execTest("imports"));
// Test for issue: https://github.com/webpack-contrib/sass-loader/issues/32
it("should pass with multiple imports", () => execTest("multiple-imports"));
// Test for issue: https://github.com/webpack-contrib/sass-loader/issues/73
it("should resolve imports from other language style correctly", () => execTest("import-other-style"));
// Test for includePath imports
it("should resolve imports from another directory declared by includePaths correctly", () => execTest("import-include-paths", {
includePaths: [path.join(__dirname, ext, "includePath")]
}));
it("should not resolve CSS imports", () => execTest("import-css"));
it("should compile bootstrap-sass without errors", () => execTest("bootstrap-sass"));
it("should correctly import scoped npm packages", () => execTest("import-from-npm-org-pkg"));
it("should resolve aliases", () => execTest("import-alias", {}, {
resolve: {
alias: {
"path-to-alias": path.join(__dirname, ext, "another", "alias." + ext)
}
}
}));
});
describe("custom importers", () => {
it("should use custom importer", () => execTest("custom-importer", {
importer: customImporter
}));
});
describe("custom functions", () => {
it("should expose custom functions", () => execTest("custom-functions", {
functions: customFunctions
}));
});
describe("prepending data", () => {
it("should extend the data-option if present", () => execTest("prepending-data", {
data: "$prepended-data: hotpink;"
}));
});
// See https://github.com/webpack-contrib/sass-loader/issues/21
describe("empty files", () => {
it("should compile without errors", () => execTest("empty"));
});
});
});
describe("sass-loader", () => {
describe("multiple compilations", () => {
it("should not interfere with each other", () =>
new Promise((resolve, reject) => {
runWebpack({
entry: {
b: path.join(__dirname, "scss", "multipleCompilations", "b.scss"),
c: path.join(__dirname, "scss", "multipleCompilations", "c.scss"),
a: path.join(__dirname, "scss", "multipleCompilations", "a.scss"),
d: path.join(__dirname, "scss", "multipleCompilations", "d.scss"),
e: path.join(__dirname, "scss", "multipleCompilations", "e.scss"),
f: path.join(__dirname, "scss", "multipleCompilations", "f.scss"),
g: path.join(__dirname, "scss", "multipleCompilations", "g.scss"),
h: path.join(__dirname, "scss", "multipleCompilations", "h.scss")
},
output: {
filename: "bundle.multiple-compilations.[name].js"
},
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: "raw-loader" },
// We're specifying an empty options object because otherwise, webpack creates a new object for every loader invocation
// Since we want to ensure that our loader is not tampering with the option object, we are triggering webpack to re-use the options object
// @see https://github.com/webpack-contrib/sass-loader/issues/368#issuecomment-278330164
{ loader: pathToSassLoader, options: {} }
]
}]
}
}, err => err ? reject(err) : resolve());
})
.then(() => {
const expectedCss = readCss("scss", "imports");
const a = readBundle("bundle.multiple-compilations.a.js");
const b = readBundle("bundle.multiple-compilations.b.js");
const c = readBundle("bundle.multiple-compilations.c.js");
const d = readBundle("bundle.multiple-compilations.d.js");
const e = readBundle("bundle.multiple-compilations.e.js");
const f = readBundle("bundle.multiple-compilations.f.js");
const g = readBundle("bundle.multiple-compilations.g.js");
const h = readBundle("bundle.multiple-compilations.h.js");
a.should.equal(expectedCss);
b.should.equal(expectedCss);
c.should.equal(expectedCss);
d.should.equal(expectedCss);
e.should.equal(expectedCss);
f.should.equal(expectedCss);
g.should.equal(expectedCss);
h.should.equal(expectedCss);
})
);
});
describe("source maps", () => {
function buildWithSourceMaps() {
return new Promise((resolve, reject) => {
runWebpack({
entry: path.join(__dirname, "scss", "imports.scss"),
output: {
filename: "bundle.source-maps.js"
},
devtool: "source-map",
module: {
rules: [{
test: /\.scss$/,
use: [
{ loader: testLoader.filename },
{
loader: pathToSassLoader, options: {
sourceMap: true
}
}
]
}]
}
}, err => err ? reject(err) : resolve());
});
}
it("should compile without errors", () => buildWithSourceMaps());
it("should produce a valid source map", () => {
const cwdGetter = process.cwd;
const fakeCwd = path.join(__dirname, "scss");
process.cwd = function () {
return fakeCwd;
};
return buildWithSourceMaps()
.then(() => {
const sourceMap = testLoader.sourceMap;
sourceMap.should.not.have.property("file");
sourceMap.should.have.property("sourceRoot", fakeCwd);
// This number needs to be updated if imports.scss or any dependency of that changes
sourceMap.sources.should.have.length(10);
sourceMap.sources.forEach(sourcePath =>
fs.existsSync(path.resolve(sourceMap.sourceRoot, sourcePath))
);
process.cwd = cwdGetter;
});
});
});
describe("errors", () => {
it("should throw an error in synchronous loader environments", () => {
try {
sassLoader.call(Object.create(loaderContextMock), "");
} catch (err) {
// check for file excerpt
err.message.should.equal("Synchronous compilation is not supported anymore. See https://github.com/webpack-contrib/sass-loader/issues/333");
}
});
it("should output understandable errors in entry files", (done) => {
runWebpack({
entry: pathToSassLoader + "!" + pathToErrorFile
}, (err) => {
err.message.should.match(/Property "some-value" must be followed by a ':'/);
err.message.should.match(/\(line 2, column 5\)/);
err.message.indexOf(pathToErrorFile).should.not.equal(-1);
done();
});
});
it("should output understandable errors of imported files", (done) => {
runWebpack({
entry: pathToSassLoader + "!" + pathToErrorImport
}, (err) => {
// check for file excerpt
err.message.should.match(/Property "some-value" must be followed by a ':'/);
err.message.should.match(/\(line 2, column 5\)/);
err.message.indexOf(pathToErrorFile).should.not.equal(-1);
done();
});
});
it("should output understandable errors when a file could not be found", (done) => {
runWebpack({
entry: pathToSassLoader + "!" + pathToErrorFileNotFound
}, (err) => {
err.message.should.match(/@import "does-not-exist";/);
err.message.should.match(/File to import not found or unreadable: does-not-exist/);
err.message.should.match(/\(line 1, column 1\)/);
err.message.indexOf(pathToErrorFileNotFound).should.not.equal(-1);
done();
});
});
it("should not auto-resolve imports with explicit file names", (done) => {
runWebpack({
entry: pathToSassLoader + "!" + pathToErrorFileNotFound2
}, (err) => {
err.message.should.match(/@import "\.\/another\/_module\.scss";/);
err.message.should.match(/File to import not found or unreadable: \.\/another\/_module\.scss/);
err.message.should.match(/\(line 1, column 1\)/);
err.message.indexOf(pathToErrorFileNotFound2).should.not.equal(-1);
done();
});
});
it("should output a message when `node-sass` is missing", (done) => {
mockRequire.reRequire(pathToSassLoader);
const module = require("module");
const originalResolve = module._resolveFilename;
module._resolveFilename = function (filename) {
if (!filename.match(/node-sass/)) {
return originalResolve.apply(this, arguments);
}
const err = new Error();
err.code = "MODULE_NOT_FOUND";
throw err;
};
runWebpack({
entry: pathToSassLoader + "!" + pathToErrorFile
}, (err) => {
module._resolveFilename = originalResolve;
mockRequire.reRequire("node-sass");
err.message.should.match(/Please install a compatible version/);
done();
});
});
it("should output a message when `node-sass` is an incompatible version", (done) => {
mockRequire.reRequire(pathToSassLoader);
mockRequire("node-sass/package.json", { version: "3.0.0" });
runWebpack({
entry: pathToSassLoader + "!" + pathToErrorFile
}, (err) => {
mockRequire.stop("node-sass");
err.message.should.match(/The installed version of `node-sass` is not compatible/);
done();
});
});
});
});
function readCss(ext, id) {
return fs.readFileSync(path.join(__dirname, ext, "spec", id + ".css"), "utf8").replace(CR, "");
}
function runWebpack(baseConfig, done) {
const webpackConfig = merge({
mode: "development",
output: {
path: path.join(__dirname, "output"),
filename: "bundle.js",
libraryTarget: "commonjs2"
}
}, baseConfig);
webpack(webpackConfig, (webpackErr, stats) => {
const err = webpackErr ||
(stats.hasErrors() && stats.compilation.errors[0]) ||
(stats.hasWarnings() && stats.compilation.warnings[0]);
done(err || null);
});
}
function readBundle(filename) {
delete require.cache[path.resolve(__dirname, `./output/${ filename }`)];
return require(`./output/${ filename }`);
}