Skip to content

Commit

Permalink
check navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 28, 2020
1 parent 012dbbc commit d214b34
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
26 changes: 26 additions & 0 deletions e2e/__tests__/__snapshots__/wrongEnv.test.ts.snap
Expand Up @@ -4,6 +4,7 @@ exports[`Wrong globals for environment print useful error for document 1`] = `
FAIL __tests__/node.js
✕ use document
○ skipped use window
○ skipped use navigator
● use document
Expand All @@ -23,6 +24,30 @@ FAIL __tests__/node.js
at Object.document (__tests__/node.js:16:15)
`;
exports[`Wrong globals for environment print useful error for navigator 1`] = `
FAIL __tests__/node.js
use navigator
skipped use document
skipped use window
use navigator
The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/en/configuration#testenvironment-string.
Consider using the "jsdom" test environment.
ReferenceError: navigator is not defined
30 |
31 | test('use navigator', () => {
> 32 | const userAgent = navigator.userAgent;
| ^
33 |
34 | console.log(userAgent);
35 |
at Object.navigator (__tests__/node.js:32:21)
`;
exports[`Wrong globals for environment print useful error for unref 1`] = `
FAIL __tests__/jsdom.js
use unref
Expand All @@ -49,6 +74,7 @@ exports[`Wrong globals for environment print useful error for window 1`] = `
FAIL __tests__/node.js
use window
skipped use document
skipped use navigator
use window
Expand Down
4 changes: 4 additions & 0 deletions e2e/__tests__/wrongEnv.test.ts
Expand Up @@ -24,6 +24,10 @@ describe('Wrong globals for environment', () => {
assertFailuresAndSnapshot(['node', '-t=document']);
});

it('print useful error for navigator', () => {
assertFailuresAndSnapshot(['node', '-t=navigator']);
});

it('print useful error for unref', () => {
assertFailuresAndSnapshot(['jsdom', '-t=unref']);
});
Expand Down
8 changes: 8 additions & 0 deletions e2e/wrong-env/__tests__/node.js
Expand Up @@ -27,3 +27,11 @@ test('use window', () => {

expect(1).toBe(1);
});

test('use navigator', () => {
const userAgent = navigator.userAgent;

console.log(userAgent);

expect(1).toBe(1);
});
3 changes: 2 additions & 1 deletion packages/jest-message-util/src/index.ts
Expand Up @@ -95,7 +95,8 @@ const blankStringRegexp = /^\s*$/;
function checkForCommonEnvironmentErrors(error: string) {
if (
error.includes('ReferenceError: document is not defined') ||
error.includes('ReferenceError: window is not defined')
error.includes('ReferenceError: window is not defined') ||
error.includes('ReferenceError: navigator is not defined')
) {
return warnAboutWrongTestEnvironment(error, 'jsdom');
} else if (error.includes('.unref is not a function')) {
Expand Down

0 comments on commit d214b34

Please sign in to comment.