Skip to content

Commit

Permalink
Add --jsx and --ts flags
Browse files Browse the repository at this point in the history
This allows users to do `tap --no-ts --node-arg=--require=my-ts-loader`
to specify their own TypeScript loader that might conflict with tap's
built-in version.

Fix #550
Fix #549
  • Loading branch information
isaacs committed May 17, 2019
1 parent e486938 commit ecfece6
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 16 deletions.
28 changes: 24 additions & 4 deletions bin/jack.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,12 @@ Much more documentation available at: https://www.node-tap.org/
positional arguments are provided.
By default, tap will search for all files ending in
.ts, .js, or .mjs, in a top-level folder named test,
tests, or __tests__, or any file ending in .spec. or .test.
before a js, mjs, or ts extension.
.ts, .tsx, .js, .jsx, or .mjs, in a top-level folder named
test, tests, or __tests__, or any file ending in '.spec.' or
'.test.' before a supported extension.
Ie, the default value for this option is:
((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mj|j|t)s$
((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mjs|[jt]sx?)$
`
}),

Expand Down Expand Up @@ -549,6 +549,18 @@ Much more documentation available at: https://www.node-tap.org/
(Default: true)`,
}),

ts: flag({
default: process.env.TAP_NO_TS !== '1',
description: `Automatically load .ts and .tsx tests with tap's bundled
ts-node module (Default: true)`,
}),

jsx: flag({
default: process.env.TAP_NO_JSX !== '1',
description: `Automatically load .jsx tests using tap's bundled import-jsx
loader (Default: true)`,
}),

'nyc-help': flag({
description: `Print nyc usage banner. Useful for
viewing options for --nyc-arg.`,
Expand Down Expand Up @@ -738,6 +750,14 @@ Much more documentation available at: https://www.node-tap.org/
description: `Set to '1' to disable automatic esm support`
})),

TAP_NO_TS: env(flag({
description: `Set to '1' to disable automatic typescript support`
})),

TAP_NO_JSX: env(flag({
description: `Set to '1' to disable automatic jsx support`
})),

