Skip to content

Commit

Permalink
Support Typescript for --before and --after
Browse files Browse the repository at this point in the history
Fix: #690
PR-URL: #751
Credit: @vespaiach
Close: #751
Reviewed-by: @isaacs

EDIT(@isaacs): pattern should be `\.tsx?$`, not `\.ts?$`.  We don't want
to run a `after.t` with typescript, so the `s` is not optional.
  • Loading branch information
vespaiach authored and isaacs committed Mar 5, 2022
1 parent 28d2d03 commit 162c1a8
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 5 deletions.
22 changes: 17 additions & 5 deletions bin/run.js
Expand Up @@ -793,13 +793,14 @@ const runTests = options => {
debug('called tap.end()')
}

const beforeAfter = (env, script) => {
const {status, signal} = spawnSync(process.execPath, [script], {
const beforeAfter = (env, args) => {
const {status, signal} = spawnSync(process.execPath, args, {
env,
stdio: 'inherit',
})

if (status || signal) {
const script = args.length === 1 ? args[0] : args[2]
const msg = `\n# failed ${script}\n# code=${status} signal=${signal}\n`
console.error(msg)
process.exitCode = status || 1
Expand All @@ -815,12 +816,23 @@ const runBeforeAfter = (options, env, tap, processDB) => {
if (processDB && (options.before || options.after))
processDB.writeIndex()

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

if (options.after) {
/* istanbul ignore next - run after istanbul's report */
signalExit(() => beforeAfter(env, options.after), { alwaysLast: true })
signalExit(() => {
if (options.ts && tsNode && /\.tsx?$/.test(options.after)) {
beforeAfter(env, ['-r', tsNode, options.after])
} else {
beforeAfter(env, [options.after])
}
}, { alwaysLast: true })
}
}

Expand Down
61 changes: 61 additions & 0 deletions tap-snapshots/test/run/ts.js.test.cjs
Expand Up @@ -24,6 +24,67 @@ ok 2 - cli-tests/mixed/foo.ts # {time} {
`

exports[`test/run/ts.js TAP via cli args ts --after --before ok > must match snapshot 1`] = `
setup
TAP version 13
ok 1 - cli-tests/ts/ok.ts # {time} {
ok 1 - this is ok
1..1
# {time}
}
1..1
# {time}
teardown
`

exports[`test/run/ts.js TAP via cli args ts --after fail > stderr 1`] = `
{CWD}/cli-tests/ts/teardown.ts:2
throw new Error('fail')
^
Error: fail
{STACK}
# failed cli-tests/ts/teardown.ts
# code=1 signal=null
`

exports[`test/run/ts.js TAP via cli args ts --after fail > stdout 1`] = `
setup
TAP version 13
ok 1 - cli-tests/ts/ok.ts # {time} {
ok 1 - this is ok
1..1
# {time}
}
1..1
# {time}
`

exports[`test/run/ts.js TAP via cli args ts --before fail > stderr 1`] = `
{CWD}/cli-tests/ts/setup.ts:2
throw new Error('fail')
^
Error: fail
{STACK}
# failed cli-tests/ts/setup.ts
# code=1 signal=null
`

exports[`test/run/ts.js TAP via cli args ts --before fail > stdout 1`] = `
`

exports[`test/run/ts.js TAP via cli args ts > must match snapshot 1`] = `
TAP version 13
ok 1 - cli-tests/ts/ok.ts # {time} {
Expand Down
56 changes: 56 additions & 0 deletions test/run/ts.js
Expand Up @@ -111,6 +111,62 @@ t.test('via cli args', t => {
})
})

t.test('ts --after --before ok', t => {
const ok = tmpfile(t, 'ts/ok.ts', `
import * as t from ${tap}
t.pass('this is ok')
`)
const before = tmpfile(t, 'ts/setup.ts', `
console.log('setup')
`)
const after = tmpfile(t, 'ts/teardown.ts', `
console.log('teardown')
`)
run([ok, `--before=${before}`, `--after=${after}`, '--ts'], {}, (er, o) => {
t.equal(er, null)
t.matchSnapshot(o)
t.end()
})
})

t.test('ts --before fail', t => {
const ok = tmpfile(t, 'ts/ok.ts', `
import * as t from ${tap}
t.pass('this is ok')
`)
const before = tmpfile(t, 'ts/setup.ts', `
throw new Error('fail')
`)
const after = tmpfile(t, 'ts/teardown.ts', `
console.log('teardown')
`)
run([ok, `--before=${before}`, `--after=${after}`, '--ts'], {}, (er, o, e) => {
t.not(er, null)
t.matchSnapshot(o, 'stdout')
t.matchSnapshot(e, 'stderr')
t.end()
})
})

t.test('ts --after fail', t => {
const ok = tmpfile(t, 'ts/ok.ts', `
import * as t from ${tap}
t.pass('this is ok')
`)
const before = tmpfile(t, 'ts/setup.ts', `
console.log('setup')
`)
const after = tmpfile(t, 'ts/teardown.ts', `
throw new Error('fail')
`)
run([ok, `--before=${before}`, `--after=${after}`, '--ts'], {}, (er, o, e) => {
t.not(er, null)
t.matchSnapshot(o, 'stdout')
t.matchSnapshot(e, 'stderr')
t.end()
})
})

t.end()
})

Expand Down

0 comments on commit 162c1a8

Please sign in to comment.