Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snapshot): concatenate name of test and snapshot #4460

Merged
merged 5 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why it is removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because expected now is the testName

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