_TAP_COVERAGE_: env(flag({
description: `Reserved for internal use.`
})),
Expand Down
26 changes: 14 additions & 12 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,16 +603,7 @@ const runAllFiles = (options, tap) => {
if (options.jobs > 1)
opt.buffered = isParallelOk(parallelOk, file) !== false

if (file.match(/\.m?js$/)) {
debug('js file', file)
const args = [
...(options.esm ? ['-r', esm] : []),
...options['node-arg'],
file,
...options['test-arg']
]
tap.spawn(node, args, opt, file)
} else if (file.match(/\.tsx?$/)) {
if (options.ts && /\.tsx?$/.test(file)) {
debug('ts file', file)
const compilerOpts = JSON.stringify({
...JSON.parse(process.env.TS_NODE_COMPILER_OPTIONS || '{}'),
Expand All @@ -629,7 +620,7 @@ const runAllFiles = (options, tap) => {
...options['test-arg']
]
tap.spawn(node, args, opt, file)
} else if (file.match(/\.jsx$/)) {
} else if (options.jsx && /\.jsx$/.test(file)) {
debug('jsx file', file)
const args = [
...(options['node-arg']),
Expand All @@ -638,12 +629,23 @@ const runAllFiles = (options, tap) => {
...(options['test-arg']),
]
tap.spawn(node, args, opt, file)
} else if (file.match(/\.tap$/)) {
} else if (/\.jsx$|\.tsx?$|\.m?js$/.test(file)) {
debug('js file', file)
const args = [
...(options.esm ? ['-r', esm] : []),
...options['node-arg'],
file,
...options['test-arg']
]
tap.spawn(node, args, opt, file)
} else if (/\.tap$/.test(file)) {
debug('tap file', file)
tap.spawn('cat', [file], opt, file)
} else if (isexe.sync(options.files[i])) {
debug('executable', file)
tap.spawn(options.files[i], options['test-arg'], opt, file)
} else {
debug('not a test file', file)
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions docs/using-with/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ Name your test files `.ts` and they'll be loaded as TypeScript.

For TypeScript with JSX (ie, TSX), name your files with a `.tsx` extension.

If you want to provide your own TypeScript configs or version, disable tap's
built-in TypeScript support, and use the `--node-arg` argument to load your
TypeScript loader. For example:

```
tap --no-ts --node-arg=--require=my-ts-node/register
```

This is useful in some cases where you might have a mix of JavaScript and
TypeScript in your tests and modules, and want to ensure that the correct
TypeScript compiler is used.

# Using tap with JSX

Name your test files `.jsx` and they'll be loaded as JSX.

To provide your own JSX handling preloader instead of tap's built-in use of
[`import-jsx`](http://npm.im/import-jsx), disable tap's built-in JSX handling,
and provide your own loader via the `--node-arg` option. For example:

```
tap --no-jsx --node-arg=--require=my-jsx-preloader
```

This is useful in some cases where you might have a mix of JavaScript and
JSX in your tests and modules, and want to ensure that the correct JSX
compiler is used.
16 changes: 16 additions & 0 deletions tap-snapshots/test-run-dump-config.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ grep: []
help: false
invert: false
jobs: {number}
jsx: true
lines: 100
node-arg: []
nyc-arg: []
Expand All @@ -43,6 +44,7 @@ test-env: []
test-ignore: /(^|/)cli-tests-[0-9]+/
test-regex: ((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mjs|[jt]sx?)$
timeout: 30
ts: true
version: false
versions: false
watch: false
Expand All @@ -68,6 +70,7 @@ grep: []
help: false
invert: false
jobs: {number}
jsx: true
lines: 100
node-arg: []
nyc-arg: []
Expand All @@ -88,6 +91,7 @@ test-env: []
test-ignore: /(^|/)cli-tests-[0-9]+/
test-regex: ((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mjs|[jt]sx?)$
timeout: 30
ts: true
version: false
versions: false
watch: false
Expand All @@ -113,6 +117,7 @@ grep: []
help: false
invert: false
jobs: {number}
jsx: true
lines: 100
node-arg: []
nyc-arg: []
Expand All @@ -133,6 +138,7 @@ test-env: []
test-ignore: $.
test-regex: ((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mjs|[jt]sx?)$
timeout: 30
ts: true
version: false
versions: false
watch: false
Expand All @@ -158,6 +164,7 @@ grep: []
help: false
invert: false
jobs: {number}
jsx: true
lines: 69
node-arg: []
nyc-arg: []
Expand All @@ -178,6 +185,7 @@ test-env: []
test-ignore: $.
test-regex: ((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mjs|[jt]sx?)$
timeout: 30
ts: true
version: false
versions: false
watch: false
Expand All @@ -203,6 +211,7 @@ grep: []
help: false
invert: false
jobs: {number}
jsx: true
lines: 100
node-arg: []
nyc-arg: []
Expand All @@ -223,6 +232,7 @@ test-env: []
test-ignore: $.
test-regex: ((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mjs|[jt]sx?)$
timeout: 30
ts: true
version: false
versions: false
watch: false
Expand All @@ -248,6 +258,7 @@ grep: []
help: false
invert: false
jobs: {number}
jsx: true
lines: 100
node-arg: []
nyc-arg: []
Expand All @@ -268,6 +279,7 @@ test-env: []
test-ignore: /(^|/)cli-tests-[0-9]+/
test-regex: ((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mjs|[jt]sx?)$
timeout: 0
ts: true
version: false
versions: false
watch: false
Expand Down Expand Up @@ -295,6 +307,7 @@ grep:
help: false
invert: false
jobs: {number}
jsx: true
lines: 100
node-arg:
- --expose-gc
Expand Down Expand Up @@ -324,6 +337,7 @@ test-env: []
test-ignore: /(^|/)cli-tests-[0-9]+/
test-regex: ((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mjs|[jt]sx?)$
timeout: 99
ts: true
version: false
versions: false
watch: false
Expand All @@ -349,6 +363,7 @@ grep: []
help: false
invert: false
jobs: {number}
jsx: true
lines: 100
node-arg: []
nyc-arg: []
Expand All @@ -369,6 +384,7 @@ test-env: []
test-ignore: /(^|/)cli-tests-[0-9]+/
test-regex: ((\\/|^)(tests?|__tests?__)\\/.*|\\.(test|spec))\\.(mjs|[jt]sx?)$
timeout: 30
ts: true
version: false
versions: false
watch: false
Expand Down
19 changes: 19 additions & 0 deletions tap-snapshots/test-run-ts.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ ok 1 - cli-tests/ts/ok.ts # {time} {
`

exports[`test/run/ts.js TAP ts manually > must match snapshot 1`] = `
TAP version 13
ok 1 - cli-tests/mixed/ok.js # {time} {
ok 1 - this is fine
1..1
# {time}
}
ok 2 - cli-tests/mixed/foo.ts # {time} {
ok 1 - this is fine
1..1
# {time}
}
1..2
# {time}
`

exports[`test/run/ts.js TAP tsx > must match snapshot 1`] = `
TAP version 13
ok 1 - cli-tests/tsx/ok.tsx # {time} {
Expand Down
16 changes: 16 additions & 0 deletions test/run/ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,19 @@ t.test('tsx', t => {
t.end()
})
})

t.test('ts manually', t => {
const ok = tmpfile(t, 'mixed/ok.js', `
require('./foo.ts')
`)
const foots = tmpfile(t, 'mixed/foo.ts', `
import * as t from ${tap}
t.pass('this is fine')
`)
const args = [ok, foots, '--no-ts', '--node-arg=--require=ts-node/register']
run(args, {}, (er, o, e) => {
t.equal(er, null)
t.matchSnapshot(o)
t.end()
})
})

0 comments on commit ecfece6

Please sign in to comment.