From 4f0535360b9597e6b6d85c979c7457bc453acd39 Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Mon, 13 Jul 2020 17:24:02 +0300 Subject: [PATCH] fix(build-command): report exit code 1 when compilation fails (#239) --- .gitignore | 1 + lib/build.js | 4 +++ lib/build.spec.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 lib/build.spec.js diff --git a/.gitignore b/.gitignore index d1fa9840..0019a93b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ .vscode/ *.swp package-lock.json +.temp \ No newline at end of file diff --git a/lib/build.js b/lib/build.js index bf698910..cee2e914 100644 --- a/lib/build.js +++ b/lib/build.js @@ -157,6 +157,10 @@ exports.run = function(dir, additionalConfig) { if (err) { return reject(err); } + const errors = stats.compilation.errors || [] + if (errors.length > 0) { + return reject(stats.compilation.errors) + } resolve(stats); }); }); diff --git a/lib/build.spec.js b/lib/build.spec.js new file mode 100644 index 00000000..b0fa5213 --- /dev/null +++ b/lib/build.spec.js @@ -0,0 +1,69 @@ +const fs = require("fs"); +const path = require("path"); +const rimraf = require("rimraf"); +const build = require("./build"); + +jest.mock("./config", () => { + const path = require("path"); + return { + load: jest.fn(() => ({ + build: { functions: path.join(".temp", "build", "lambda") }, + })), + loadContext: jest.fn(() => ({ environment: {} })), + }; +}); + +const buildTemp = path.join(".temp", "build"); +const functions = path.join(buildTemp, "functions"); + +const setupFunction = (script, filename) => { + fs.mkdirSync(functions, { recursive: true }); + fs.writeFileSync(path.join(functions, filename), script); +}; + +describe("build", () => { + const functionsBuildOutputDir = require("./config").load().build.functions; + + beforeEach(() => { + fs.mkdirSync(buildTemp, { recursive: true }); + }); + + afterEach(() => { + rimraf.sync(buildTemp); + }); + + describe("run", () => { + it("should return webpack stats on successful build", async () => { + const script = `module.exports = () => console.log("hello world")`; + setupFunction(script, "index.js"); + + const stats = await build.run(functions); + expect(stats.compilation.errors).toHaveLength(0); + expect( + fs.existsSync(path.join(functionsBuildOutputDir, "index.js")) + ).toEqual(true); + }); + + it("should throw error on complication errors", async () => { + const script = `module.exports = () => console.log("hello`; + setupFunction(script, "index.js"); + + expect.assertions(1); + + await expect(build.run(functions)).rejects.toHaveLength(1); + }); + + it("should throw error on invalid config", async () => { + const script = `module.exports = () => console.log("hello world")`; + setupFunction(script, "index.js"); + + expect.assertions(1); + + await expect( + build.run(functions, { + userWebpackConfig: "non-existing-webpack-config.js", + }) + ).rejects.toThrow("Cannot find module"); + }); + }); +}); diff --git a/package.json b/package.json index 2d115652..e60cfb90 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,8 @@ "devDependencies": { "auto-changelog": "^1.13.0", "gh-release": "^3.5.0", - "jest": "^23.6.0" + "jest": "^23.6.0", + "rimraf": "^3.0.2" }, "engines": { "node": ">=8.0.0"