Skip to content

Commit

Permalink
fix(build-command): report exit code 1 when compilation fails (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah authored Jul 13, 2020
1 parent 7fc827a commit 4f05353
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
.vscode/
*.swp
package-lock.json
.temp
4 changes: 4 additions & 0 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
69 changes: 69 additions & 0 deletions lib/build.spec.js
Original file line number Diff line number Diff line change
@@ -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");
});
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4f05353

Please sign in to comment.