From 3d996bec44265a0b842fbdd737ff1e6c3b824955 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Tue, 10 Oct 2017 12:46:29 +0200 Subject: [PATCH] feat(snapshot): concatenate name of test and snapshot (#4460) * feat(snapshot): concatenate name of test and snapshot I find the context of the test name and the snapshot name very useful in cases where you have multiple different Meanwhile I also added the option to add a testName to `toThrowErrorMatchingSnapshot`. I inspired from https://github.com/facebook/jest/pull/2094, but couldn't really find back where the exact tests are I need to change for this to work. Let me know if I'm on the correct path * update test * fix: make sure currentTestName exists --- .../to_throw_error_matching_snapshot.test.js | 12 +++++------ packages/jest-snapshot/src/index.js | 20 +++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/integration_tests/__tests__/to_throw_error_matching_snapshot.test.js b/integration_tests/__tests__/to_throw_error_matching_snapshot.test.js index 78ad60584084..e304b3a2cd16 100644 --- a/integration_tests/__tests__/to_throw_error_matching_snapshot.test.js +++ b/integration_tests/__tests__/to_throw_error_matching_snapshot.test.js @@ -48,19 +48,19 @@ test(`throws the error if tested function didn't throw error`, () => { } }); -test('does not accept arguments', () => { - const filename = 'does-not-accept-arguments.test.js'; - const template = makeTemplate(`test('does not accept arguments', () => { +test('accepts custom snapshot name', () => { + const filename = 'accept-custom-snapshot-name.test.js'; + const template = makeTemplate(`test('accepts custom snapshot name', () => { expect(() => { throw new Error('apple'); }) - .toThrowErrorMatchingSnapshot('foobar'); + .toThrowErrorMatchingSnapshot('custom-name'); }); `); { writeFiles(TESTS_DIR, {[filename]: template()}); const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]); - expect(stderr).toMatch('Matcher does not accept any arguments.'); - expect(status).toBe(1); + expect(stderr).toMatch('1 snapshot written in 1 test suite.'); + expect(status).toBe(0); } }); diff --git a/packages/jest-snapshot/src/index.js b/packages/jest-snapshot/src/index.js index bd59c992baa5..69c9ead962b6 100644 --- a/packages/jest-snapshot/src/index.js +++ b/packages/jest-snapshot/src/index.js @@ -14,12 +14,7 @@ import type {Path, SnapshotUpdateState} from 'types/Config'; import fs from 'fs'; import path from 'path'; import diff from 'jest-diff'; -import { - EXPECTED_COLOR, - ensureNoExpected, - matcherHint, - RECEIVED_COLOR, -} from 'jest-matcher-utils'; +import {EXPECTED_COLOR, matcherHint, RECEIVED_COLOR} from 'jest-matcher-utils'; import SnapshotState from './State'; import {addSerializer, getSerializers} from './plugins'; import * as utils from './utils'; @@ -67,7 +62,9 @@ const toMatchSnapshot = function(received: any, testName?: string) { } const result = snapshotState.match( - testName || currentTestName || '', + testName && currentTestName + ? `${currentTestName}: ${testName}` + : currentTestName || '', received, ); const {count, pass} = result; @@ -115,7 +112,10 @@ const toMatchSnapshot = function(received: any, testName?: string) { }; }; -const toThrowErrorMatchingSnapshot = function(received: any, expected: void) { +const toThrowErrorMatchingSnapshot = function( + received: any, + testName?: string, +) { this.dontThrow && this.dontThrow(); const {isNot} = this; @@ -125,8 +125,6 @@ const toThrowErrorMatchingSnapshot = function(received: any, expected: void) { ); } - ensureNoExpected(expected, '.toThrowErrorMatchingSnapshot'); - let error; try { @@ -144,7 +142,7 @@ const toThrowErrorMatchingSnapshot = function(received: any, expected: void) { ); } - return toMatchSnapshot.call(this, error.message); + return toMatchSnapshot.call(this, error.message, testName); }; module.exports = {