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

Fix "instanceof Error" #8220

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions e2e/__tests__/instanceof.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

import runJest from '../runJest';

test('suite with `instanceof` checks', () => {
const {status} = runJest('instanceof');

expect(status).toBe(0);
});
35 changes: 35 additions & 0 deletions e2e/instanceof/__tests__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs');
const http = require('http');

test('fs error', () => {
expect.hasAssertions();

try {
fs.readFileSync('does not exist');
} catch (err) {
expect(err).toBeInstanceOf(Error);
}
});

test('http error', () =>
new Promise((resolve, reject) => {
const request = http.request('http://does-not-exist/blah', res => {
console.log(`STATUS: ${res.statusCode}`);
res.on('end', () => {
reject(new Error('Ended before failure'));
});
});

request.once('error', err => {
try {
expect(err).toBeInstanceOf(Error);
resolve();
} catch (e) {
reject(e);
}
});
}));

test('Array', () => {
expect([]).toBeInstanceOf(Array);
});
5 changes: 5 additions & 0 deletions e2e/instanceof/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
11 changes: 11 additions & 0 deletions packages/jest-util/src/fixInstanceOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function fixInstanceOfError(error: typeof Error): void {
const toString = Object.prototype.toString;
const originalHasInstance = error[Symbol.hasInstance];
Object.defineProperty(error, Symbol.hasInstance, {
value(potentialInstance: any): boolean {
return this === error
? toString.call(potentialInstance) === '[object Error]'
: originalHasInstance.call(this, potentialInstance);
Comment on lines +6 to +8
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: because jest supports node >= 10 now, I think that util.types.isNativeError would work. I did only some limited test and it worked for my small case.

},
});
}
3 changes: 3 additions & 0 deletions packages/jest-util/src/installCommonGlobals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fs from 'fs';
import {Config} from '@jest/types';
import createProcessObject from './createProcessObject';
import deepCyclicCopy from './deepCyclicCopy';
import {fixInstanceOfError} from './fixInstanceOf';

const DTRACE = Object.keys(global).filter(key => key.startsWith('DTRACE'));

Expand Down Expand Up @@ -67,5 +68,7 @@ export default function(
globalObject.setImmediate = global.setImmediate;
globalObject.clearImmediate = global.clearImmediate;

fixInstanceOfError(globalObject.Error);

return Object.assign(globalObject, deepCyclicCopy(globals));
}