diff --git a/bin/jack.js b/bin/jack.js index edf741d46..f72b8f3fa 100644 --- a/bin/jack.js +++ b/bin/jack.js @@ -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?)$ ` }), @@ -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.`, @@ -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.` })), diff --git a/bin/run.js b/bin/run.js index 316ecbb34..0e69432d1 100755 --- a/bin/run.js +++ b/bin/run.js @@ -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 || '{}'), @@ -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']), @@ -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) } } } diff --git a/docs/using-with/index.md b/docs/using-with/index.md index d75fbc1a7..2f06ea447 100644 --- a/docs/using-with/index.md +++ b/docs/using-with/index.md @@ -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. diff --git a/tap-snapshots/test-run-dump-config.js-TAP.test.js b/tap-snapshots/test-run-dump-config.js-TAP.test.js index 3a439ccf0..552719868 100644 --- a/tap-snapshots/test-run-dump-config.js-TAP.test.js +++ b/tap-snapshots/test-run-dump-config.js-TAP.test.js @@ -23,6 +23,7 @@ grep: [] help: false invert: false jobs: {number} +jsx: true lines: 100 node-arg: [] nyc-arg: [] @@ -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 @@ -68,6 +70,7 @@ grep: [] help: false invert: false jobs: {number} +jsx: true lines: 100 node-arg: [] nyc-arg: [] @@ -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 @@ -113,6 +117,7 @@ grep: [] help: false invert: false jobs: {number} +jsx: true lines: 100 node-arg: [] nyc-arg: [] @@ -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 @@ -158,6 +164,7 @@ grep: [] help: false invert: false jobs: {number} +jsx: true lines: 69 node-arg: [] nyc-arg: [] @@ -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 @@ -203,6 +211,7 @@ grep: [] help: false invert: false jobs: {number} +jsx: true lines: 100 node-arg: [] nyc-arg: [] @@ -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 @@ -248,6 +258,7 @@ grep: [] help: false invert: false jobs: {number} +jsx: true lines: 100 node-arg: [] nyc-arg: [] @@ -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 @@ -295,6 +307,7 @@ grep: help: false invert: false jobs: {number} +jsx: true lines: 100 node-arg: - --expose-gc @@ -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 @@ -349,6 +363,7 @@ grep: [] help: false invert: false jobs: {number} +jsx: true lines: 100 node-arg: [] nyc-arg: [] @@ -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 diff --git a/tap-snapshots/test-run-ts.js-TAP.test.js b/tap-snapshots/test-run-ts.js-TAP.test.js index 22691aed6..3ad2e1109 100644 --- a/tap-snapshots/test-run-ts.js-TAP.test.js +++ b/tap-snapshots/test-run-ts.js-TAP.test.js @@ -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} { diff --git a/test/run/ts.js b/test/run/ts.js index 994b34df6..d9659658e 100644 --- a/test/run/ts.js +++ b/test/run/ts.js @@ -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() + }) +})