Skip to content
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
10 changes: 8 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,15 @@ describe('looking for a new house', () => {
```


### `.toMatchSnapshot()`
### `.toMatchSnapshot(?string)`

This ensures that a React component matches the most recent snapshot. Check out [the React + Jest tutorial](https://facebook.github.io/jest/docs/tutorial-react.html) for more information on snapshot testing.
This ensures that a value matches the most recent snapshot. Check out [the React + Jest tutorial](https://facebook.github.io/jest/docs/tutorial-react.html) for more information on snapshot testing.

You can also specify an optional snapshot name. Otherwise, the name is inferred
from the test.

*Note: While snapshot testing is most commonly used with React components, any
serializable value can be used as a snapshot.*

### `.toThrow()`

Expand Down
12 changes: 6 additions & 6 deletions integration_tests/__tests__/toMatchSnapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ test('does not mark snapshots as obsolete in skipped tests', () => {
}
});

test('does not accept arguments', () => {
const filename = 'does-not-accept-arguments-test.js';
test('accepts custom snapshot name', () => {
const filename = 'accept-custom-snapshot-name-test.js';
const template = makeTemplate(
`test('does not accept arguments', () => {
expect(true).toMatchSnapshot('foobar');
`test('accepts custom snapshot name', () => {
expect(true).toMatchSnapshot('custom-name');
});
`,
);
Expand All @@ -154,8 +154,8 @@ test('does not accept arguments', () => {
makeTests(TESTS_DIR, {[filename]: template()});
const {stderr, status} = runJest(DIR, [filename]);
expect(stderr).toMatch(
'Matcher does not accept any arguments.',
'1 snapshot written in 1 test suite.'
);
expect(status).toBe(1);
expect(status).toBe(0);
}
});
6 changes: 2 additions & 4 deletions packages/jest-snapshot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const initializeSnapshotState = (

const getSnapshotState = () => snapshotState;

const toMatchSnapshot = function(received: any, expected: void) {
const toMatchSnapshot = function(received: any, testName?: string) {
this.dontThrow && this.dontThrow();

const {currentTestName, isNot, snapshotState} = this;
Expand All @@ -73,13 +73,11 @@ const toMatchSnapshot = function(received: any, expected: void) {
);
}

ensureNoExpected(expected, '.toMatchSnapshot');

if (!snapshotState) {
throw new Error('Jest: snapshot state must be initialized.');
}

const result = snapshotState.match(currentTestName, received);
const result = snapshotState.match(testName || currentTestName, received);
const {pass} = result;

if (pass) {
Expand Down