Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node:test interop round 1 #905

Merged
merged 7 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ nx run-many --target=prepare -p @tapjs/run
nx run-many --target=prepare -p tap
node \
--loader=ts-node/esm \
--no-warnings=ExperimentalLoader \
--no-warnings \
scripts/default-build.mts
# lastly, run the actual install to do all the linking
npm install
2 changes: 1 addition & 1 deletion scripts/default-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const res = spawnSync(
process.execPath,
[
'--loader=ts-node/esm',
'--no-warnings=ExperimentalLoader',
'--no-warnings',
resolve(dirname(fileURLToPath(import.meta.url)), './default-build.mts'),
],
{ stdio: 'inherit' }
Expand Down
2 changes: 1 addition & 1 deletion scripts/default-build.mts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ spawnSync(
process.execPath,
[
'--loader=ts-node/esm',
'--no-warnings=ExperimentalLoader',
'--no-warnings',
build,
...builtins,
],
Expand Down
2 changes: 1 addition & 1 deletion scripts/version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const res = spawnSync(
process.execPath,
[
'--loader=ts-node/esm',
'--no-warnings=ExperimentalLoader',
'--no-warnings',
resolve(dirname(fileURLToPath(import.meta.url)), './version.mts'),
...process.argv.slice(2)
],
Expand Down
2 changes: 1 addition & 1 deletion src/config/src/jack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export default jack({
Even if excluded, the runner will use some of these plugins
in its own operation.
`,
default: [],
default: [] as string[],
},
})

Expand Down
4 changes: 2 additions & 2 deletions src/core/dist/commonjs/clean-yaml-object.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/dist/commonjs/clean-yaml-object.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/core/dist/esm/clean-yaml-object.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/dist/esm/clean-yaml-object.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/core/src/clean-yaml-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export const cleanYamlObject = (object: { [k: string]: any }) => {
// the shipped compare methods will generally supply
// their own diff, which is much nicer.
if (
res.found &&
res.wanted &&
'found' in res &&
'wanted' in res &&
res.found !== res.wanted &&
!res.diff
) {
Expand Down
16 changes: 2 additions & 14 deletions src/core/tap-snapshots/test/tap.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -416,19 +416,13 @@ Object {
"stderr": String(
Error: poop
---
stack: >
stack: |
TAP.<anonymous> (test/tap.ts:##:##)

After.#callTeardown (...)

TAP.#t.onEOF (...)

TAP.#process (src/test-base.ts:##:##)

TAP.#end (src/test-base.ts:##:##)

TAP.end (src/test-base.ts:##:##)

Timeout.<anonymous> (src/tap.ts:##:##)
at:
fileName: test/tap.ts
Expand Down Expand Up @@ -482,19 +476,13 @@ Object {
"stderr": String(
Error: poop
---
stack: >
stack: |
TAP.<anonymous> (test/tap.ts:##:##)

After.#callTeardown (...)

TAP.#t.onEOF (...)

TAP.#process (src/test-base.ts:##:##)

TAP.#end (src/test-base.ts:##:##)

TAP.end (src/test-base.ts:##:##)

Timeout.<anonymous> (src/tap.ts:##:##)
at:
fileName: test/tap.ts
Expand Down
4 changes: 2 additions & 2 deletions src/docs/content/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ t.pass('this is fine')
When run, that outputs:

```
$ node --loader=ts-node/esm --no-warnings=ExperimentalLoader --enable-source-maps t.mts
$ node --loader=ts-node/esm --no-warnings --enable-source-maps t.mts
TAP version 14
ok 1 - this is fine
1..1
Expand All @@ -136,7 +136,7 @@ However, the `t.matchOnly()` does _not_ pass, because the object
has other properties not specified in the comparison pattern.

```
$ node --loader=ts-node/esm --no-warnings=ExperimentalLoader --enable-source-maps t.mts
$ node --loader=ts-node/esm --no-warnings --enable-source-maps t.mts
TAP version 14
ok 1 - this passes
not ok 2 - this fails
Expand Down
17 changes: 17 additions & 0 deletions src/docs/content/plugins/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ config](https://github.com/tapjs/tapjs/blob/0b315bf/src/snapshot/src/index.ts#L3
which tells it to save new snapshots rather than compare against
stored snapshots.

### `config[field].nodeArgs` Function

If a config field has a `nodeArgs` method, then this method will
be called with the current value of the configuration field, and
is expected to return an array of strings, which will be added to
the `execArgv` of the test process.

This is provided primarily for interoperability with `node:test`
tests, so that for example `--grep` can be translated to
`--test-name-pattern`. However, it's also a way for a plugin to
modify the arguments passed to node, _ahead_ of the main test
script file.

**Do not use this to add --loader or --import arguments.** Those
should be done using the `loader` or `importLoader` exports,
respectively.

## `testFileExtensions`

If an array of strings is exported as `testFileExtensions`, then
Expand Down
7 changes: 6 additions & 1 deletion src/filter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from '@tapjs/core'
import type { Test, TestOpts } from '@tapjs/test'

const reRegExpLiteral = /^\/(.*)\/([a-z]*)$/

/**
* Options added by this plugin
*
Expand Down Expand Up @@ -79,7 +81,7 @@ export class Filter {
this.#grep = !Array.isArray(grep) ? [grep] : grep
} else if (env.TAP_GREP !== undefined) {
this.#grep = env.TAP_GREP.split('\n').map(g => {
const p = g.match(/^\/(.*)\/([a-z]*)$/)
const p = g.match(reRegExpLiteral)
g = p && p[1] ? p[1] : g
const flags = p ? p[2] : ''
return new RegExp(g, flags)
Expand Down Expand Up @@ -205,6 +207,7 @@ export const config = {
short: 'O',
description: `Only run tests with \`{only: true}\` option, or created with
the \`t.only(...)\` function.`,
nodeArgs: (v: boolean) => v ? ['--test-only'] : []
},

/**
Expand Down Expand Up @@ -236,6 +239,8 @@ export const config = {
To specify regular expression flags, format pattern like a
JavaScript RegExp literal. For example: \`/xyz/i\` for
case-insensitive matching.`,
nodeArgs: (value: string[]) =>
value.map(g => `--test-name-pattern=${g}`),
},

/**
Expand Down
24 changes: 21 additions & 3 deletions src/filter/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TestOpts } from '@tapjs/test'
import t, { Test } from 'tap'
import { plugin } from '../dist/esm/index.js'
import { config, plugin } from '../dist/esm/index.js'

t.equal(t.pluginLoaded(plugin), true, 'plugin is loaded')

Expand Down Expand Up @@ -55,7 +55,11 @@ t.test('grep', async t =>

t.test('grep, failSkip:true', async t =>
t.matchSnapshot(
await run({ name: 'grepper', failSkip: true, grep: ['cat', 'purr'] })
await run({
name: 'grepper',
failSkip: true,
grep: ['cat', 'purr'],
})
)
)

Expand Down Expand Up @@ -88,7 +92,11 @@ t.test('only', async t =>

t.test('only, failSkip: true', async t =>
t.matchSnapshot(
await run({ name: 'only the lonely', runOnly: true, failSkip: true })
await run({
name: 'only the lonely',
runOnly: true,
failSkip: true,
})
)
)

Expand Down Expand Up @@ -149,3 +157,13 @@ t.test('get defaults from env', t => {
})
t.end()
})

t.test('assign filter args for node as well', t => {
t.strictSame(config.grep.nodeArgs(['a', 'b']), [
'--test-name-pattern=a',
'--test-name-pattern=b',
])
t.strictSame(config.only.nodeArgs(true), ['--test-only'])
t.strictSame(config.only.nodeArgs(false), [])
t.end()
})
8 changes: 8 additions & 0 deletions src/parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ export class Parser
return
}

if (
typeof diags.duration_ms === 'number' &&
this.#current.time === null
) {
this.#current.time = diags.duration_ms
delete diags.duration_ms
}

this.#current.diag = diags
// we still don't emit the result here yet, to support diags
// that come ahead of buffered subtests.
Expand Down
1 change: 1 addition & 0 deletions src/parser/src/parse-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const parseDirective = (
if (!line.trim()) return false

line = line.replace(OPEN_BRACE_EOL, '').trim()
.replace(/^duration_ms ([0-9.]+)$/, 'time=$1ms')
const time = line.match(
/^time=((?:[1-9][0-9]*|0)(?:\.[0-9]+)?)(ms|s)$/i
)
Expand Down
Loading