User: I have a yarn ts monorepo, it has these subdirectories   core server tests  here is my root tsconfig  { "compilerOptions": { "target": "es2020", "module": "commonjs", "moduleResolution": "node", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "baseUrl": ".", "paths": { "@photon/core/*": ["core/src/*"], "@photon/server/*": ["server/src/*"] }, "outDir": "dist", "sourceMap": true, "inlineSources": true, "rootDir": ".", "declaration": true, "composite": true, "skipLibCheck": true }, "references": [ { "path": "./core" }, { "path": "./server" }, { "path": "./tests" } ] }   and here is tsconfig for core  { "extends": "../tsconfig.json", "compilerOptions": { "outDir": "./dist", "rootDir": "./src", "sourceMap": true }, "include": ["src/**/*"] }   and tests  { "extends": "../tsconfig.json", "compilerOptions": { "outDir": "./dist", "rootDir": "./src", "composite": true, "types": ["mocha", "chai"], "sourceMap": true }, "include": ["src/**/*"], "ts-node": { "transpileOnly": true, "files": true } } 

the test script won't work, probably because I switched to commonjs

    "test": "NODE_OPTIONS='--loader ts-node/esm' mocha --extensions ts \"src/**/*.test.ts\"",

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.

### Fix to your `test` script:

```json
"test": "mocha --require ts-node/register --extensions ts \"src/**/*.test.ts\""
```

### Explanation

- `--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`:

```json
"scripts": {
  "test": "mocha --require ts-node/register --extensions ts \"src/**/*.test.ts\""
}
```

Run it with:

```sh
yarn test
```

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?

{
  "require": "ts-node/register", "extensions": ["ts"], "spec": "tests/src/**/*.test.ts" }

Assistant:  YesYes, your updated `.mocharc.json` looks mostly correct to work with CommonJS and ts-node. Just a few notes:

1. 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/`.

2. Since you're using tsconfig composite, you might want to tell ts-node to use your `tsconfig.json` explicitly by adding a config option.

3. You can also add `"timeout"` or other mocha options if needed.

### Example updated `.mocharc.json`:

```json
{
  "require": "ts-node/register",
  "extensions": ["ts"],
  "spec": "tests/src/**/*.test.ts",
  "watch-files": ["tests/src/**/*.ts"],
  "watch-ignore": ["node_modules"],
  "timeout": 5000
}
```

### 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:

```bash
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:

```json
"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
$ yarn workspace @photon/core build
warning package.json: No license field
warning package.json: No license field
$ 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: