Skip to content

Commit 4f05353

Browse files
authored
fix(build-command): report exit code 1 when compilation fails (#239)
1 parent 7fc827a commit 4f05353

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
.vscode/
33
*.swp
44
package-lock.json
5+
.temp

lib/build.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ exports.run = function(dir, additionalConfig) {
157157
if (err) {
158158
return reject(err);
159159
}
160+
const errors = stats.compilation.errors || []
161+
if (errors.length > 0) {
162+
return reject(stats.compilation.errors)
163+
}
160164
resolve(stats);
161165
});
162166
});

lib/build.spec.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const rimraf = require("rimraf");
4+
const build = require("./build");
5+
6+
jest.mock("./config", () => {
7+
const path = require("path");
8+
return {
9+
load: jest.fn(() => ({
10+
build: { functions: path.join(".temp", "build", "lambda") },
11+
})),
12+
loadContext: jest.fn(() => ({ environment: {} })),
13+
};
14+
});
15+
16+
const buildTemp = path.join(".temp", "build");
17+
const functions = path.join(buildTemp, "functions");
18+
19+
const setupFunction = (script, filename) => {
20+
fs.mkdirSync(functions, { recursive: true });
21+
fs.writeFileSync(path.join(functions, filename), script);
22+
};
23+
24+
describe("build", () => {
25+
const functionsBuildOutputDir = require("./config").load().build.functions;
26+
27+
beforeEach(() => {
28+
fs.mkdirSync(buildTemp, { recursive: true });
29+
});
30+
31+
afterEach(() => {
32+
rimraf.sync(buildTemp);
33+
});
34+
35+
describe("run", () => {
36+
it("should return webpack stats on successful build", async () => {
37+
const script = `module.exports = () => console.log("hello world")`;
38+
setupFunction(script, "index.js");
39+
40+
const stats = await build.run(functions);
41+
expect(stats.compilation.errors).toHaveLength(0);
42+
expect(
43+
fs.existsSync(path.join(functionsBuildOutputDir, "index.js"))
44+
).toEqual(true);
45+
});
46+
47+
it("should throw error on complication errors", async () => {
48+
const script = `module.exports = () => console.log("hello`;
49+
setupFunction(script, "index.js");
50+
51+
expect.assertions(1);
52+
53+
await expect(build.run(functions)).rejects.toHaveLength(1);
54+
});
55+
56+
it("should throw error on invalid config", async () => {
57+
const script = `module.exports = () => console.log("hello world")`;
58+
setupFunction(script, "index.js");
59+
60+
expect.assertions(1);
61+
62+
await expect(
63+
build.run(functions, {
64+
userWebpackConfig: "non-existing-webpack-config.js",
65+
})
66+
).rejects.toThrow("Cannot find module");
67+
});
68+
});
69+
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"devDependencies": {
4747
"auto-changelog": "^1.13.0",
4848
"gh-release": "^3.5.0",
49-
"jest": "^23.6.0"
49+
"jest": "^23.6.0",
50+
"rimraf": "^3.0.2"
5051
},
5152
"engines": {
5253
"node": ">=8.0.0"

0 commit comments

Comments
 (0)