Closed EvHaus closed 6 months ago
tape just runs node, so the only extensions it understands by default are js, cjs, or mjs - so if you want jsx/tsx/ts to work directly, you'll have to --require
or --import
something that can handle those, like babel-node or ts-node.
@EvHaus have you had any progress on using a loader here, or transpiling beforehand?
I haven't spent any more time with it. Would you recommend the transpiling route or the ts-node
route? I'm guessing ts-node
would be a more realistic workflow from a DX perspective.
For performance, I'd recommend pre-transpiling; for DX, ts-node.
It might be good to do both, because the delta wouldn't be due to tape.
I just turned my tests into TypeScript and in my case it was as easy as:
index.test.ts
in tsconfig.jsontsc && tape index.test.js
Not great because it requires running TypeScript first, but in my case I was already doing it: https://github.com/fregante/text-field-edit/pull/29
Got it working. The trick was to:
ts-node
version 10.9.2
ts-node --esm
-P tsconfig.json
to ts-node
"type": "module"
from package.json
Glad you got it working! type module just makes things worse anyways :-)
Got it working. Remove "type": "module" from package.json
You got it working by not doing what the issue suggested. Now it's not ESM anymore. It'd be good to keep the issue open. Likewise in my case it worked by not having TS anymore.
Removing type module means that only .mjs
is ESM (as it should be).
@fregante maybe i'm confused, help me understand why this should be open and what we need to fix?
what we need to fix?
Documenting how to deal with ESM/TS given that all the tries with ts-node above failed
@fregante if you know the answer to that, a PR to the docs would be most helpful :-)
I prefer to avoid native ESM or native TS, so I'm not the expert here.
tape just runs node, so the only extensions it understands by default are js, cjs, or mjs - so if you want jsx/tsx/ts to work directly, you'll have to
--require
or--import
something that can handle those, like babel-node or ts-node.
@ljharb Would you be able to provide a (non working) example of running tape using node and passing nodes --import
flag? I tried running node_modules/tape/bin/tape --help
and node_modules/.bin/tape --help
but there is no help output and I have no idea in what order I am meant to pass arguments or flags.
The documentation mentions the --require
flag and since you mention import, I tried --import
but I'm not sure I am even running tape correctly because running tape as above with an empty --require
or --import
flag doesn't return an error.
Tape: 5.7.5
Nodejs: 22
OS: Linux
@darcyrush that sounds like a great idea to add to https://github.com/tape-testing/tape/tree/HEAD/example !
Specifically, it's something like NODE_OPTIONS='--import=ts-node' tape '**/*.ts'
, for example.
@ljharb Thank you for the example. I tried to get ts-node
working with a work-around I stumbled across in a ts-node issue, but I still wasn't successful.
I tried the following;
NODE_OPTIONS='--import ./ts-node.register.mjs' node_modules/tape/bin/tape 'test/**/*.test.ts'`
With ts-node.register.mjs
being
import { pathToFileURL } from "node:url";
import { register } from "node:module";
register("ts-node/esm", pathToFileURL("./"));
I thought that would transpile to JS before reaching tape, but I'm still not really sure about how the 'piping' works.
Managed it to work with this package.json entry for npm run test
:
"test": "cross-env NODE_OPTIONS='--import=tsx' tape test/*.ts",
I'm trying to add
tape
to https://github.com/EvHaus/test-runner-benchmarks as a follow up to @ljharb's Twitter post here, but I'm having a really hard time figuring out how to get it to work with a Typescript codebase.My test file looks like this:
And my
package.json
has"type": "module"
set.1st Attempt (
tape
)If I run:
I get:
2nd Attempt (
ts-node
)If I switch to
ts-node
and use:I get:
3rd Attempt (
ts-node --esm
)If I try with
ts-node --esm
, ala:I get:
4th Attempt (
tape-es
)If I try with tape-es:
I get:
5th Attempt (
ts-node
&tape-es
)If I combine
ts-node
andtape-es
, ala:I get:
I know how much you hate the ESM drama, but I'm out of ideas and I'd really love to add
tape
to the benchmarks (so it can also serve as possible documentation for those who want to try it). Any pointers?