ts-node-test icon indicating copy to clipboard operation
ts-node-test copied to clipboard

Modules in tests?

Open TheChilliPL opened this issue 2 years ago • 6 comments

For some reason, I am unable to use modules in tests.

ℹ (node:8988) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
ℹ F:\Chilli\WebstormProjects\juan\src\tests\utils.ts:1
ℹ import { test } from "node:test";
ℹ ^^^^^^
ℹ
ℹ SyntaxError: Cannot use import statement outside a module
ℹ     at internalCompileFunction (node:internal/vm:73:18)

If I try to make the file .mts instead of .ts, the error changes:

ℹ file:///F:/Chilli/WebstormProjects/juan/src/tests/utils.mts:2
ℹ Object.defineProperty(exports, "__esModule", { value: true });
ℹ                       ^
ℹ
ℹ ReferenceError: exports is not defined in ES module scope
ℹ     at file:///F:/Chilli/WebstormProjects/juan/src/tests/utils.mts:2:23
ℹ     at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
ℹ
ℹ Node.js v20.0.0

while the utils.(m)ts file starts with

import { test } from "node:test";
let assert = require("node:assert");
import * as Utils from "../utils";

Note that I am unable to change my "module": "commonjs" in tsconfig.json and I cannot put "type": "module" in my package.json. If I do any of those, it still doesn't work, and the rest of my project breaks as well.

TheChilliPL avatar May 02 '23 23:05 TheChilliPL

If I replace all the imports with let … = require(…) thing, it crashes on the first required export with a SyntaxError

TheChilliPL avatar May 02 '23 23:05 TheChilliPL

Note that the tests do run with no problems if I call npx ts-node src/tests/utils.ts

TheChilliPL avatar May 02 '23 23:05 TheChilliPL

This project uses the ts-node/esm loader internally, so it may cause issues with CommonJS:

https://github.com/meyfa/ts-node-test/blob/e6cfd841004d96f9e7859518a28238e47595afe7/src/index.ts#L27

Can you try with esModuleInterop set to true in tsconfig.json?

You may also have this affect ts-node only, and not your build results: https://github.com/TypeStrong/ts-node/#via-tsconfigjson-recommended

meyfa avatar May 02 '23 23:05 meyfa

Maybe related: https://github.com/nodejs/node/issues/47880

Can you try with Node.js 18 just so we know whether the Node.js loaders issue is at the root of this problem?

meyfa avatar May 09 '23 12:05 meyfa

This project uses the ts-node/esm loader internally, so it may cause issues with CommonJS:

https://github.com/meyfa/ts-node-test/blob/e6cfd841004d96f9e7859518a28238e47595afe7/src/index.ts#L27

Can you try with esModuleInterop set to true in tsconfig.json?

You may also have this affect ts-node only, and not your build results: TypeStrong/ts-node#via-tsconfigjson-recommended

"esModuleInterop": true doesn't help. It still throws SyntaxError: Cannot use import statement outside a module. Tests do work if I have "type": "module" in my package.json but then I cannot run my code as normal (TypeScript complains that .ts is not a valid file type??)

TheChilliPL avatar Jun 10 '23 08:06 TheChilliPL

I managed to make it run like normal with "type": "module" by using tsx myself instead of ts-node

TheChilliPL avatar Jun 10 '23 08:06 TheChilliPL