Skip to content

Commit

Permalink
Pass node-arg options to --before/--after scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 16, 2023
1 parent c14c887 commit e0e7184
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
8 changes: 4 additions & 4 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,19 +822,19 @@ const runBeforeAfter = (options, env, tap, processDB) => {

if (options.before) {
if (options.ts && tsNode && /\.tsx?$/.test(options.before)) {
beforeAfter(env, ['-r', tsNode, options.before])
beforeAfter(env, ['-r', tsNode, ...options['node-arg'], options.before])
} else {
beforeAfter(env, [options.before])
beforeAfter(env, [...options['node-arg'], options.before])
}
}

if (options.after) {
/* istanbul ignore next - run after istanbul's report */
signalExit(() => {
if (options.ts && tsNode && /\.tsx?$/.test(options.after)) {
beforeAfter(env, ['-r', tsNode, options.after])
beforeAfter(env, ['-r', tsNode, ...options['node-arg'], options.after])
} else {
beforeAfter(env, [options.after])
beforeAfter(env, [...options['node-arg'], options.after])
}
}, { alwaysLast: true })
}
Expand Down
7 changes: 7 additions & 0 deletions tap-snapshots/test/run/before-after.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ exports[`test/run/before-after.js TAP basic > stderr 1`] = `
`

exports[`test/run/before-after.js TAP basic > stdout 1`] = `
this is fine
slow
TAP version 13
# Subtest: cli-tests/t1.js
this is fine
ok 1 - this is fine
1..1
# {time}
ok 1 - cli-tests/t1.js # {time}
# Subtest: cli-tests/t2.js
this is fine
# Subtest: sub
ok 1 - this is fine
1..1
Expand All @@ -29,6 +32,7 @@ ok 1 - cli-tests/t1.js # {time}
ok 2 - cli-tests/t2.js # {time}
# Subtest: cli-tests/t3.js
this is fine
# Subtest: sub
not ok 1 - not fine
---
Expand All @@ -54,6 +58,8 @@ ok 2 - cli-tests/t2.js # {time}
not ok 3 - cli-tests/t3.js # {time}
---
args:
- -r
- ./cli-tests/loggy.js
- cli-tests/t3.js
command: {NODE}
cwd: {CWD}
Expand All @@ -70,6 +76,7 @@ not ok 3 - cli-tests/t3.js # {time}
1..3
# failed 1 of 3 tests
# {time}
this is fine
ok
`
Expand Down
5 changes: 4 additions & 1 deletion test/run/before-after.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const {
} = require('./')

const ok = tmpfile(t, 'ok.js', `console.log('ok')`)
const loggy = tmpfile(t, 'loggy.js', `
console.log('this is fine')
`)
const fail = tmpfile(t, 'fail.js', `
throw new Error('fail')
`)
Expand All @@ -31,7 +34,7 @@ t.test('sub', async t => t.fail('not fine'))

t.test('basic', t => {
t.plan(3)
run([`--before=${slow}`, `--after=${ok}`, ok, slow, t1, t2, t3], {}, (er, o, e) => {
run([`--node-arg=-r`, `--node-arg=./${loggy}`, `--before=${slow}`, `--after=${ok}`, ok, slow, t1, t2, t3], {}, (er, o, e) => {
t.ok(er, 'error')
t.matchSnapshot(o, 'stdout')
t.matchSnapshot(e, 'stderr')
Expand Down

0 comments on commit e0e7184

Please sign in to comment.