Skip to content

Commit c915fdb

Browse files
committed
[INTERNAL] Add missing tests for Compiler.js
1 parent 2cedd41 commit c915fdb

File tree

4 files changed

+105
-15
lines changed

4 files changed

+105
-15
lines changed

lib/Compiler.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,12 @@ class Compiler {
4141
console.log("File not found: " + pathname);
4242
callback({type: "File", message: "Could not find file at path '" + pathname + "'"});
4343
} else {
44-
try {
45-
// Save import mapping to calculate full import paths later on
46-
this.mFileMappings[currentFileInfo.rootFilename] =
47-
this.mFileMappings[currentFileInfo.rootFilename] || {};
48-
this.mFileMappings[currentFileInfo.rootFilename][pathname] = result.path;
49-
50-
handleDataAndCallCallback(pathname, result.content);
51-
} catch (e) {
52-
// eslint-disable-next-line no-console
53-
console.log(e);
54-
callback(e);
55-
}
44+
// Save import mapping to calculate full import paths later on
45+
this.mFileMappings[currentFileInfo.rootFilename] =
46+
this.mFileMappings[currentFileInfo.rootFilename] || {};
47+
this.mFileMappings[currentFileInfo.rootFilename][pathname] = result.path;
48+
49+
handleDataAndCallCallback(pathname, result.content);
5650
}
5751
} catch (err) {
5852
// eslint-disable-next-line no-console

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
},
2424
"scripts": {
2525
"lint": "eslint ./",
26-
"unit": "mocha test/*.js test/lib/**/*.js",
27-
"unit-debug": "mocha --inspect --inspect-brk test/*.js test/lib/**/*.js",
26+
"unit": "mocha test/*.js test/lib/**",
27+
"unit-debug": "mocha --inspect --inspect-brk test/*.js test/lib/**",
2828
"coverage": "nyc npm run unit",
2929
"test": "npm run lint && npm run coverage && npm run depcheck",
3030
"preversion": "npm test",

test/lib/Compiler.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* eslint-env mocha */
2+
3+
const assert = require("assert");
4+
const sinon = require("sinon");
5+
6+
// tested module
7+
const Compiler = require("../../lib/Compiler");
8+
9+
describe("Compiler#createFileHandler", function() {
10+
before(() => {
11+
sinon.stub(console, "log");
12+
});
13+
after(() => {
14+
sinon.restore();
15+
});
16+
17+
it("should propagate errors via callback function (TypeError)", async function() {
18+
const compiler = new Compiler({
19+
options: {
20+
rootPaths: ["foo"]
21+
},
22+
fileUtils: {},
23+
});
24+
25+
const file = "someFile";
26+
const currentFileInfo = {
27+
currentDirectory: undefined // This will cause a TypeError when calling path.join
28+
};
29+
const handleDataAndCallCallback = sinon.stub();
30+
const callback = sinon.stub();
31+
32+
await compiler.fnFileHandler(file, currentFileInfo, handleDataAndCallCallback, callback);
33+
34+
assert.equal(handleDataAndCallCallback.callCount, 0);
35+
assert.equal(callback.callCount, 1);
36+
assert.equal(callback.getCall(0).args.length, 1);
37+
assert.equal(callback.getCall(0).args[0].name, "TypeError");
38+
assert.equal(callback.getCall(0).args[0].message,
39+
`The "path" argument must be of type string. Received undefined`
40+
);
41+
});
42+
it("should propagate errors via callback function (File not found)", async function() {
43+
const compiler = new Compiler({
44+
options: {
45+
rootPaths: ["foo"]
46+
},
47+
fileUtils: {
48+
readFile: sinon.stub().resolves(null)
49+
},
50+
});
51+
52+
const file = "someFile";
53+
const currentFileInfo = {
54+
currentDirectory: "someFolder"
55+
};
56+
const handleDataAndCallCallback = sinon.stub();
57+
const callback = sinon.stub();
58+
59+
await compiler.fnFileHandler(file, currentFileInfo, handleDataAndCallCallback, callback);
60+
61+
assert.equal(handleDataAndCallCallback.callCount, 0);
62+
assert.equal(callback.callCount, 1);
63+
assert.equal(callback.getCall(0).args.length, 1);
64+
assert.equal(callback.getCall(0).args[0].type, "File");
65+
assert.equal(callback.getCall(0).args[0].message,
66+
`Could not find file at path 'someFolder/someFile'`
67+
);
68+
});
69+
it("should propagate errors via callback function (error within handleDataAndCallCallback)", async function() {
70+
const compiler = new Compiler({
71+
options: {
72+
rootPaths: ["foo"]
73+
},
74+
fileUtils: {
75+
readFile: sinon.stub().resolves({
76+
path: "", content: ""
77+
})
78+
},
79+
});
80+
81+
const file = "someFile";
82+
const currentFileInfo = {
83+
currentDirectory: "someFolder"
84+
};
85+
const handleDataAndCallCallback = sinon.stub().throws(new Error("Error from handleDataAndCallCallback"));
86+
const callback = sinon.stub();
87+
88+
await compiler.fnFileHandler(file, currentFileInfo, handleDataAndCallCallback, callback);
89+
90+
assert.equal(handleDataAndCallCallback.callCount, 1);
91+
assert.equal(callback.callCount, 1);
92+
assert.equal(callback.getCall(0).args.length, 1);
93+
assert.equal(callback.getCall(0).args[0].message,
94+
`Error from handleDataAndCallCallback`
95+
);
96+
});
97+
});

test/lib/themingParameters/dataUri.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-env mocha */
22

33
const assert = require("assert");
4-
// const sinon = require("sinon");
54

65
// tested module
76
const themingParametersDataUri = require("../../../lib/themingParameters/dataUri");

0 commit comments

Comments
 (0)