Skip to content

Commit

Permalink
feat: reject in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
NoriSte committed Oct 15, 2019
1 parent 705fbb5 commit 460e6bf
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 12 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@
"@stryker-mutator/jest-runner": "2.1.0",
"coveralls": "3.0.7",
"cz-conventional-changelog": "3.0.2",
"jest": "24.9.0",
"jest": "^24.9.0",
"jest-watch-typeahead": "^0.4.0",
"semantic-release": "15.13.24"
},
"dependencies": {
"enquirer": "^2.3.2",
"npm": "^6.12.0"
},
"jest": {
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
]
}
}
3 changes: 3 additions & 0 deletions src/__snapshots__/index.error.integration.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should throw an error if the project is not found 1`] = `"No NPM project found"`;
2 changes: 1 addition & 1 deletion src/bin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
"use strict";

require("./index.js")(process.argv);
require("./index.js")(process.argv).catch(console.error);
18 changes: 18 additions & 0 deletions src/index.error.integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
jest.unmock("npm");
jest.mock("npm", () => ({
load: jest.fn(callback => callback()),
config: {
sources: {
project: {
path: undefined
}
}
}
}));

const main = require("./index.js");

it("Should throw an error if the project is not found", async () => {
await expect(main()).rejects.toMatchSnapshot();
});
4 changes: 3 additions & 1 deletion src/index.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

const npm = require("npm");
const main = require("./index.js");
const { readNpmScripts } = require("./read-npm-scripts.js");
const { readNpmScripts, npmProjectLogPrefix } = require("./read-npm-scripts.js");

it("Should work", async () => {
const spy = jest.spyOn(global.console, "log");
const scripts = await readNpmScripts();
const scriptNames = Object.keys(scripts);
const argv = await main();

expect(npm.load).toBeCalled();
expect(spy.mock.calls[0][0].startsWith(npmProjectLogPrefix)).toBe(true);
expect(npm.run).toBeCalled();
expect(scriptNames).toContain(npm.run.mock.calls[0][0][0]);
expect(argv).toBe(npm.run.mock.calls[0][0]);
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const main = argv => {
npm.run(params);
resolve(params);
});
})
.catch(console.error);
});
};

module.exports = main;
10 changes: 6 additions & 4 deletions src/read-npm-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

const npm = require("npm");

const npmProjectLogPrefix = "NPM project: ";
const readNpmScripts = () =>
new Promise(resolve => {
new Promise((resolve, reject) => {
npm.load(() => {
if (!npm.config.sources.project.path) {
throw new Error("No NPM project found");
reject("No NPM project found");
}
const packageJsonPath = npm.config.sources.project.path.replace(".npmrc", "package.json");
console.log(`NPM project: ${packageJsonPath}`);
console.log(npmProjectLogPrefix + packageJsonPath);
const packageJson = require(packageJsonPath);
resolve(packageJson.scripts);
});
});

module.exports = {
readNpmScripts
readNpmScripts,
npmProjectLogPrefix
};
4 changes: 2 additions & 2 deletions test-projects/import-nprr/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test-projects/import-nprr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"nprr": "file:../../nprr-0.0.0-development.tgz"
"nprr": "file:../../nprr-1.0.0.tgz"
}
}

0 comments on commit 460e6bf

Please sign in to comment.