Skip to content

Modules in tests? #64

Description

@TheChilliPL

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.

Activity

  1. TheChilliPL commented on May 2, 2023

    @TheChilliPL
    Author

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

  2. TheChilliPL commented on May 2, 2023

    @TheChilliPL
    Author

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

  3. meyfa commented on May 2, 2023

    @meyfa
    Owner

    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

  4. meyfa commented on May 9, 2023

    @meyfa
    Owner

    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?

  5. TheChilliPL commented on Jun 10, 2023

    @TheChilliPL
    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??)

  6. TheChilliPL commented on Jun 10, 2023

    @TheChilliPL
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions