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]: Multiple You are trying to import a file outside of the scope of the test code. errors when bumping to 29.6.0 #14302

Closed
juan-fernandez opened this issue Jul 5, 2023 · 3 comments · Fixed by #14304

Comments

@juan-fernandez
Copy link

juan-fernandez commented Jul 5, 2023

Version

29.6.0

Steps to reproduce

There's a reproducible case in this repository:

https://github.com/juan-fernandez/test-jest-bug

The steps to follow are:

git clone git@github.com:juan-fernandez/test-jest-bug.git
cd test-jest-bug
yarn
yarn test

When you run yarn test, you see that the test fails with an error that wasn't there in jest@29.5.0.

Expected behavior

While I understand that the test bad-test.test itself is bad (hence the name), and this is something that the test shouldn't be doing, I think it makes sense to add this check either as a warning or with a way to opt out, so that jest users can adapt to this new behavior incrementally.

Actual behavior

You see this error:

 FAIL  ./bad-test.test.js


  ● Test suite failed to run

    ReferenceError: You are trying to `import` a file outside of the scope of the test code.

       5 |     afterAll(() => {
       6 |         setImmediate(() => {
    >  7 |             require('./cleanup.js')
         |             ^
       8 |         })
       9 |     })
      10 | })

      at Runtime._execModule (node_modules/jest-runtime/build/index.js:1381:13)
      at Immediate.require (bad-test.test.js:7:13)

Test Suites: 1 failed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.123 s, estimated 1 s

Additional context

I don't think this is a bug (this issue does not fit in proposal or question either), and I think the changes in #14110 make sense, but there's no way right now to adjust to this change incrementally.

Environment

System:
    OS: macOS 13.4.1
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 19.9.0 - ~/.volta/tools/image/node/19.9.0/bin/node
    Yarn: 4.0.0-rc.42 - ~/.volta/tools/image/yarn/4.0.0-rc.42/bin/yarn
    npm: 9.6.3 - ~/.volta/tools/image/node/19.9.0/bin/npm
  npmPackages:
    jest: 29.6.0 => 29.6.0
@juan-fernandez juan-fernandez changed the title [Bug]: Multiple You are trying to import a file outside of the scope of the test code. errors when bumping to 29.6.0 [Bug]: Multiple You are trying to import a file outside of the scope of the test code. errors when bumping to 29.6.0 Jul 5, 2023
@elimumford
Copy link

elimumford commented Jul 5, 2023

Same issue, similar use pattern (dynamic loading, using await import)

Directly downgrading to 29.5 doesn't fix the issue either because it will leave all the dependencies in the lock files at 29.6 (due to ^29... refs... and there are a lot of them... working on a backtrack to pin to 29.5's...

@SimenB
Copy link
Member

SimenB commented Jul 6, 2023

@github-actions
Copy link

github-actions bot commented Aug 7, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants