Skip to content

Commit

Permalink
Load typescript when --ts is specified and --jsx is not
Browse files Browse the repository at this point in the history
Fix #736
  • Loading branch information
isaacs committed Apr 5, 2021
1 parent 1febbd4 commit 32e45eb
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 36 deletions.
14 changes: 7 additions & 7 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,15 +673,15 @@ const runAllFiles = (options, env, tap, processDB) => {
if (options.flow && flowNode)
options['node-arg'].push('-r', flowNode)

if (options.ts && options.jsx && tsNode && /\.tsx?$/.test(file)) {
debug('ts file', file)
const compilerOpts = JSON.stringify({
...JSON.parse(env.TS_NODE_COMPILER_OPTIONS || '{}'),
jsx: 'react'
})
if (options.ts && tsNode && /\.tsx?$/.test(file)) {
debug('typescript file', file)
const compilerOpts = JSON.parse(env.TS_NODE_COMPILER_OPTIONS || '{}')
if (options.jsx)
compilerOpts.jsx = 'react'

opt.env = {
...env,
TS_NODE_COMPILER_OPTIONS: compilerOpts,
TS_NODE_COMPILER_OPTIONS: JSON.stringify(compilerOpts),
}
const args = [
'-r', tsNode,
Expand Down
125 changes: 119 additions & 6 deletions tap-snapshots/test/run/ts.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,26 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/run/ts.js TAP ts > must match snapshot 1`] = `
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 via cli args ts > must match snapshot 1`] = `
TAP version 13
ok 1 - cli-tests/ts/ok.ts # {time} {
ok 1 - this is fine
Expand All @@ -18,26 +37,120 @@ ok 1 - cli-tests/ts/ok.ts # {time} {
`

exports[`test/run/ts.js TAP ts manually > must match snapshot 1`] = `
exports[`test/run/ts.js TAP via cli args ts, but no tsx > must match snapshot 1`] = `
TAP version 13
ok 1 - cli-tests/mixed/ok.js # {time} {
not ok 1 - cli-tests/tsx/ok.tsx # {time}
---
args:
- -r
- {CWD}/node_modules/ts-node/register/index.js
- cli-tests/tsx/ok.tsx
command: {NODE}
cwd: {CWD}
env:
TS_NODE_COMPILER_OPTIONS: "{}"
exitCode: 1
file: cli-tests/tsx/ok.tsx
stdio:
- 0
- pipe
- 2
timeout: {default}
...
{
1..0 # no tests found
}
1..1
# failed 1 test
# {time}
`

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

exports[`test/run/ts.js TAP via env no ts > must match snapshot 1`] = `
TAP version 13
not ok 1 - cli-tests/ts/ok.ts # {time}
---
args:
- cli-tests/ts/ok.ts
command: {NODE}
cwd: {CWD}
env: {}
exitCode: 1
file: cli-tests/ts/ok.ts
stdio:
- 0
- pipe
- 2
timeout: {default}
...
{
1..0 # no tests found
}
1..1
# failed 1 test
# {time}
`

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

exports[`test/run/ts.js TAP via env ts, but no tsx > must match snapshot 1`] = `
TAP version 13
not ok 1 - cli-tests/tsx/ok.tsx # {time}
---
args:
- -r
- {CWD}/node_modules/ts-node/register/index.js
- cli-tests/tsx/ok.tsx
command: {NODE}
cwd: {CWD}
env:
TS_NODE_COMPILER_OPTIONS: "{}"
exitCode: 1
file: cli-tests/tsx/ok.tsx
stdio:
- 0
- pipe
- 2
timeout: {default}
...
{
1..0 # no tests found
}
1..1
# failed 1 test
# {time}
`

exports[`test/run/ts.js TAP tsx > must match snapshot 1`] = `
exports[`test/run/ts.js TAP via env tsx > must match snapshot 1`] = `
TAP version 13
ok 1 - cli-tests/tsx/ok.tsx # {time} {
ok 1 - this is fine
Expand Down
126 changes: 103 additions & 23 deletions test/run/ts.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,117 @@
process.env.TAP_TS = '1'
process.env.TAP_JSX = '1'

const {
tmpfile,
run,
tap,
t,
} = require('./')

t.test('ts', t => {
const ok = tmpfile(t, 'ts/ok.ts', `
import * as t from ${tap}
t.pass('this is fine')
`)
run([ok], {}, (er, o, e) => {
t.equal(er, null)
t.matchSnapshot(o)
t.end()
t.beforeEach(() => {
delete process.env.TAP_TS
delete process.env.TAP_JSX
})

t.test('via env', t => {
t.test('ts', t => {
process.env.TAP_TS = '1'
const ok = tmpfile(t, 'ts/ok.ts', `
import * as t from ${tap}
t.pass('this is fine')
`)
run([ok], {}, (er, o, e) => {
t.equal(er, null)
t.matchSnapshot(o)
t.end()
})
})

t.test('no ts', t => {
const ok = tmpfile(t, 'ts/ok.ts', `
import * as t from ${tap}
t.pass('this is fine')
`)
run([ok], {}, (er, o, e) => {
t.ok(er)
t.matchSnapshot(o)
t.end()
})
})

t.test('ts, but no tsx', t => {
process.env.TAP_TS = '1'
const ok = tmpfile(t, 'tsx/ok.tsx', `
import * as React from 'react'
import * as t from ${tap}
const div = (<div>Hello</div>)
t.pass('this is fine')
`)
run([ok], {}, (er, o, e) => {
t.ok(er)
t.matchSnapshot(o)
t.end()
})
})

t.test('tsx', t => {
process.env.TAP_JSX = '1'
process.env.TAP_TS = '1'
const ok = tmpfile(t, 'tsx/ok.tsx', `
import * as React from 'react'
import * as t from ${tap}
const div = (<div>Hello</div>)
t.pass('this is fine')
`)
run([ok], {}, (er, o, e) => {
t.equal(er, null)
t.matchSnapshot(o)
t.end()
})
})

t.end()
})

t.test('tsx', t => {
const ok = tmpfile(t, 'tsx/ok.tsx', `
import * as React from 'react'
import * as t from ${tap}
const div = (<div>Hello</div>)
t.pass('this is fine')
`)
run([ok], {}, (er, o, e) => {
t.equal(er, null)
t.matchSnapshot(o)
t.end()
t.test('via cli args', t => {
t.test('ts', t => {
const ok = tmpfile(t, 'ts/ok.ts', `
import * as t from ${tap}
t.pass('this is fine')
`)
run([ok, '--ts'], {}, (er, o, e) => {
t.equal(er, null)
t.matchSnapshot(o)
t.end()
})
})

t.test('ts, but no tsx', t => {
const ok = tmpfile(t, 'tsx/ok.tsx', `
import * as React from 'react'
import * as t from ${tap}
const div = (<div>Hello</div>)
t.pass('this is fine')
`)
run([ok, '--ts'], {}, (er, o, e) => {
t.ok(er)
t.matchSnapshot(o)
t.end()
})
})

t.test('tsx', t => {
const ok = tmpfile(t, 'tsx/ok.tsx', `
import * as React from 'react'
import * as t from ${tap}
const div = (<div>Hello</div>)
t.pass('this is fine')
`)
run([ok, '--ts', '--jsx'], {}, (er, o, e) => {
t.equal(er, null)
t.matchSnapshot(o)
t.end()
})
})

t.end()
})

t.test('ts manually', t => {
Expand Down

0 comments on commit 32e45eb

Please sign in to comment.