Skip to content

Commit 9cfb711

Browse files
committed
improve tests
1 parent d0a6e81 commit 9cfb711

5 files changed

Lines changed: 75 additions & 20 deletions

File tree

‎test/Errors.test.js‎

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("Errors", () => {
1717
callback();
1818
},
1919
writeFile: function(name, content, callback) {
20-
files[name] = content.toString("utf-8");
20+
files[name] = content.toString("utf8");
2121
callback();
2222
}
2323
};
@@ -42,6 +42,14 @@ describe("Errors", () => {
4242
callback(stats.errors, stats.warnings);
4343
});
4444
}
45+
function getErrorsPromise(options, callback) {
46+
return new Promise((resolve, reject) => {
47+
getErrors(options, (errors, warnings) => {
48+
callback(errors, warnings);
49+
resolve();
50+
});
51+
});
52+
};
4553
it("should throw an error if file doesn't exist", (done) => {
4654
getErrors({
4755
mode: "development",
@@ -168,23 +176,59 @@ describe("Errors", () => {
168176
done();
169177
});
170178
});
171-
it("should show loader name when emit an error from a loader", (done) => {
172-
getErrors({
173-
mode: "development",
174-
entry: "./entry-point",
175-
module: {
176-
rules: [{
177-
test: /entry-point\.js$/,
178-
use: path.resolve(base, "./emit-error-loader")
179-
}]
180-
}
179+
it.only("should show loader name when emit/throw an error or warning from a loader", () => {
180+
return Promise.all([
181+
getErrorsPromise({
182+
mode: "development",
183+
entry: "./not-a-json.js",
184+
module: {
185+
rules: [{
186+
test: /not-a-json\.js$/,
187+
use: [
188+
"json-loader",
189+
{
190+
loader: path.resolve(base, "./emit-error-loader")
191+
}
192+
]
193+
}]
194+
}
181195

182-
}, (errors, warnings) => {
183-
errors.length.should.be.eql(1);
184-
warnings.length.should.be.eql(1);
185-
errors[0].split("\n")[1].should.match(/^Module build failed\(from emit-error-loader\.js\)/);
186-
warnings[0].split("\n")[1].should.match(/^Module warning\(from emit-error-loader\.js\)/);
187-
done();
188-
});
196+
}, (errors, warnings) => {
197+
warnings.length.should.be.eql(1);
198+
warnings[0].split("\n")[1].should.match(/^Module Warning \(from emit-error-loader\)/);
199+
errors.length.should.be.eql(2);
200+
errors[0].split("\n")[1].should.match(/^Module Error \(from emit-error-loader\)/);
201+
errors[1].split("\n")[1].should.match(/^Module build failed \(from json-loader\)/);
202+
}),
203+
getErrorsPromise({
204+
mode: "development",
205+
entry: "./entry-point.js",
206+
module: {
207+
rules: [{
208+
test: /entry-point\.js$/,
209+
use: path.resolve(base, "./async-error-loader")
210+
}]
211+
}
212+
213+
}, (errors, warnings) => {
214+
errors.length.should.be.eql(1);
215+
errors[0].split("\n")[1].should.match(/^Module build failed \(from async-error-loader\)/);
216+
}),
217+
getErrorsPromise({
218+
mode: "development",
219+
entry: "./entry-point.js",
220+
module: {
221+
rules: [{
222+
test: /entry-point\.js$/,
223+
use: path.resolve(base, "./throw-error-loader")
224+
}]
225+
}
226+
227+
}, (errors, warnings) => {
228+
errors.length.should.be.eql(1);
229+
errors[0].split("\n")[1].should.match(/^Module build failed \(from throw-error-loader\)/);
230+
}),
231+
232+
]);
189233
});
190234
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function(source) {
2+
const callback = this.async();
3+
const error = new Error("this is a callback error");
4+
callback(error, source);
5+
};
6+
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = function(source) {
2-
this.emitWarning(new Error("a warning from loader"));
3-
this.emitError(new Error("a error from loader"));
2+
this.emitWarning(new Error("this is a warning"));
3+
this.emitError(new Error("this is an error"));
44
return source;
55
};

‎test/fixtures/errors/not-a-json.js‎

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = function(source) {
2+
throw new Error("this is a threw error");
3+
};
4+
5+

0 commit comments

Comments
 (0)