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 = {