Skip to content

Commit

Permalink
test: fix npm run test
Browse files Browse the repository at this point in the history
Fixes two issues that enable running tests downstream in nodejs:
- Running tests using `npm run-script test` instead of the `npm test`
alias will result in a failure since `test/lib/npm.js` have an assertion
for "test" being the command only. This changes it so that this
particular test also accepts "run-script" as the command.
- Explicitly enable colors for tap output so that running the test suite
in a non-tty environment will result in output that matches coloured
snapshots.

PR-URL: npm#2225
Credit: @ruyadorno
Close: npm#2225
Reviewed-by: @darcyclarke
  • Loading branch information
ruyadorno committed Nov 27, 2020
1 parent f51e506 commit 14c3f6f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
"Remove the 'files' below once we're done porting old tests over"
],
"tap": {
"color": 1,
"files": "test/{lib,bin}",
"coverage-map": "test/coverage-map.js",
"check-coverage": true,
Expand Down
15 changes: 10 additions & 5 deletions test/lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ const event = process.env.npm_lifecycle_event
for (const env of Object.keys(process.env).filter(e => /^npm_/.test(e))) {
if (env === 'npm_command') {
// should only be running this in the 'test' or 'run-script' command!
// if the lifecycle event is 'test', then it'll be 'test', otherwise
// it should always be run-script. Of course, it'll be missing if this
// test is just run directly, which is also acceptable.
const cmd = event === 'test' ? 'test' : 'run-script'
t.match(process.env[env], cmd)
// if the lifecycle event is 'test', then it'll be either 'test' or 'run',
// otherwise it should always be run-script. Of course, it'll be missing
// if this test is just run directly, which is also acceptable.
if (event === 'test') {
t.ok(
['test', 'run-script'].some(i => i === event),
'should match "npm test" or "npm run test"'
)
} else
t.match(process.env[env], 'run-script')
}
delete process.env[env]
}
Expand Down

0 comments on commit 14c3f6f

Please sign in to comment.