ts-mocha: TypeError: Unknown file extension ".ts" - esmodules support

Original post in Anchor repo: [(https://github.com/project-serum/anchor/issues/1286)]

Anchor.toml [script] command: test = “yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts”

The problem is the m1.ts file cannot import functions from another ts file!!!??? in my utils.ts

export const log1 = console.log;

in my m1.ts file:

import { log1 } from './utils';
describe('scenario1', async () => {
  it('initialize', async () => {
    log1('\n---------== init');
  });
});

the imported log1 function or any other function will cause the Unknown file extension “.ts” error!!??

my local package dependencies: “mocha”: “^9.1.3”, “ts-mocha”: “^9.0.0-alpha1”, “ts-node”: “^10.4.0”, “typescript”: “^4.5.4”

Please advise. Thank you unknown file extension ts2

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 16
  • Comments: 39 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Hello everyone,I have the same problem and I think the problem was caused by tsconfig.json

// ❌ when module = esnext
{
	"compilerOptions": {
    	"module": "esnext"
    }
}
// ✅ change module to commonjs,then everything is ok
{
	"compilerOptions": {
    	"module": "CommonJS"
    }
}

Hope this trick can help.

I’ve tried a variety of solutions in this thread. These did not work:

  1. Changing tsconfig.json’s module type to commonjs was not a fix. (This was already the case.)
  2. Changing "type": "module" in package.json was not a fix. (Wasn’t the case.)
  3. Adding loader=ts-node/esm did not fix things.
  4. Changing mocha / ts-mocha / ts-node versions did not fix things.

So, I’m kind of at a loss.

Try ts-mocha -n loader=ts-node/esm -p tsconfig.json tests/**/*.test.ts via npm scripts, it works for me! (Optional) Check npx ts-mocha --help for more details of this option.

In my case, on of my dependencies (nanoid) added "type": "module" to their package.json. I had to downgrade nanoid to fix mocha.

create .mocharc.json and

{ 
    "node-option": [
	 "loader=ts-node/esm"
     ]
}

Hello Everyone, thanks for multiple useful contributions here in this issue, I’ll try to look into this issue today and propose some solutions, also I will try to include a special test case scenario on a branch in the repo to help with debugging. Cheers!

Pretty sure this is due to type: module in package.json (possibly in a dep). See the ts-node issue for this.

Not sure how to mangle ts-mocha into doing the right thing here? Something like node --loader=ts-node/esm ts-mocha ... should be enough, but doesn’t work for me, perhaps ts-mocha needs to support --loader itself?

I have created a branch with a reproduction test case for experimentation here: https://github.com/piotrwitek/ts-mocha/tree/support-for-node-es-modules

After some research, the issue was indeed introduced by ts-node dependency not handling esmodules and I found some solutions we could try to implement:

More details on ts-node esm support:

I won’t have time to work on it anytime soon, but will try to help and accept PRs for this. Cheers!

Thank you for your post… but “ts-mocha”: “9.0.0” results in the same error as above.

create .mocharc.json and

{ 
    "node-option": [
	 "loader=ts-node/esm"
     ]
}

I tried adding this but then get:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/<path-to-project>/node_modules/ts-node/esm' imported from /<path-to-project>/
    at new NodeError (node:internal/errors:405:5)
    at finalizeResolution (node:internal/modules/esm/resolve:324:11)
    at moduleResolve (node:internal/modules/esm/resolve:943:10)
    at defaultResolve (node:internal/modules/esm/resolve:1129:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ESMLoader.import (node:internal/modules/esm/loader:524:22)
    at initializeLoader (node:internal/process/esm_loader:75:58)
    at loadESM (node:internal/process/esm_loader:90:11) {
  code: 'ERR_MODULE_NOT_FOUND'
}

node 18.17.0

i switched to vitest, almost there

@webJose So, since I commented on this, I’ve switched test runners to Vitest in every scenario. As far as I can tell, Jest / Mocha just don’t work, or don’t work consistently, and Vitest works with very little configuration.

came across this today

"ts-mocha": "^10.0.0"
➜  fireproof git:(main) ✗ pnpm test

> @fireproof/core@0.5.6 test /Users/nikos/WebstormProjects/fireproof-nikos-clone/packages/fireproof
> npm run test:mocha


> @fireproof/core@0.5.6 test:mocha
> ts-mocha test/*.test.ts


TypeError: Unknown file extension ".ts" for /Users/nikos/WebstormProjects/fireproof-nikos-clone/packages/fireproof/test/clock.test.ts
    at new NodeError (node:internal/errors:399: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:64:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34)```

![Uploading image.png…]()

Hi @piotrwitek , thank you for your quick reply. Please take the following code: https://github.com/AuroraLantean/SolanaAnchorDemo2 Thank you!