Skip to content

Commit

Permalink
Support AssertionError expected and actual values (jestjs#3217)
Browse files Browse the repository at this point in the history
* node `assert` snapshot tests

* WIP: getting close to a working version

* Refactor assert error messages

* Remove jest-diff from jest-jasmine2 deps

* Support throws and doesNotThrow assert matchers

* Update snapshots

* Fix snapshots across different node verions

* Human readable messages
  • Loading branch information
Kent C. Dodds authored and cpojer committed Apr 11, 2017
1 parent 0c87839 commit e0cafe7
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 6 deletions.
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 deeply equal to:
{\\"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 deeply equal 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 not to deeply equal to:
{\\"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 deeply and strictly equal to:
{\\"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 not to deeply and strictly equal 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..
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(() => {});
});
2 changes: 2 additions & 0 deletions packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"license": "BSD-3-Clause",
"main": "build/index.js",
"dependencies": {
"chalk": "^1.1.3",
"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
Loading

0 comments on commit e0cafe7

Please sign in to comment.