From 3c3a7213dda694e2b02ef923dfbd7a2d5c653e75 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sat, 11 May 2019 21:41:29 +0200 Subject: [PATCH 01/13] added fix to improve chai output --- .../chaiAssertionLibrary.ts.snap | 91 +++++++++++++++++++ e2e/__tests__/chaiAssertionLibrary.ts | 20 ++++ .../__tests__/chai_assertion.js | 26 ++++++ .../package.json | 9 ++ e2e/chai-assertion-library-errors/yarn.lock | 47 ++++++++++ .../jest-circus/src/formatNodeAssertErrors.ts | 10 +- packages/jest-jasmine2/src/jasmine/Env.ts | 5 +- packages/jest-jasmine2/src/jasmine/Spec.ts | 8 +- 8 files changed, 213 insertions(+), 3 deletions(-) create mode 100644 e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap create mode 100644 e2e/__tests__/chaiAssertionLibrary.ts create mode 100644 e2e/chai-assertion-library-errors/__tests__/chai_assertion.js create mode 100644 e2e/chai-assertion-library-errors/package.json create mode 100644 e2e/chai-assertion-library-errors/yarn.lock diff --git a/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap b/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap new file mode 100644 index 000000000000..5634e3bfbd3c --- /dev/null +++ b/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap @@ -0,0 +1,91 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`chai assertion errors should display properly 1`] = ` +FAIL __tests__/chai_assertion.js + ● chai.js assertion library test › expect + + assert.(received, expected) + + Expected value "hello sunshine" + Received: + "hello world" + + Message: + expected 'hello world' to equal 'hello sunshine' + + Difference: + + - Expected + + Received + + - hello sunshine + + hello world + + 11 | describe('chai.js assertion library test', () => { + 12 | it('expect', () => { + > 13 | chai.expect('hello world').to.equal('hello sunshine'); + | ^ + 14 | }); + 15 | + 16 | it('should', () => { + + at Object.equal (__tests__/chai_assertion.js:13:35) + + ● chai.js assertion library test › should + + assert.(received, expected) + + Expected value "hello sunshine" + Received: + "hello world" + + Message: + expected 'hello world' to equal 'hello sunshine' + + Difference: + + - Expected + + Received + + - hello sunshine + + hello world + + 18 | const stringExpected = 'hello world'; + 19 | const actualExpected = 'hello sunshine'; + > 20 | stringExpected.should.to.equal(actualExpected); + | ^ + 21 | }); + 22 | + 23 | it('assert', () => { + + at Object.equal (__tests__/chai_assertion.js:20:30) + + ● chai.js assertion library test › assert + + assert.(received, expected) + + Expected value "hello sunshine" + Received: + "hello world" + + Message: + expected 'hello world' to equal 'hello sunshine' + + Difference: + + - Expected + + Received + + - hello sunshine + + hello world + + 22 | + 23 | it('assert', () => { + > 24 | chai.assert.strictEqual('hello world', 'hello sunshine'); + | ^ + 25 | }); + 26 | }); + 27 | + + at Object.strictEqual (__tests__/chai_assertion.js:24:17) +`; diff --git a/e2e/__tests__/chaiAssertionLibrary.ts b/e2e/__tests__/chaiAssertionLibrary.ts new file mode 100644 index 000000000000..78d75be1b5f1 --- /dev/null +++ b/e2e/__tests__/chaiAssertionLibrary.ts @@ -0,0 +1,20 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. 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. + */ + +import path from 'path'; +import {wrap} from 'jest-snapshot-serializer-raw'; +import runJest from '../runJest'; +import {extractSummary, run} from '../Utils'; + +test('chai assertion errors should display properly', () => { + const dir = path.resolve(__dirname, '../chai-assertion-library-errors'); + run('yarn', dir); + + const {stderr} = runJest('chai-assertion-library-errors'); + const {rest} = extractSummary(stderr); + expect(wrap(rest)).toMatchSnapshot(); +}); \ No newline at end of file diff --git a/e2e/chai-assertion-library-errors/__tests__/chai_assertion.js b/e2e/chai-assertion-library-errors/__tests__/chai_assertion.js new file mode 100644 index 000000000000..0e513a890660 --- /dev/null +++ b/e2e/chai-assertion-library-errors/__tests__/chai_assertion.js @@ -0,0 +1,26 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. 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. + */ + +'use strict'; +const chai = require('chai'); + +describe('chai.js assertion library test', () => { + it('expect', () => { + chai.expect('hello world').to.equal('hello sunshine'); + }); + + it('should', () => { + chai.should(); + const stringExpected = 'hello world'; + const actualExpected = 'hello sunshine'; + stringExpected.should.to.equal(actualExpected); + }); + + it('assert', () => { + chai.assert.strictEqual('hello world', 'hello sunshine'); + }); +}); diff --git a/e2e/chai-assertion-library-errors/package.json b/e2e/chai-assertion-library-errors/package.json new file mode 100644 index 000000000000..de82c8d2a783 --- /dev/null +++ b/e2e/chai-assertion-library-errors/package.json @@ -0,0 +1,9 @@ +{ + "jest": { + "testEnvironment": "node", + "verbose": false + }, + "dependencies": { + "chai": "^4.2.0" + } +} diff --git a/e2e/chai-assertion-library-errors/yarn.lock b/e2e/chai-assertion-library-errors/yarn.lock new file mode 100644 index 000000000000..612de38aeffa --- /dev/null +++ b/e2e/chai-assertion-library-errors/yarn.lock @@ -0,0 +1,47 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +assertion-error@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b" + integrity sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw== + +chai@^4.2.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5" + integrity sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw== + dependencies: + assertion-error "^1.1.0" + check-error "^1.0.2" + deep-eql "^3.0.1" + get-func-name "^2.0.0" + pathval "^1.1.0" + type-detect "^4.0.5" + +check-error@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82" + integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII= + +deep-eql@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df" + integrity sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw== + dependencies: + type-detect "^4.0.0" + +get-func-name@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41" + integrity sha1-6td0q+5y4gQJQzoGY2YCPdaIekE= + +pathval@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0" + integrity sha1-uULm1L3mUwBe9rcTYd74cn0GReA= + +type-detect@^4.0.0, type-detect@^4.0.5: + version "4.0.8" + resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c" + integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g== diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index 3ad52ac72117..0e3859c1ff76 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -60,7 +60,7 @@ const formatNodeAssertErrors = (event: Circus.Event, state: Circus.State) => { } else { error = errors; } - return error.code === 'ERR_ASSERTION' + return isAssertionError(error) ? {message: assertionErrorMessage(error, {expand: state.expand})} : errors; }); @@ -168,4 +168,12 @@ function assertionErrorMessage( ); } +function isAssertionError(error: Circus.TestError) { + return ( + error instanceof AssertionError || + error.name === AssertionError.name || + error.code === 'ERR_ASSERTION' + ); +} + export default formatNodeAssertErrors; diff --git a/packages/jest-jasmine2/src/jasmine/Env.ts b/packages/jest-jasmine2/src/jasmine/Env.ts index 494096d9882c..8e7bdb44c080 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.ts +++ b/packages/jest-jasmine2/src/jasmine/Env.ts @@ -663,7 +663,10 @@ export default function(j$: Jasmine) { let checkIsError; let message; - if (error instanceof AssertionError) { + if ( + error instanceof AssertionError || + error.name === AssertionError.name + ) { checkIsError = false; // @ts-ignore TODO Possible error: j$.Spec does not have expand property message = assertionErrorMessage(error, {expand: j$.Spec.expand}); diff --git a/packages/jest-jasmine2/src/jasmine/Spec.ts b/packages/jest-jasmine2/src/jasmine/Spec.ts index b385c3e16555..578b94f63764 100644 --- a/packages/jest-jasmine2/src/jasmine/Spec.ts +++ b/packages/jest-jasmine2/src/jasmine/Spec.ts @@ -237,7 +237,7 @@ export default class Spec { expected: '', actual: '', error: - error instanceof AssertionError + this.isAssertionError(error) ? assertionErrorMessage(error, {expand: this.expand}) : error, }, @@ -292,6 +292,12 @@ export default class Spec { getFullName() { return this.getSpecName(this); } + + isAssertionError(error: Error) { + return ( + error instanceof AssertionError || error.name === AssertionError.name + ); + } } Spec.pendingSpecExceptionMessage = '=> marked Pending'; From c4ba6820b4a9d542c8effe2eb7c0b3404cbcc4d5 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sat, 11 May 2019 21:45:54 +0200 Subject: [PATCH 02/13] fix linting issue --- e2e/__tests__/chaiAssertionLibrary.ts | 2 +- packages/jest-jasmine2/src/jasmine/Spec.ts | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/e2e/__tests__/chaiAssertionLibrary.ts b/e2e/__tests__/chaiAssertionLibrary.ts index 78d75be1b5f1..5e76ff3dd28a 100644 --- a/e2e/__tests__/chaiAssertionLibrary.ts +++ b/e2e/__tests__/chaiAssertionLibrary.ts @@ -17,4 +17,4 @@ test('chai assertion errors should display properly', () => { const {stderr} = runJest('chai-assertion-library-errors'); const {rest} = extractSummary(stderr); expect(wrap(rest)).toMatchSnapshot(); -}); \ No newline at end of file +}); diff --git a/packages/jest-jasmine2/src/jasmine/Spec.ts b/packages/jest-jasmine2/src/jasmine/Spec.ts index 578b94f63764..ad96d44562da 100644 --- a/packages/jest-jasmine2/src/jasmine/Spec.ts +++ b/packages/jest-jasmine2/src/jasmine/Spec.ts @@ -236,10 +236,9 @@ export default class Spec { passed: false, expected: '', actual: '', - error: - this.isAssertionError(error) - ? assertionErrorMessage(error, {expand: this.expand}) - : error, + error: this.isAssertionError(error) + ? assertionErrorMessage(error, {expand: this.expand}) + : error, }, true, ); From bfd6e62ef9554c9919840eb21b91ffefb29376e0 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sat, 11 May 2019 21:47:45 +0200 Subject: [PATCH 03/13] update the change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00e413a7e791..0f5d9d925890 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## master ### Features +- `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454)) ### Fixes From f9ddff700c2e9b3587fd940f96d7f36cfa0a78e4 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sat, 11 May 2019 21:58:25 +0200 Subject: [PATCH 04/13] fix changelog lint --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f5d9d925890..e3413014fea4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## master ### Features + - `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454)) ### Fixes From 5772bd321dd4bab7605e00671062939d36a8aed2 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sun, 12 May 2019 22:44:02 +0200 Subject: [PATCH 05/13] added extra check to see error is not undefined --- packages/jest-circus/src/formatNodeAssertErrors.ts | 4 ++-- packages/jest-jasmine2/src/jasmine/Env.ts | 2 +- packages/jest-jasmine2/src/jasmine/Spec.ts | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index 0e3859c1ff76..fbe245dfd2c0 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -171,8 +171,8 @@ function assertionErrorMessage( function isAssertionError(error: Circus.TestError) { return ( error instanceof AssertionError || - error.name === AssertionError.name || - error.code === 'ERR_ASSERTION' + (error && + (error.name === AssertionError.name || error.code === 'ERR_ASSERTION')) ); } diff --git a/packages/jest-jasmine2/src/jasmine/Env.ts b/packages/jest-jasmine2/src/jasmine/Env.ts index 8e7bdb44c080..559bd2244872 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.ts +++ b/packages/jest-jasmine2/src/jasmine/Env.ts @@ -665,7 +665,7 @@ export default function(j$: Jasmine) { if ( error instanceof AssertionError || - error.name === AssertionError.name + (error && error.name === AssertionError.name) ) { checkIsError = false; // @ts-ignore TODO Possible error: j$.Spec does not have expand property diff --git a/packages/jest-jasmine2/src/jasmine/Spec.ts b/packages/jest-jasmine2/src/jasmine/Spec.ts index ad96d44562da..e22c9f885748 100644 --- a/packages/jest-jasmine2/src/jasmine/Spec.ts +++ b/packages/jest-jasmine2/src/jasmine/Spec.ts @@ -294,7 +294,8 @@ export default class Spec { isAssertionError(error: Error) { return ( - error instanceof AssertionError || error.name === AssertionError.name + error instanceof AssertionError || + (error && error.name === AssertionError.name) ); } } From db0eead7d9f40166f887daad6e388ccb26735107 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sun, 12 May 2019 23:11:16 +0200 Subject: [PATCH 06/13] fixed the comments --- packages/jest-circus/src/formatNodeAssertErrors.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index fbe245dfd2c0..ac2d85f3423c 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -168,11 +168,12 @@ function assertionErrorMessage( ); } -function isAssertionError(error: Circus.TestError) { +function isAssertionError(error: Circus.TestError): error is AssertionError { return ( - error instanceof AssertionError || - (error && - (error.name === AssertionError.name || error.code === 'ERR_ASSERTION')) + error && + (error instanceof AssertionError || + error.name === AssertionError.name || + error.code === 'ERR_ASSERTION') ); } From 243ba8533d4e4ffecc614059c018dc6ad16a5ed6 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sun, 12 May 2019 23:21:21 +0200 Subject: [PATCH 07/13] fix tsc issue --- packages/jest-circus/src/formatNodeAssertErrors.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index ac2d85f3423c..77222ab6b191 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -168,7 +168,9 @@ function assertionErrorMessage( ); } -function isAssertionError(error: Circus.TestError): error is AssertionError { +function isAssertionError( + error: Circus.TestError, +): error is AssertionErrorWithStack { return ( error && (error instanceof AssertionError || From e568d073265e5c7735c1eb71bd37209d7c80f3a9 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Wed, 15 May 2019 23:49:48 +0200 Subject: [PATCH 08/13] fix the incorrect test case --- .../chaiAssertionLibrary.ts.snap | 20 +++++++++---------- .../__tests__/chai_assertion.js | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap b/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap index 5634e3bfbd3c..0f41413e1750 100644 --- a/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap +++ b/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap @@ -35,30 +35,30 @@ FAIL __tests__/chai_assertion.js assert.(received, expected) - Expected value "hello sunshine" + Expected value "hello world" Received: - "hello world" + "hello sunshine" Message: - expected 'hello world' to equal 'hello sunshine' + expected 'hello sunshine' to equal 'hello world' Difference: - Expected + Received - - hello sunshine - + hello world + - hello world + + hello sunshine - 18 | const stringExpected = 'hello world'; - 19 | const actualExpected = 'hello sunshine'; - > 20 | stringExpected.should.to.equal(actualExpected); - | ^ + 18 | const expectedString = 'hello world'; + 19 | const actualString = 'hello sunshine'; + > 20 | actualString.should.equal(expectedString); + | ^ 21 | }); 22 | 23 | it('assert', () => { - at Object.equal (__tests__/chai_assertion.js:20:30) + at Object.equal (__tests__/chai_assertion.js:20:25) ● chai.js assertion library test › assert diff --git a/e2e/chai-assertion-library-errors/__tests__/chai_assertion.js b/e2e/chai-assertion-library-errors/__tests__/chai_assertion.js index 0e513a890660..3b247bf94f75 100644 --- a/e2e/chai-assertion-library-errors/__tests__/chai_assertion.js +++ b/e2e/chai-assertion-library-errors/__tests__/chai_assertion.js @@ -15,9 +15,9 @@ describe('chai.js assertion library test', () => { it('should', () => { chai.should(); - const stringExpected = 'hello world'; - const actualExpected = 'hello sunshine'; - stringExpected.should.to.equal(actualExpected); + const expectedString = 'hello world'; + const actualString = 'hello sunshine'; + actualString.should.equal(expectedString); }); it('assert', () => { From 0764ed4e7b1d13af3e38be17e63c21ade142408e Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Thu, 16 May 2019 23:24:24 +0200 Subject: [PATCH 09/13] remove assert.(a,b) message if operator not set --- .../__snapshots__/chaiAssertionLibrary.ts.snap | 6 +++--- packages/jest-circus/src/formatNodeAssertErrors.ts | 10 ++++++---- packages/jest-jasmine2/src/assertionErrorMessage.ts | 4 ++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap b/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap index 0f41413e1750..deedee098cc4 100644 --- a/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap +++ b/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap @@ -4,7 +4,7 @@ exports[`chai assertion errors should display properly 1`] = ` FAIL __tests__/chai_assertion.js ● chai.js assertion library test › expect - assert.(received, expected) + Expected value "hello sunshine" Received: @@ -33,7 +33,7 @@ FAIL __tests__/chai_assertion.js ● chai.js assertion library test › should - assert.(received, expected) + Expected value "hello world" Received: @@ -62,7 +62,7 @@ FAIL __tests__/chai_assertion.js ● chai.js assertion library test › assert - assert.(received, expected) + Expected value "hello sunshine" Received: diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index 77222ab6b191..d5384c4af508 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -91,10 +91,12 @@ const operatorMessage = (operator: string | undefined) => { }; const assertThrowingMatcherHint = (operatorName: string) => - chalk.dim('assert') + - chalk.dim('.' + operatorName + '(') + - chalk.red('function') + - chalk.dim(')'); + operatorName + ? chalk.dim('assert') + + chalk.dim('.' + operatorName + '(') + + chalk.red('function') + + chalk.dim(')') + : ''; const assertMatcherHint = ( operator: string | undefined | null, diff --git a/packages/jest-jasmine2/src/assertionErrorMessage.ts b/packages/jest-jasmine2/src/assertionErrorMessage.ts index 8e83b4241df9..6f167afcaadf 100644 --- a/packages/jest-jasmine2/src/assertionErrorMessage.ts +++ b/packages/jest-jasmine2/src/assertionErrorMessage.ts @@ -61,6 +61,10 @@ const assertThrowingMatcherHint = (operatorName: string) => chalk.dim(')'); const assertMatcherHint = (operator: string | null, operatorName: string) => { + if (!operatorName) { + return ''; + } + let message = chalk.dim('assert') + chalk.dim('.' + operatorName + '(') + From 821a795f61b89543b4e408ab5ba2bd55ba3df9f6 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Thu, 16 May 2019 23:43:07 +0200 Subject: [PATCH 10/13] remove assert.(a,b) message if operator not set --- .../jest-circus/src/formatNodeAssertErrors.ts | 18 ++++++++++------- .../src/assertionErrorMessage.ts | 20 +++++++++---------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index d5384c4af508..5d4670540403 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -102,13 +102,17 @@ const assertMatcherHint = ( operator: string | undefined | null, operatorName: string, ) => { - let message = - chalk.dim('assert') + - chalk.dim('.' + operatorName + '(') + - chalk.red('received') + - chalk.dim(', ') + - chalk.green('expected') + - chalk.dim(')'); + let message = ''; + + if (operatorName) { + message = + chalk.dim('assert') + + chalk.dim('.' + operatorName + '(') + + chalk.red('received') + + chalk.dim(', ') + + chalk.green('expected') + + chalk.dim(')'); + } if (operator === '==') { message += diff --git a/packages/jest-jasmine2/src/assertionErrorMessage.ts b/packages/jest-jasmine2/src/assertionErrorMessage.ts index 6f167afcaadf..c252c502df0a 100644 --- a/packages/jest-jasmine2/src/assertionErrorMessage.ts +++ b/packages/jest-jasmine2/src/assertionErrorMessage.ts @@ -61,17 +61,17 @@ const assertThrowingMatcherHint = (operatorName: string) => chalk.dim(')'); const assertMatcherHint = (operator: string | null, operatorName: string) => { - if (!operatorName) { - return ''; - } + let message = ''; - let message = - chalk.dim('assert') + - chalk.dim('.' + operatorName + '(') + - chalk.red('received') + - chalk.dim(', ') + - chalk.green('expected') + - chalk.dim(')'); + if (operatorName) { + message = + chalk.dim('assert') + + chalk.dim('.' + operatorName + '(') + + chalk.red('received') + + chalk.dim(', ') + + chalk.green('expected') + + chalk.dim(')'); + } if (operator === '==') { message += From 69d377839d2756ebf8919d62eba563b3b0418ce3 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Thu, 16 May 2019 23:52:46 +0200 Subject: [PATCH 11/13] fix assert message --- packages/jest-jasmine2/src/assertionErrorMessage.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/jest-jasmine2/src/assertionErrorMessage.ts b/packages/jest-jasmine2/src/assertionErrorMessage.ts index c252c502df0a..cc0610448048 100644 --- a/packages/jest-jasmine2/src/assertionErrorMessage.ts +++ b/packages/jest-jasmine2/src/assertionErrorMessage.ts @@ -55,10 +55,12 @@ const operatorMessage = (operator: string | null) => { }; const assertThrowingMatcherHint = (operatorName: string) => - chalk.dim('assert') + - chalk.dim('.' + operatorName + '(') + - chalk.red('function') + - chalk.dim(')'); + operatorName + ? chalk.dim('assert') + + chalk.dim('.' + operatorName + '(') + + chalk.red('function') + + chalk.dim(')') + : ''; const assertMatcherHint = (operator: string | null, operatorName: string) => { let message = ''; From 173e9c6fe6e7f1c33533d87b2c3c0add0e2f9c93 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Thu, 16 May 2019 23:53:28 +0200 Subject: [PATCH 12/13] fix linting --- packages/jest-jasmine2/src/assertionErrorMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-jasmine2/src/assertionErrorMessage.ts b/packages/jest-jasmine2/src/assertionErrorMessage.ts index cc0610448048..dea1521382d1 100644 --- a/packages/jest-jasmine2/src/assertionErrorMessage.ts +++ b/packages/jest-jasmine2/src/assertionErrorMessage.ts @@ -60,7 +60,7 @@ const assertThrowingMatcherHint = (operatorName: string) => chalk.dim('.' + operatorName + '(') + chalk.red('function') + chalk.dim(')') - : ''; + : ''; const assertMatcherHint = (operator: string | null, operatorName: string) => { let message = ''; From de3815f437c31eed6bb5a80ab3f935d49f4dc9c1 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Fri, 17 May 2019 00:28:48 +0200 Subject: [PATCH 13/13] removed unwanted empty string in final message --- .../__snapshots__/chaiAssertionLibrary.ts.snap | 6 ------ packages/jest-circus/src/formatNodeAssertErrors.ts | 13 +++++++------ packages/jest-jasmine2/src/assertionErrorMessage.ts | 13 +++++++------ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap b/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap index deedee098cc4..1a5f6f2363cc 100644 --- a/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap +++ b/e2e/__tests__/__snapshots__/chaiAssertionLibrary.ts.snap @@ -4,8 +4,6 @@ exports[`chai assertion errors should display properly 1`] = ` FAIL __tests__/chai_assertion.js ● chai.js assertion library test › expect - - Expected value "hello sunshine" Received: "hello world" @@ -33,8 +31,6 @@ FAIL __tests__/chai_assertion.js ● chai.js assertion library test › should - - Expected value "hello world" Received: "hello sunshine" @@ -62,8 +58,6 @@ FAIL __tests__/chai_assertion.js ● chai.js assertion library test › assert - - Expected value "hello sunshine" Received: "hello world" diff --git a/packages/jest-circus/src/formatNodeAssertErrors.ts b/packages/jest-circus/src/formatNodeAssertErrors.ts index 5d4670540403..b8d25c7201c2 100644 --- a/packages/jest-circus/src/formatNodeAssertErrors.ts +++ b/packages/jest-circus/src/formatNodeAssertErrors.ts @@ -140,8 +140,7 @@ function assertionErrorMessage( if (operatorName === 'doesNotThrow') { return ( - assertThrowingMatcherHint(operatorName) + - '\n\n' + + buildHintString(assertThrowingMatcherHint(operatorName)) + chalk.reset(`Expected the function not to throw an error.\n`) + chalk.reset(`Instead, it threw:\n`) + ` ${printReceived(actual)}` + @@ -152,8 +151,7 @@ function assertionErrorMessage( if (operatorName === 'throws') { return ( - assertThrowingMatcherHint(operatorName) + - '\n\n' + + buildHintString(assertThrowingMatcherHint(operatorName)) + chalk.reset(`Expected the function to throw an error.\n`) + chalk.reset(`But it didn't throw anything.`) + chalk.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') + @@ -162,8 +160,7 @@ function assertionErrorMessage( } return ( - assertMatcherHint(operator, operatorName) + - '\n\n' + + buildHintString(assertMatcherHint(operator, operatorName)) + chalk.reset(`Expected value ${operatorMessage(operator)}`) + ` ${printExpected(expected)}\n` + chalk.reset(`Received:\n`) + @@ -185,4 +182,8 @@ function isAssertionError( ); } +function buildHintString(hint: string) { + return hint ? hint + '\n\n' : ''; +} + export default formatNodeAssertErrors; diff --git a/packages/jest-jasmine2/src/assertionErrorMessage.ts b/packages/jest-jasmine2/src/assertionErrorMessage.ts index dea1521382d1..cb51e1476fd9 100644 --- a/packages/jest-jasmine2/src/assertionErrorMessage.ts +++ b/packages/jest-jasmine2/src/assertionErrorMessage.ts @@ -101,8 +101,7 @@ function assertionErrorMessage( if (operatorName === 'doesNotThrow') { return ( - assertThrowingMatcherHint(operatorName) + - '\n\n' + + buildHintString(assertThrowingMatcherHint(operatorName)) + chalk.reset(`Expected the function not to throw an error.\n`) + chalk.reset(`Instead, it threw:\n`) + ` ${printReceived(actual)}` + @@ -113,8 +112,7 @@ function assertionErrorMessage( if (operatorName === 'throws') { return ( - assertThrowingMatcherHint(operatorName) + - '\n\n' + + buildHintString(assertThrowingMatcherHint(operatorName)) + chalk.reset(`Expected the function to throw an error.\n`) + chalk.reset(`But it didn't throw anything.`) + chalk.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') + @@ -123,8 +121,7 @@ function assertionErrorMessage( } return ( - assertMatcherHint(operator, operatorName) + - '\n\n' + + buildHintString(assertMatcherHint(operator, operatorName)) + chalk.reset(`Expected value ${operatorMessage(operator)}`) + ` ${printExpected(expected)}\n` + chalk.reset(`Received:\n`) + @@ -135,4 +132,8 @@ function assertionErrorMessage( ); } +function buildHintString(hint: string) { + return hint ? hint + '\n\n' : ''; +} + export default assertionErrorMessage;