question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest cannot find custom environment in @jest-environment docblock

See original GitHub issue

🐛 Bug Report

When I added a @jest-environment docblock to a test suite (name1.test.js) I got below error when running test:

Validation Error:

      Test environment MongoEnvironment cannot be found. Make sure the testEnvironment configuration option points to an existing node module.

But if I comment out line 2 in name1.test.js (to remove @jest-environment), my custom test environment named MongoEnvironment is working well, line 11 in mongo-environment.js works correctly. It means Jest is configured well, isn’t it?

To Reproduce

  1. Clone https://github.com/tiendq/haypley-services
  2. Checkout branch test/reproduce-jest-docblock-error
  3. yarn install
  4. yarn test name1.test.js

Expected behavior

Test runs well. No error.

Link to repl or repo (highly encouraged)

https://github.com/tiendq/haypley-services

envinfo

System:
    OS: macOS 10.14.5
    CPU: (4) x64 Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz
  Binaries:
    Node: 10.16.3 - /usr/local/opt/node@10/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.9.0 - /usr/local/opt/node@10/bin/npm
  npmPackages:
    jest: ^24.8.0 => 24.8.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
SimenBcommented, Nov 29, 2019

The docs should probably explicitly state that the value of the pragma must be a requireable module (so either something in node_modules or a relative path to some other module), not just a name. Wanna contribute that change to the docs?

EDIT: What he said 🙂

8reactions
jeysalcommented, Nov 29, 2019

Jest has no way to find the environment just based on the string MongoEnvironment. If you specify * @jest-environment ./test/jest/mongo-environment.js it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set custom Jest environment in docblock - Stack Overflow
Yes, you can set a custom environment for a single Jest test using the docblock syntax. If your custom environment is installed as...
Read more >
Configuring Jest
Any docblock pragmas in test files will be passed to the environment constructor and can be used for per-test configuration. If the pragma...
Read more >
Configuring Jest compiled - w3resource
Any docblock pragmas in test files that are passed to the environment constructor and can also be used for per-test configuration. In the...
Read more >
jest: configuring test environments - code-comments
Add a @jest-environment doc block. Note: The docs suggest that this approach works, however, it did not work in my experience. Perhaps a...
Read more >
Jest Runner | Stryker Mutator
Don't worry; using Stryker's alternative is harmless during regular unit testing. If you're using a custom test environment, you'll need to mixin the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found