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

ERR_UNKNOWN_FILE_EXTENSION when importing certain packages in tests #4952

Closed
4 tasks done
patreeceeo opened this issue Nov 29, 2022 · 4 comments
Closed
4 tasks done
Labels
area: integrations related to working with 3rd party software (e.g., babel, typescript)

Comments

@patreeceeo
Copy link

patreeceeo commented Nov 29, 2022

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Similar, but not the same as, #4900. Nothing in there helped with my situation.

Mocha works fine until I import something (anything, apparently) from this particular package (@xrengine/engine) in my tests. When I do, and only when I do, I get this error:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for .../Code/repo-mocha-issue/MathFunctions.test.ts

Steps to Reproduce

MCVE: https://github.com/patreeceeo/repro-mocha-issue

  1. Clone the above
  2. npm install
  3. npm run test

Expected behavior: tests should run

Actual behavior: error

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/patrick/Code/repo-mocha-issue/MathFunctions.test.ts
    at new NodeError (node:internal/errors:363:5)
    at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
    at Loader.getFormat (node:internal/modules/esm/loader:105:42)
    at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
    at async Loader.import (node:internal/modules/esm/loader:177:17)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async formattedImport (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/nodejs/esm-utils.js:38:28)
    at async Object.exports.loadFilesAsync (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
    at async singleRun (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/cli/run.js:370:5)

Reproduces how often: 100%

Versions

  • The output of mocha --version and node_modules/.bin/mocha --version: 10.1.0 (locally, no global install)
  • The output of node --version: 16.3.0 / 18.12.1
  • Your operating system
    • name and version: 5.15.0-50-generic 20.04.1-Ubuntu
    • architecture (32 or 64-bit): 64
  • Your shell (e.g., bash, zsh, PowerShell, cmd): ZSH
  • Your browser and version (if running browser tests): N/A
  • Any third-party Mocha-related modules (and their versions): [email protected]
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): See above
@patreeceeo
Copy link
Author

Also, I was running mocha with the debug flag set but didn't see anything helpful in there. So that might be a side issue. Here's the complete output:

> [email protected] test
> DEBUG=mocha:* mocha --config .mocharc.js

  mocha:cli:config loadConfig: trying to parse config at .mocharc.js +0ms
  mocha:cli:config parsers: load cwd-relative path: "/home/patrick/Code/repo-mocha-issue/.mocharc.js" +1ms
  mocha:cli:options no config found in /home/patrick/Code/repo-mocha-issue/package.json +0ms
  mocha:cli:mocha loaded opts {
  _: [ '**/*.test.ts' ],
  config: false,
  package: false,
  failZero: false,
  parallel: false,
  require: [ 'tests/mocha.env', 'ts-node/register' ],
  extension: [ 'ts' ],
  bail: true,
  exit: true,
  recursive: true,
  jobs: 1,
  timeout: '2000',
  ignore: [ 'node_modules/**' ],
  diff: true,
  reporter: 'spec',
  slow: 75,
  ui: 'bdd',
  'watch-ignore': [ 'node_modules', '.git' ]
} +0ms
  mocha:cli:mocha running Mocha in-process +2ms
  mocha:cli:cli entered main with raw args [] +0ms
  mocha:plugin-loader registered plugin def "mochaHooks" +0ms
  mocha:plugin-loader registered plugin def "mochaGlobalSetup" +0ms
  mocha:plugin-loader registered plugin def "mochaGlobalTeardown" +0ms
  mocha:plugin-loader registered 3 plugin defs (0 ignored) +0ms
  mocha:cli:run:helpers resolved required file tests/mocha.env to /home/patrick/Code/repo-mocha-issue/tests/mocha.env +0ms
  mocha:cli:run:helpers loaded required module "tests/mocha.env" +5ms
  mocha:cli:run:helpers loaded required module "ts-node/register" +175ms
  mocha:plugin-loader finalized plugins: [Object: null prototype] {} +195ms
  mocha:cli:run post-yargs config {
  package: [Getter/Setter],
  _: [],
  config: false,
  failZero: false,
  'fail-zero': false,
  parallel: false,
  require: [ 'tests/mocha.env', 'ts-node/register' ],
  extension: [ 'ts' ],
  bail: true,
  exit: true,
  recursive: true,
  jobs: 1,
  timeout: '2000',
  ignore: [ 'node_modules/**' ],
  diff: true,
  reporter: 'spec',
  slow: 75,
  ui: 'bdd',
  'watch-ignore': [ 'node_modules', '.git' ],
  watchIgnore: [ 'node_modules', '.git' ],
  spec: [ '**/*.test.ts' ],
  '$0': 'mocha'
} +0ms
  mocha:suite slow 75 +0ms
  mocha:suite timeout 2000 +0ms
  mocha:suite bail true +1ms
  mocha:mocha configured 0 global setup functions +0ms
  mocha:mocha configured 0 global teardown functions +0ms
  mocha:cli:run:helpers test files (in order):  [ '/home/patrick/Code/repo-mocha-issue/MathFunctions.test.ts' ] +0ms
  mocha:cli:run:helpers single run with 1 file(s) +1s
  mocha:mocha set lazy load to true +1s

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/patrick/Code/repo-mocha-issue/MathFunctions.test.ts
    at new NodeError (node:internal/errors:393:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:79:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:121:38)
    at defaultLoad (node:internal/modules/esm/load:81:20)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:605:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)
    at async formattedImport (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/nodejs/esm-utils.js:38:28)
    at async Object.exports.loadFilesAsync (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
    at async singleRun (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/cli/run.js:370:5)

@magicdawn
Copy link

use

NODE_OPTIONS="--loader ts-node/esm" mocha

mocha is using import() to load any test file. so the --require ts-node/register just do not work

see youself in
at async formattedImport (/home/patrick/Code/repo-mocha-issue/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)

@patreeceeo
Copy link
Author

patreeceeo commented Nov 30, 2022

Well, with that I get a different error at least.

TSError: ⨯ Unable to compile TypeScript:
MathFunctions.test.ts:9:6 - error TS2695: Left side of comma operator is unused and has no side effects.

9     (0, assert_1.default)(MathConstants_1.V_000);

Seems like something is wrong with the transpilation of Typescript in the test code now.

@juergba juergba added area: integrations related to working with 3rd party software (e.g., babel, typescript) and removed unconfirmed-bug labels Dec 4, 2022
@juergba
Copy link
Contributor

juergba commented Dec 4, 2022

@patreeceeo I suggest studying this thread TypeStrong/ts-node#1007 which I suppose you haven't read, yet.
Furthermore you could transpile directly using tsc (without ts-node) in order to narrow down the cause.

Node's --experimental-loader (alias --loader) option is still in experimental status, so you will need to study and experiment.

This issue is related to ts-node. I will close this issue unless there is some interesting activity.

@juergba juergba closed this as not planned Won't fix, can't repro, duplicate, stale Dec 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: integrations related to working with 3rd party software (e.g., babel, typescript)
Projects
None yet
Development

No branches or pull requests

3 participants