Jest: Jest cannot find custom environment in @jest-environment docblock

Created on 29 Nov 2019  路  6Comments  路  Source: facebook/jest

馃悰 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
Bug Report Needs Repro Needs Triage

Most helpful comment

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 馃檪

All 6 comments

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.

Oh, you closed it so fast, at least document says only about environment name, not a JS file path. Who don't think Jest will use its configuration?

Jest does use the configured environment - the docblock is just for overriding it for individual test files.
And the name MongoEnvironment does not appear in the configuration, so I'm not sure what you'd expect.

I do see Jest knows well about my custom MongoEnvironment in other cases, so I expect it works with docblock too. Or at least document should say docblock is just for overriding and it might not know your configuration :)

Happy to accept a PR improving the docs if you see an opportunity!
As for the docblock there is no way to know which file MongoEnvironment refers to so it has to be a file path.

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 馃檪

Was this page helpful?
0 / 5 - 0 ratings