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

Modules in tests? #64

Open
TheChilliPL opened this issue May 2, 2023 · 6 comments
Open

Modules in tests? #64

TheChilliPL opened this issue May 2, 2023 · 6 comments

Comments

@TheChilliPL
Copy link

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
Copy link
Author

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

@TheChilliPL
Copy link
Author

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

@meyfa
Copy link
Owner

meyfa commented May 2, 2023

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

const esmLoader = pathToFileURL(require.resolve('ts-node/esm')).toString()

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
Copy link
Owner

meyfa commented May 9, 2023

Maybe related: nodejs/node#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?

@TheChilliPL
Copy link
Author

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

const esmLoader = pathToFileURL(require.resolve('ts-node/esm')).toString()

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants