Skip to content

Commit

Permalink
feat(snapshot): concatenate name of test and snapshot (#4460)
Browse files Browse the repository at this point in the history
* 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 #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
  • Loading branch information
Haroenv authored and cpojer committed Oct 10, 2017
1 parent c172538 commit 3d996be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
20 changes: 9 additions & 11 deletions packages/jest-snapshot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -125,8 +125,6 @@ const toThrowErrorMatchingSnapshot = function(received: any, expected: void) {
);
}

ensureNoExpected(expected, '.toThrowErrorMatchingSnapshot');

let error;

try {
Expand All @@ -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 = {
Expand Down

0 comments on commit 3d996be

Please sign in to comment.