fix the test script
Assistant: You'reYou're using "module": "commonjs" in your tsconfig.json files, but your test script is still using --loader ts-node/esm which is for ESM module support. Since your project compiles to CommonJS, you should avoid using the ESM loader and instead use the standard ts-node register loader for CommonJS.
--loader ts-node/esm is for ESM modules, but you are targeting CommonJS.
--require ts-node/register works with CommonJS to transpile on the fly with ts-node.
Mocha will pick up .ts files and compile them using ts-node during tests.
Make sure ts-node, mocha, and typings like @types/mocha and @types/chai are installed.
Optional improvement
If you want to keep using "module": "ESNext" or "module": "ES2020", you would stick with the ESM loader and adjust your tsconfigs accordingly. But given your current CommonJS target, the simpler approach is above.
Example final test script section in package.json:
This should solve your issue by using the correct ts-node loader that matches your compilation target.
User: ok, I updated the test script, you probably need to update the mocharc as well?
Assistant: YesYes, your updated .mocharc.json looks mostly correct to work with CommonJS and ts-node. Just a few notes:
Make sure the spec path matches where your test files actually live. You wrote "tests/src/**/*.test.ts", which matches your tests folder structure, assuming you keep your test files under tests/src/.
Since you're using tsconfig composite, you might want to tell ts-node to use your tsconfig.json explicitly by adding a config option.
You can also add "timeout" or other mocha options if needed.
Optional: Tell ts-node where the config is (via environment variable or TS_NODE_PROJECT)
If you want to be explicit, you can run mocha with:
TS_NODE_PROJECT=tests/tsconfig.json yarn test
Or hardcode it in mocha config via an environment variable or ts-node option, but that usually isn’t necessary if tsconfig.json files are correctly set up.
Summary:
.mocharc.json with "require": "ts-node/register" is correct for CommonJS.
Make sure the spec points to your test files.
Your test script in package.json can stay as:
"test": "mocha"
since config handles requiring ts-node and test file globs. Or keep your test script with explicit params but avoid duplication.
If you want me to provide a full example with all files polished, just ask!
User: yarn test
yarn run v1.22.22
warning package.json: No license field
yarnworkspace@photon/corebuildwarningpackage.json:Nolicensefieldwarningpackage.json:Nolicensefield tsc
$ mocha --require ts-node/register --extensions ts "src//*.test.ts"
Warning: Cannot find any files matching pattern "tests/src//*.test.ts"
Exception during run: TypeError: Unknown file extension ".ts" for /Users/animaonline/Desktop/src/private/Photon Cloud/tests/src/index.test.ts
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:176:9)
at defaultGetFormat (node:internal/modules/esm/get_format:219:36)
at defaultLoad (node:internal/modules/esm/load:143:22)
at async ModuleLoader.load (node:internal/modules/esm/loader:555:7)
at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:434:45)
at async link (node:internal/modules/esm/module_job:87:21) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
error Command failed with exit code 1.
Assistant: TheThe error you're encountering:
TypeError: Unknown file extension ".ts" for /.../tests/src/index.test.ts
comes from Node trying to run the .ts test files as ES modules, but your project is using CommonJS and mocha doesn't know how to handle .ts files here because it is likely invoking Node with ESM expectations.
Root cause:
Your test script or Mocha is still triggering ESM loading (--loader ts-node/esm) even though you removed that from package.json test script.
Or the Node version (16+?) runs by default in ESM mode for .ts or the test runner is expecting ESM loader but your test files and tsconfig are CommonJS.
4. Run Mocha with Node explicitly in CommonJS mode
Make sure your environment doesn't have NODE_OPTIONS or --experimental-modules flags that force ESM.
Sometimes running:
node --loader ts-node/esm ...
can cause this issue. For CommonJS, do not use --loader ts-node/esm.
5. Ensure Node version (v16 or higher) and ts-node are compatible
If you want to support ESM properly, you'd have to convert your project tsconfig to module: "ESNext" and adjust the tests accordingly, but since you're on CommonJS, remain consistent.
6. Debug workaround: test with verbose extension registration
In rare cases, Mocha tries to run .ts files as ESM when it shouldn't. To avoid this:
Let me know if you want me to help you rewrite your test setup for ESM modules instead, which matches --loader ts-node/esm but requires different tsconfig settings ("module": "esnext"), or adjusting Node flags.
User: yarn test
yarn run v1.22.22
warning package.json: No license field
yarnworkspace@photon/corebuildwarningpackage.json:Nolicensefieldwarningpackage.json:Nolicensefield tsc
$ mocha --require ts-node/register --extensions ts "tests/src/*.test.ts"
Error: No test files found
inside the src folder there is a index.test.ts
Assistant: The error Error: No test files found indicates that Mocha’s glob pattern is not matching your test files.
You mentioned your test file is inside tests/src/index.test.ts, but your glob is:
"tests/src/*.test.ts"
This matches only test files directly inside src (one level only). If your test file might be nested (or even if you want to be safe), use a recursive glob:
If running npx mocha --require ts-node/register --extensions ts "tests/src/**/*.test.ts" still says no tests found, check:
Mocha version (mocha --version) — older versions may have different config behavior.
ts-node and types versions compatibility (ts-node --version, node --version).
If this passes running directly but fails via yarn test, then the problem is with the yarn script or workspace configuration, especially since you have a Yarn monorepo and possibly workspaces.