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 8 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
269 changes: 269 additions & 0 deletions integration_tests/__tests__/__snapshots__/failures-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,272 @@ 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

assert.equal(received, expected) or assert(received)

Expected value to be (operator: ==):
true
Received:
false

at Object.<anonymous> (__tests__/node-assertion-error-test.js:16:3)

● assert with a message

assert.equal(received, expected) or assert(received)

Expected value to be (operator: ==):
true
Received:
false

Message:
this is a message

at Object.<anonymous> (__tests__/node-assertion-error-test.js:20:3)

● assert.ok

assert.equal(received, expected) or assert(received)

Expected value to be (operator: ==):
true
Received:
false

at Object.<anonymous> (__tests__/node-assertion-error-test.js:24:10)

● assert.ok with a message

assert.equal(received, expected) or assert(received)

Expected value to be (operator: ==):
true
Received:
false

Message:
this is a message

at Object.<anonymous> (__tests__/node-assertion-error-test.js:28:10)

● assert.equal

assert.equal(received, expected) or assert(received)

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

at Object.<anonymous> (__tests__/node-assertion-error-test.js:32:10)

● assert.notEqual

assert.notEqual(received, expected)

Expected value not to be (operator: !=):
1
Received:
1

Difference:

Compared values have no visual difference.

at Object.<anonymous> (__tests__/node-assertion-error-test.js:36:10)

● assert.deepEqual

assert.deepEqual(received, expected)

Expected value to deepEqual to:
Copy link
Member

Choose a reason for hiding this comment

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

Is this just like ==, but deep? In that case, how about:

"Expected value to deeply equal:"

maybe?

{\\"a\\": {\\"b\\": {\\"c\\": 6}}}
Received:
{\\"a\\": {\\"b\\": {\\"c\\": 5}}}

Difference:

- Expected
+ Received

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

at Object.<anonymous> (__tests__/node-assertion-error-test.js:40:10)

● assert.deepEqual with a message

assert.deepEqual(received, expected)

Expected value to deepEqual to:
{\\"a\\": {\\"b\\": {\\"c\\": 7}}}
Received:
{\\"a\\": {\\"b\\": {\\"c\\": 5}}}

Message:
this is a message

Difference:

- Expected
+ Received

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

at Object.<anonymous> (__tests__/node-assertion-error-test.js:44:10)

● assert.notDeepEqual

assert.notDeepEqual(received, expected)

Expected value to notDeepEqual to:
Copy link
Member

Choose a reason for hiding this comment

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

"not to deeply equal"?

{\\"a\\": 1}
Received:
{\\"a\\": 1}

Difference:

Compared values have no visual difference.

at Object.<anonymous> (__tests__/node-assertion-error-test.js:48:10)

● assert.strictEqual

assert.strictEqual(received, expected)

Expected value to be (operator: ===):
NaN
Received:
1

at Object.<anonymous> (__tests__/node-assertion-error-test.js:52:10)

● assert.notStrictEqual

assert.notStrictEqual(received, expected)

Expected value not to be (operator: !==):
1
Received:
1

Message:
My custom error message

Difference:

Compared values have no visual difference.

at Object.<anonymous> (__tests__/node-assertion-error-test.js:56:10)

● assert.deepStrictEqual

assert.deepStrictEqual(received, expected)

Expected value to deepStrictEqual to:
Copy link
Member

Choose a reason for hiding this comment

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

"to deeply and strictly equal"?

{\\"a\\": 2}
Received:
{\\"a\\": 1}

Difference:

- Expected
+ Received

Object {
- \\"a\\": 2,
+ \\"a\\": 1,
}

at Object.<anonymous> (__tests__/node-assertion-error-test.js:60:10)

● assert.notDeepStrictEqual

assert.notDeepStrictEqual(received, expected)

Expected value to notDeepStrictEqual to:
{\\"a\\": 1}
Received:
{\\"a\\": 1}

Difference:

Compared values have no visual difference.

at Object.<anonymous> (__tests__/node-assertion-error-test.js:64:10)

● assert.ifError

Error
1 thrown

● assert.doesNotThrow

assert.doesNotThrow(function)

Expected the function not to throw an error.
Instead, it threw:
[Error: err!]

Message:
Got unwanted exception..

at Object.<anonymous> (__tests__/node-assertion-error-test.js:72:10)

● assert.throws

assert.throws(function)

Expected the function to throw an error.
But it didn't throw anything.

Message:
Missing expected exception..
Copy link
Member

Choose a reason for hiding this comment

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

This is coming from assert, right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, assert generates this message and marks that it's not generated 🤷‍♂️, that's why it's shown here.


at Object.<anonymous> (__tests__/node-assertion-error-test.js:78:10)

✕ assert
✕ assert with a message
✕ assert.ok
✕ assert.ok with a message
✕ assert.equal
✕ assert.notEqual
✕ assert.deepEqual
✕ assert.deepEqual with a message
✕ assert.notDeepEqual
✕ assert.strictEqual
✕ assert.notStrictEqual
✕ assert.deepStrictEqual
✕ assert.notDeepStrictEqual
✕ assert.ifError
✕ assert.doesNotThrow
✕ assert.throws

",
"summary": "Test Suites: 1 failed, 1 total
Tests: 16 failed, 16 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching \\"node-assertion-error-test.js\\".
",
}
`;
11 changes: 10 additions & 1 deletion integration_tests/__tests__/failures-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ skipOnWindows.suite();
// snapshot tests fail on different machines. This function makes sure
// this extra line is always removed.
const stripInconsistentStackLines = summary => {
summary.rest = summary.rest.replace(/\n^.*process\._tickCallback.*$/gm, '');
summary.rest = summary.rest
.replace(/\n^.*process\._tickCallback.*$/gm, '')
.replace(/\n^.*_throws.*$/gm, '')
.replace(/\n^.*Function\..*(throws|doesNotThrow).*$/gm, '')
.replace(/(\n^.*Object.<anonymous>)\.test(.*$)/gm, '$1$2');
return summary;
};

Expand All @@ -36,3 +40,8 @@ test('throwing not Error objects', () => {
stderr = runJest(dir, ['assertion-count-test.js']).stderr;
expect(stripInconsistentStackLines(extractSummary(stderr))).toMatchSnapshot();
});

test('works with node assert', () => {
const {stderr} = runJest(dir, ['node-assertion-error-test.js']);
expect(stripInconsistentStackLines(extractSummary(stderr))).toMatchSnapshot();
});
79 changes: 79 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,79 @@
/**
* 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.notEqual', () => {
assert.notEqual(1, 1);
});

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');
});

test('assert.notDeepEqual', () => {
assert.notDeepEqual({a: 1}, {a: 1});
});

test('assert.strictEqual', () => {
assert.strictEqual(1, NaN);
});

test('assert.notStrictEqual', () => {
assert.notStrictEqual(1, 1, 'My custom error message');
});

test('assert.deepStrictEqual', () => {
assert.deepStrictEqual({a: 1}, {a: 2});
});

test('assert.notDeepStrictEqual', () => {
assert.notDeepStrictEqual({a: 1}, {a: 1});
});

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

test('assert.doesNotThrow', () => {
assert.doesNotThrow(() => {
throw Error('err!');
});
});

test('assert.throws', () => {
assert.throws(() => {});
});
Loading