Skip to content

Commit

Permalink
Merge pull request #13384 from Automattic/vkarpov15/gh-13373
Browse files Browse the repository at this point in the history
Add SUPPRESS_JEST_WARNINGS environment variable to hide jest warnings
  • Loading branch information
vkarpov15 committed May 8, 2023
2 parents 76e6456 + 0ab335f commit d96de21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 6 additions & 0 deletions docs/jest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Jest is a JavaScript runtime developed by Facebook that is usually used for test
Because Jest is designed primarily for testing React applications, using it to test Node.js server-side applications comes with a lot of caveats.
We strongly recommend using a different testing framework, like [Mocha](https://mochajs.org/).

To suppress any Jest warnings from Mongoose, set the `SUPPRESS_JEST_WARNINGS` environment variable:

```
env SUPPRESS_JEST_WARNINGS=1 npm test
```

If you choose to delve into dangerous waters and test Mongoose apps with Jest, here's what you need to know:

<h2 id="recommended-testenvironment"><a href="#recommended-testenvironment">Recommended <code>testEnvironment</code></a></h2>
Expand Down
26 changes: 15 additions & 11 deletions lib/helpers/printJestWarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

const utils = require('../utils');

if (typeof jest !== 'undefined' && typeof window !== 'undefined') {
utils.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
'with Jest\'s default jsdom test environment. Please make sure you read ' +
'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
'https://mongoosejs.com/docs/jest.html');
}
if (typeof jest !== 'undefined' && !process.env.SUPPRESS_JEST_WARNINGS) {
if (typeof window !== 'undefined') {
utils.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
'with Jest\'s default jsdom test environment. Please make sure you read ' +
'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
'https://mongoosejs.com/docs/jest.html. Set the SUPPRESS_JEST_WARNINGS to true ' +
'to hide this warning.');
}

if (typeof jest !== 'undefined' && setTimeout.clock != null && typeof setTimeout.clock.Date === 'function') {
utils.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
'with Jest\'s mock timers enabled. Please make sure you read ' +
'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
'https://mongoosejs.com/docs/jest.html');
if (setTimeout.clock != null && typeof setTimeout.clock.Date === 'function') {
utils.warn('Mongoose: looks like you\'re trying to test a Mongoose app ' +
'with Jest\'s mock timers enabled. Please make sure you read ' +
'Mongoose\'s docs on configuring Jest to test Node.js apps: ' +
'https://mongoosejs.com/docs/jest.html. Set the SUPPRESS_JEST_WARNINGS to true ' +
'to hide this warning.');
}
}

0 comments on commit d96de21

Please sign in to comment.