tape-testing / tape

tap-producing test harness for node and browsers
MIT License
5.77k stars 307 forks source link

Failed to test module package with ts-node #599

Closed GiviMAD closed 7 months ago

GiviMAD commented 11 months ago

Hi, I have a package tested with tape and I'm having problem to change my package to a module.

The problem seems to be that I'm using the ts-node register to invoke the tests execution and it seems to be incompatible with the current "importOrRequire" implementation, as it requires the file to have a ".js" extension but when running with ts-node the reported file extension is ".ts".

Can ".ts" and ".mts" be added there as possible extensions for a module?

Thank you in advance, and thanks for this cool library.

ljharb commented 11 months ago

That shouldn't be required; if the extension is anything besides mjs or js, it'll just use require, which ts-node should be intercepting.

GiviMAD commented 11 months ago

I'm running the tests like:

c8 --check-coverage --lines 100 tape -r ts-node/register/transpile-only ./index.spec.ts

As soon as I change the package.json type to module I get this error:

/Users/****/node_modules/ts-node/dist-raw/node-internal-errors.js:46
  const err = new Error(getErrRequireEsmMessage(filename, parentPath, packageJsonPath))
              ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/***/index.spec.ts
require() of ES modules is not supported.
require() of /Users/***/index.spec.ts from /Users/***/node_modules/tape/bin/import-or-require.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/***/package.json.

    at createErrRequireEsm (/Users/***/node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (/Users/***/node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (/Users/***/node_modules/ts-node/src/index.ts:1610:5)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at importOrRequire (/Users/***/node_modules/tape/bin/import-or-require.js:14:2)
    at /Users/***/node_modules/tape/bin/tape:96:8
    at Array.reduce (<anonymous>) {
  code: 'ERR_REQUIRE_ESM'
}

Should I run the test in a different way?

ljharb commented 11 months ago

You're using type module, though - why?

ljharb commented 7 months ago

Closing due to lack of response.