-
Hi, I'm trying to migrate from tap 16 to tap 18. One problem that I'm facing is that I had a custom Assert that was added to the So I create a new file In the REPL, I type But it still not work, I got a first error:
I fix this error by returning a string, I guess this is an error in the doc. After that I got a second error:
And here I'm clueless, is it possible to create a custom plugin? Is it mandatory to create it in is own package and publish it to NPM? Is there a better way to create a custom assert? Here is the content of myAssert.ts: import { TapPlugin, TestBase } from '@tapjs/core'
export interface HelloSayer {
hello: (who?: string) => string
}
export const plugin: TapPlugin<HelloSayer> = (t: TestBase) => {
return {
hello: (who: string = 'world') => {
console.error(`${t.name} says "Hello, ${who}!"`)
return 'hello'
},
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
In order to preserve the behavior of tap working with both ESM and CommonJS, all plugins have to be able to be loaded with both require and import. You don't have to publish it to npm, but it does have to be a thing in Whatever is the type provided to the import { TapPlugin } from '@tapjs/core'
export const plugin: TapPlugin = (t) => {
return {
hello: (who: string = 'world') => {
console.error(`${t.name} says "Hello, ${who}!"`)
},
}
} To be honest, if you don't need to hook into the test lifecycle stuff or provide an interface to something that's "big" enough to justify being its own package, it may often be a better idea to just define a function that takes a // test/fixtures/utils.ts
import { Test } from 'tap'
export const hello = (t: Test, who: string = 'world') =>
console.error(`${t.name} says "Hello, ${who}!"`) // test/whatever.ts
import t from 'tap'
import { hello } from './fixtures/utils.js'
t.test('hello sayer', t => {
hello(t, 'universe')
t.end()
}) |
Beta Was this translation helpful? Give feedback.
In order to preserve the behavior of tap working with both ESM and CommonJS, all plugins have to be able to be loaded with both require and import.
You don't have to publish it to npm, but it does have to be a thing in
node_modules
with the exports set up properly.tshy
is an easy way to do this, plus if you write it in TS then the plugin type signatures will be preserved in the built Test class.Whatever is the type provided to the
TapPlugin<T>
parameter has to be what the plugin function returns. It can be void or string or whatever, but has to match. You can also just let TS infer the parameter and not provide it, so this would work: