Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(build-command): report exit code 1 when compilation fails #239

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 || []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix. Other changes are to facilitate the tests

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