From d413b63fd8d1190d2cd15f073d48efb71808b7de Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Tue, 28 Mar 2017 07:09:12 -0600 Subject: [PATCH] WIP: getting close to a working version --- .../__snapshots__/failures-test.js.snap | 98 +++++++++++++------ packages/jest-jasmine2/package.json | 1 + packages/jest-jasmine2/src/jasmine/Spec.js | 24 ++++- 3 files changed, 92 insertions(+), 31 deletions(-) diff --git a/integration_tests/__tests__/__snapshots__/failures-test.js.snap b/integration_tests/__tests__/__snapshots__/failures-test.js.snap index 2a33d89ecfa1..a394cc634b05 100644 --- a/integration_tests/__tests__/__snapshots__/failures-test.js.snap +++ b/integration_tests/__tests__/__snapshots__/failures-test.js.snap @@ -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 @@ -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 @@ -109,51 +107,91 @@ Object { "rest": " FAIL __tests__/node-assertion-error-test.js ● assert - AssertionError: false == true - - at Object.test (__tests__/node-assertion-error-test.js:16:3) + Error + AssertionError: false == true ● assert with a message - AssertionError: this is a message - - at Object.test (__tests__/node-assertion-error-test.js:20:3) + Error + AssertionError: this is a message ● assert.ok - AssertionError: false == true - - at Object.test (__tests__/node-assertion-error-test.js:24:10) + Error + AssertionError: false == true ● assert.ok with a message - AssertionError: this is a message - - at Object.test (__tests__/node-assertion-error-test.js:28:10) + Error + AssertionError: this is a message ● assert.equal - AssertionError: 1 == 2 + Error + expect(received).toBe(expected) - at Object.test (__tests__/node-assertion-error-test.js:32:10) + Expected value to be (using ===): + 2 + Received: + 1 ● assert.equal with a message - AssertionError: this is a message + Error + expect(received).toBe(expected) - at Object.test (__tests__/node-assertion-error-test.js:36:10) + Expected value to be (using ===): + 2 + Received: + 1 ● assert.deepEqual - AssertionError: { a: { b: { c: 5 } } } deepEqual { a: { b: { c: 6 } } } + Error + expect(received).toBe(expected) + + Expected value to be (using ===): + {\\"a\\": {\\"b\\": {\\"c\\": 6}}} + Received: + {\\"a\\": {\\"b\\": {\\"c\\": 5}}} + + Difference: - at Object.test (__tests__/node-assertion-error-test.js:40:10) + - Expected + + Received + + Object { + \\"a\\": Object { + \\"b\\": Object { + - \\"c\\": 6, + + \\"c\\": 5, + }, + }, + } ● assert.deepEqual with a message - AssertionError: this is a message + Error + expect(received).toBe(expected) + + Expected value to be (using ===): + {\\"a\\": {\\"b\\": {\\"c\\": 7}}} + Received: + {\\"a\\": {\\"b\\": {\\"c\\": 5}}} + + Difference: + + - Expected + + Received - at Object.test (__tests__/node-assertion-error-test.js:44:10) + Object { + \\"a\\": Object { + \\"b\\": Object { + - \\"c\\": 7, + + \\"c\\": 5, + }, + }, + } ✕ assert ✕ assert with a message diff --git a/packages/jest-jasmine2/package.json b/packages/jest-jasmine2/package.json index a4220b2f6ea2..b215c928a86c 100644 --- a/packages/jest-jasmine2/package.json +++ b/packages/jest-jasmine2/package.json @@ -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", diff --git a/packages/jest-jasmine2/src/jasmine/Spec.js b/packages/jest-jasmine2/src/jasmine/Spec.js index e9403e869547..b70f45767ea6 100644 --- a/packages/jest-jasmine2/src/jasmine/Spec.js +++ b/packages/jest-jasmine2/src/jasmine/Spec.js @@ -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'); @@ -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}` : ''); + } + this.addExpectationResult( false, { - matcherName: '', + matcherName: 'blah', + message, passed: false, expected: '', actual: '',