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

Support AssertionError expected and actual values #3217

Merged
merged 9 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
128 changes: 118 additions & 10 deletions integration_tests/__tests__/__snapshots__/failures-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ Object {
"rest": " FAIL __tests__/assertion-count-test.js
● .assertions() › throws

expect(received).toBeTruthy()

Expected value to be truthy, instead received
false
Error
Error: expect(received).toBeTruthy()

at Object.throws (__tests__/assertion-count-test.js:14:17)
Expected value to be truthy, instead received
false

● .assertions() › throws

Expand All @@ -74,12 +73,11 @@ Object {

● .assertions() › throws on redeclare of assertion count

expect(received).toBeTruthy()

Expected value to be truthy, instead received
false
Error
Error: expect(received).toBeTruthy()

at Object.redeclare (__tests__/assertion-count-test.js:18:17)
Expected value to be truthy, instead received
false

● .assertions() › throws on assertion

Expand All @@ -103,3 +101,113 @@ Ran all test suites matching \\"assertion-count-test.js\\".
",
}
`;

exports[`works with node assert 1`] = `
Object {
"rest": " FAIL __tests__/node-assertion-error-test.js
● assert

Error
AssertionError: false == true

● assert with a message

Error
AssertionError: this is a message

● assert.ok

Error
AssertionError: false == true

● assert.ok with a message

Error
AssertionError: this is a message

● assert.equal

Error
expect(received).toBe(expected)

Expected value to be (using ===):
2
Received:
1

● assert.equal with a message

Error
expect(received).toBe(expected)

Expected value to be (using ===):
2
Received:
1

● assert.deepEqual

Error
expect(received).toBe(expected)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like toBe is probably the wrong thing to use for the matcher hint... 🤔


Expected value to be (using ===):
{\\"a\\": {\\"b\\": {\\"c\\": 6}}}
Received:
{\\"a\\": {\\"b\\": {\\"c\\": 5}}}

Difference:

- Expected
+ Received

Object {
\\"a\\": Object {
\\"b\\": Object {
- \\"c\\": 6,
+ \\"c\\": 5,
},
},
}

● assert.deepEqual with a message

Error
expect(received).toBe(expected)

Expected value to be (using ===):
{\\"a\\": {\\"b\\": {\\"c\\": 7}}}
Received:
{\\"a\\": {\\"b\\": {\\"c\\": 5}}}

Difference:

- Expected
+ Received

Object {
\\"a\\": Object {
\\"b\\": Object {
- \\"c\\": 7,
+ \\"c\\": 5,
},
},
}

✕ assert
✕ assert with a message
✕ assert.ok
✕ assert.ok with a message
✕ assert.equal
✕ assert.equal with a message
✕ assert.deepEqual
✕ assert.deepEqual with a message

",
"summary": "Test Suites: 1 failed, 1 total
Tests: 8 failed, 8 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching \\"node-assertion-error-test.js\\".
",
}
`;
7 changes: 7 additions & 0 deletions integration_tests/__tests__/failures-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ test('throwing not Error objects', () => {
extractSummary(stderr),
)).toMatchSnapshot();
});

test('works with node assert', () => {
const {stderr} = runJest(dir, ['node-assertion-error-test.js']);
expect(stripInconsistentStackLines(
extractSummary(stderr),
)).toMatchSnapshot();
});
45 changes: 45 additions & 0 deletions integration_tests/failures/__tests__/node-assertion-error-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/

'use strict';

const assert = require('assert');

test('assert', () => {
assert(false);
});

test('assert with a message', () => {
assert(false, 'this is a message');
});

test('assert.ok', () => {
assert.ok(false);
});

test('assert.ok with a message', () => {
assert.ok(false, 'this is a message');
});

test('assert.equal', () => {
assert.equal(1, 2);
});

test('assert.equal with a message', () => {
assert.equal(1, 2, 'this is a message');
});

test('assert.deepEqual', () => {
assert.deepEqual({a: {b: {c: 5}}}, {a: {b: {c: 6}}});
});

test('assert.deepEqual with a message', () => {
assert.deepEqual({a: {b: {c: 5}}}, {a: {b: {c: 7}}}, 'this is a message');
});
1 change: 1 addition & 0 deletions packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"main": "build/index.js",
"dependencies": {
"graceful-fs": "^4.1.11",
"jest-diff": "^19.0.0",
"jest-matcher-utils": "^19.0.0",
"jest-matchers": "^19.0.0",
"jest-message-util": "^19.0.0",
Expand Down
24 changes: 23 additions & 1 deletion packages/jest-jasmine2/src/jasmine/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/* eslint-disable sort-keys */
'use strict';

const diff = require('jest-diff');
const {
matcherHint,
printReceived,
printExpected,
} = require('jest-matcher-utils');
const ExpectationFailed = require('../ExpectationFailed');
const expectationResultFactory = require('../expectationResultFactory');

Expand Down Expand Up @@ -121,10 +127,26 @@ Spec.prototype.onException = function onException(e) {
return;
}

const {expected, actual} = e || {};
let message;
if (expected && actual) {
const diffString = diff(expected, actual, {
expand: this.expand,
});
message = matcherHint('.toBe') +
'\n\n' +
`Expected value to be (using ===):\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(actual)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '');
}
Copy link
Member

Choose a reason for hiding this comment

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

Any way we can make the call to diff and the message lazy, as in message = () => {…}? Otherwise I'm a bit worried here about perf in the case where we don't actually need the message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that earlier and the process just hung forever which I thought was odd...


this.addExpectationResult(
false,
{
matcherName: '',
matcherName: 'blah',
message,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've verified that adding this property (even when it's undefined in most cases) does not cause a negative impact on the other tests. Though the tests are failing due to snapshot issues noted in #3216

passed: false,
expected: '',
actual: '',
Expand Down