Skip to content

Commit

Permalink
try to make it work, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jan 26, 2019
1 parent b337101 commit 4309d7a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-util]` Install fix for `instanceof` as part of other globals into test environments ([#5995](https://github.com/facebook/jest/pull/5995))

### Chore & Maintenance

- `[website]` Fix broken help link on homepage ([#7706](https://github.com/facebook/jest/pull/7706))
Expand Down
17 changes: 17 additions & 0 deletions e2e/__tests__/instanceof.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/
'use strict';

import runJest from '../runJest';

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

expect(status).toBe(0);
});
46 changes: 46 additions & 0 deletions e2e/instanceof/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/
'use strict';

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"
}
}
10 changes: 4 additions & 6 deletions packages/jest-jasmine2/src/jasmine/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/* @flow */
/* eslint-disable sort-keys */

import {AssertionError} from 'assert';

import ExpectationFailed from '../ExpectationFailed';

import expectationResultFactory from '../expectationResultFactory';
Expand Down Expand Up @@ -148,12 +146,12 @@ Spec.prototype.onException = function onException(error) {
return;
}

if (error instanceof ExpectationFailed) {
return;
if (error && error.code === 'ERR_ASSERTION') {
error = assertionErrorMessage(error, {expand: this.expand});
}

if (error instanceof AssertionError) {
error = assertionErrorMessage(error, {expand: this.expand});
if (error instanceof ExpectationFailed) {
return;
}

this.addExpectationResult(
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-util/src/installCommonGlobals.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {ConfigGlobals} from 'types/Config';
import type {Global} from 'types/Global';

import fs from 'fs';
import addInstanceOfAlias from './addInstanceOfAlias';
import createProcessObject from './createProcessObject';
import deepCyclicCopy from './deepCyclicCopy';

Expand Down Expand Up @@ -66,5 +67,7 @@ export default function(globalObject: Global, globals: ConfigGlobals) {
globalObject.setImmediate = global.setImmediate;
globalObject.clearImmediate = global.clearImmediate;

addInstanceOfAlias(globalObject.Error, Error);

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

0 comments on commit 4309d7a

Please sign in to comment.