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

[Bug]: setupFilesAfterEnv with a path to a TS file leads to the error "SyntaxError: Cannot use import statement outside a module" #4439

Closed
xhuberdeau opened this issue Jul 10, 2024 · 4 comments · Fixed by #4440
Labels
🐛 Bug Confirmed Bug is confirmed

Comments

@xhuberdeau
Copy link

Version

29.2.1

Steps to reproduce

  1. Clone https://github.com/xhuberdeau/reproduce-tsjest-29.2.1-error
  2. Run yarn install
  3. Run yarn test
  4. See error "SyntaxError: Cannot use import statement outside a module"
  5. Now go inside package.json, change ts-jest version to 29.2.0
  6. Run yarn install again
  7. Run yarn test again
  8. => no more error

Expected behavior

Before 29.2.1, I could run tests with a custom config using setupFilesAfterEnv pointing to a ts file.

Actual behavior

Since 29.2.1, I get a SyntaxError: Cannot use import statement outside a module error

Debug log

yarn run v1.22.22
$ jest --config=jest/acceptance.jest.config.js --watchAll=false --coverage --runInBand
 FAIL  src/test.acceptance.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /home/xavier-huberdeau/Documents/reproduce-tsjest-29.2.1-error/jest/setupTests.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import path from 'node:path';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)


=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.846 s, estimated 1 s
Ran all test suites.
error Command failed with exit code 1.

Additional context

No response

Environment

System:
    OS: Linux 6.5 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1265U
  Binaries:
    Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node
    Yarn: 1.22.22 - ~/.yarn/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm
    bun: 1.1.12 - ~/.nvm/versions/node/v18.18.0/bin/bun
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@NeoDobby
Copy link

This happens to me too, but with actual TS test files with import statements, only on linux, not on windows

@kujtimprenku
Copy link

This error started showing up for me too even though my jest config file does not make use of setupFilesAfterEnv

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 10, 2024

Sorry for the issue, we will release hotfix quickly

@xhuberdeau
Copy link
Author

Sorry for the issue, we will release hotfix quickly

Awesome, thanks a lot for the quick fix!

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

Successfully merging a pull request may close this issue.

4 participants