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

[jest-snapshot] Normalize line endings in serialize function #3002

Merged
merged 1 commit into from
Feb 27, 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
46 changes: 32 additions & 14 deletions integration_tests/__tests__/__snapshots__/snapshot-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,80 @@

exports[`Snapshot Validation deletes a snapshot when a test does removes all the snapshots 1`] = `
"Test Suites: 3 passed, 3 total
Tests: 7 passed, 7 total
Snapshots: 7 added, 7 total
Tests: 9 passed, 9 total
Snapshots: 9 added, 9 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
exports[`Snapshot Validation deletes a snapshot when a test does removes all the snapshots 2`] = `
"Test Suites: 3 passed, 3 total
Tests: 5 passed, 5 total
Snapshots: 4 passed, 4 total
Tests: 6 passed, 6 total
Snapshots: 5 passed, 5 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
exports[`Snapshot Validation deletes the snapshot if the test suite has been removed 1`] = `
"Test Suites: 3 passed, 3 total
Tests: 7 passed, 7 total
Snapshots: 7 added, 7 total
Tests: 9 passed, 9 total
Snapshots: 9 added, 9 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
exports[`Snapshot Validation deletes the snapshot if the test suite has been removed 2`] = `
"Test Suites: 2 passed, 2 total
Tests: 4 passed, 4 total
Snapshots: 4 passed, 4 total
Tests: 5 passed, 5 total
Snapshots: 5 passed, 5 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
exports[`Snapshot Validation updates the snapshot when a test removes some snapshots 1`] = `
"Test Suites: 3 passed, 3 total
Tests: 7 passed, 7 total
Snapshots: 7 added, 7 total
Tests: 9 passed, 9 total
Snapshots: 9 added, 9 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
exports[`Snapshot Validation updates the snapshot when a test removes some snapshots 2`] = `
"Test Suites: 3 passed, 3 total
Tests: 7 passed, 7 total
Snapshots: 1 updated, 5 passed, 6 total
Tests: 9 passed, 9 total
Snapshots: 1 updated, 7 passed, 8 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
exports[`Snapshot Validation works on subsequent runs without \`-u\` 1`] = `
"Test Suites: 3 passed, 3 total
Tests: 9 passed, 9 total
Snapshots: 9 added, 9 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
exports[`Snapshot Validation works on subsequent runs without \`-u\` 2`] = `
"Test Suites: 3 passed, 3 total
Tests: 9 passed, 9 total
Snapshots: 9 passed, 9 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
exports[`Snapshot works as expected 1`] = `
"Test Suites: 2 passed, 2 total
Tests: 4 passed, 4 total
Snapshots: 4 added, 4 total
Tests: 5 passed, 5 total
Snapshots: 5 added, 5 total
Time: <<REPLACED>>
Ran all test suites.
"
Expand Down
43 changes: 31 additions & 12 deletions integration_tests/__tests__/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ describe('Snapshot', () => {
const result = runJest.json('snapshot', []);
const json = result.json;

expect(json.numTotalTests).toBe(4);
expect(json.numPassedTests).toBe(4);
expect(json.numTotalTests).toBe(5);
expect(json.numPassedTests).toBe(5);
expect(json.numFailedTests).toBe(0);
expect(json.numPendingTests).toBe(0);
expect(result.status).toBe(0);
Expand All @@ -107,7 +107,7 @@ describe('Snapshot', () => {
).not.toBe(undefined);

const info = result.stderr.toString();
expect(info).toMatch('4 snapshots written in 2 test suites');
expect(info).toMatch('5 snapshots written in 2 test suites');
expect(extractSummary(info).summary).toMatchSnapshot();
});

Expand Down Expand Up @@ -194,6 +194,25 @@ describe('Snapshot', () => {
fs.writeFileSync(copyOfTestPath, originalTestContent);
});

it('works on subsequent runs without `-u`', () => {
const firstRun = runJest.json('snapshot', []);

const content = require(snapshotOfCopy);
expect(content).not.toBe(undefined);
const secondRun = runJest.json('snapshot', []);

expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(9);
expect(secondRun.json.success).toBe(true);

const infoFR = firstRun.stderr.toString();
const infoSR = secondRun.stderr.toString();
expect(infoFR).toMatch('9 snapshots written in 3 test suites');
expect(infoSR).toMatch('9 passed, 9 total');
expect(extractSummary(infoFR).summary).toMatchSnapshot();
expect(extractSummary(infoSR).summary).toMatchSnapshot();
});

it('deletes the snapshot if the test suite has been removed', () => {
const firstRun = runJest.json('snapshot', []);
fs.unlinkSync(copyOfTestPath);
Expand All @@ -202,13 +221,13 @@ describe('Snapshot', () => {
expect(content).not.toBe(undefined);
const secondRun = runJest.json('snapshot', ['-u']);

expect(firstRun.json.numTotalTests).toBe(7);
expect(secondRun.json.numTotalTests).toBe(4);
expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(5);
expect(fileExists(snapshotOfCopy)).toBe(false);

const infoFR = firstRun.stderr.toString();
const infoSR = secondRun.stderr.toString();
expect(infoFR).toMatch('7 snapshots written in 3 test suites');
expect(infoFR).toMatch('9 snapshots written in 3 test suites');
expect(infoSR).toMatch('1 obsolete snapshot file removed');
expect(extractSummary(infoFR).summary).toMatchSnapshot();
expect(extractSummary(infoSR).summary).toMatchSnapshot();
Expand All @@ -221,13 +240,13 @@ describe('Snapshot', () => {
const secondRun = runJest.json('snapshot', ['-u']);
fs.unlinkSync(copyOfTestPath);

expect(firstRun.json.numTotalTests).toBe(7);
expect(secondRun.json.numTotalTests).toBe(5);
expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(6);

expect(fileExists(snapshotOfCopy)).toBe(false);
const infoFR = firstRun.stderr.toString();
const infoSR = secondRun.stderr.toString();
expect(infoFR).toMatch('7 snapshots written in 3 test suites');
expect(infoFR).toMatch('9 snapshots written in 3 test suites');
expect(infoSR).toMatch('1 obsolete snapshot file removed');
expect(extractSummary(infoFR).summary).toMatchSnapshot();
expect(extractSummary(infoSR).summary).toMatchSnapshot();
Expand All @@ -246,8 +265,8 @@ describe('Snapshot', () => {
const secondRun = runJest.json('snapshot', ['-u']);
fs.unlinkSync(copyOfTestPath);

expect(firstRun.json.numTotalTests).toBe(7);
expect(secondRun.json.numTotalTests).toBe(7);
expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(9);
expect(fileExists(snapshotOfCopy)).toBe(true);
const afterRemovingSnapshot = getSnapshotOfCopy();

Expand All @@ -268,7 +287,7 @@ describe('Snapshot', () => {

const infoFR = firstRun.stderr.toString();
const infoSR = secondRun.stderr.toString();
expect(infoFR).toMatch('7 snapshots written in 3 test suites');
expect(infoFR).toMatch('9 snapshots written in 3 test suites');
expect(extractSummary(infoFR).summary).toMatchSnapshot();
expect(infoSR).toMatch('1 snapshot updated in 1 test suite');
expect(infoSR).toMatch('1 obsolete snapshot removed');
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/snapshot/__tests__/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ describe('snapshot', () => {
'Jest: `.not` cannot be used with `.toMatchSnapshot()`.'
);
});

// Issue reported here: https://github.com/facebook/jest/issues/2969
it('works with \\r\\n', () => {
expect('<div>\r\n</div>').toMatchSnapshot();
});
});
9 changes: 9 additions & 0 deletions packages/jest-snapshot/src/__tests__/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
getSnapshotPath,
keyToTestName,
saveSnapshotFile,
serialize,
testNameToKey,
SNAPSHOT_GUIDE_LINK,
SNAPSHOT_VERSION,
Expand Down Expand Up @@ -195,3 +196,11 @@ test('escaping', () => {
const snapshotData = readData.key;
expect(data).toEqual(snapshotData);
});

test('serialize handles \\r\\n', () => {
const data = '<div>\r\n</div>';
const serializedData = serialize(data);

expect(serializedData)
.toBe('\n\"<div>\n</div>\"\n');
});
12 changes: 7 additions & 5 deletions packages/jest-snapshot/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ const addExtraLineBreaks =
string => string.includes('\n') ? `\n${string}\n` : string;

const serialize = (data: any): string => {
return addExtraLineBreaks(prettyFormat(data, {
escapeRegex: true,
plugins: getSerializers(),
printFunctionName: false,
}));
return addExtraLineBreaks(normalizeNewlines(
prettyFormat(data, {
escapeRegex: true,
plugins: getSerializers(),
printFunctionName: false,
})
));
};

const unescape = (data: any): string =>
Expand Down