2

I recently discovered the npm version [major|minor|patch] command to automatically bump the package version number and commit the changes. This was a magical little discovery.

Is it possible to run tests prior to the bump & git commit with npm? Trying to avoid having to write a bash script.

I can't find anything on google or stack search "npm version ....." matches too many unrelated results. And there is no info about testing in the docs npm-version

I was begging to write a script before this discovery. To test, bump version, then git commit.

I'm using karma, package.json contains

"scripts": {
    "test": "karma run"
}

While testing the npm version command I added a failing test

it("force fail",function(){
    expect(true).toEqual(1);
});  

So karma run and npm test both result in a failed test run. I was hoping this would stop the version patch, but no luck. I managed to bump and commit with failing tests.

1 Answer 1

3

Adding them to scripts could work, though I really would recommend just defining a function in your shell’s runtime configuration.

"scripts": {
    "test": "karma run",
    "major": "karma run && npm version major && git commit",
    "minor": "karma run && npm version minor && git commit",
    "patch": "karma run && npm version patch && git commit"
}
Sign up to request clarification or add additional context in comments.

3 Comments

npm version is already handling the git commit, and that command wouldn't check for passed tests before continuing to bump the version. I want to test for successful test run before continuing.
@Lex: That’s what && does. Or at least it’s what it ought to do; if it doesn’t, karma is doing its job wrong.
Sorry you're right, it is failing or passing based on karma run fail or pass (for both karma run & npm test). I would have thought putting build scripts/commands in the run time bad practice, for a team or open source projects. It is a good idea though, which solves the immediate requirement. I'm gonna go do that :) alias npmpatch='npm test && npm version patch'. I was hoping there was a mystical flag in the npm command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.