Skip to content

Commit

Permalink
Tests + updateSnapshot fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed May 4, 2017
1 parent 36cee44 commit 4ff1379
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 100 deletions.
11 changes: 10 additions & 1 deletion integration_tests/__tests__/__snapshots__/snapshot-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ Ran all test suites.
"
`;
exports[`Snapshot Validation does not save snapshots in CI mode by default 1`] = `
"Test Suites: 3 failed, 3 total
Tests: 7 failed, 2 passed, 9 total
Snapshots: 9 failed, 9 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: 9 passed, 9 total
Expand Down Expand Up @@ -72,7 +81,7 @@ Ran all test suites.
"
`;
exports[`Snapshot works as expected 1`] = `
exports[`Snapshot stores new snapshots on the first run 1`] = `
"Test Suites: 2 passed, 2 total
Tests: 5 passed, 5 total
Snapshots: 5 added, 5 total
Expand Down
6 changes: 5 additions & 1 deletion integration_tests/__tests__/snapshot-serializers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const snapshotsDir = path.resolve(testDir, '__tests__/__snapshots__');
const snapshotPath = path.resolve(snapshotsDir, 'snapshot-test.js.snap');

const runAndAssert = () => {
const result = runJest.json('snapshot-serializers', ['--no-cache']);
const result = runJest.json('snapshot-serializers', [
'-w=1',
'--ci=false',
'--no-cache',
]);
const json = result.json;
expect(json.numTotalTests).toBe(7);
expect(json.numPassedTests).toBe(7);
Expand Down
64 changes: 50 additions & 14 deletions integration_tests/__tests__/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ describe('Snapshot', () => {
beforeEach(cleanup);
afterAll(cleanup);

it('works as expected', () => {
const result = runJest.json('snapshot', []);
it('stores new snapshots on the first run', () => {
const result = runJest.json('snapshot', ['-w=1', '--ci=false']);
const json = result.json;

expect(json.numTotalTests).toBe(5);
Expand All @@ -122,7 +122,11 @@ describe('Snapshot', () => {

it('works with escaped characters', () => {
// Write the first snapshot
let result = runJest('snapshot-escape', ['snapshot-test.js']);
let result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-test.js',
]);
let stderr = result.stderr.toString();

expect(stderr).toMatch('1 snapshot written');
Expand All @@ -136,7 +140,12 @@ describe('Snapshot', () => {
const newTestData = initialTestData + testData;
fs.writeFileSync(snapshotEscapeTestFile, newTestData, 'utf8');

result = runJest('snapshot-escape', ['snapshot-test.js']);
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'--updateSnapshot',
'snapshot-test.js',
]);
stderr = result.stderr.toString();

expect(stderr).toMatch('1 snapshot written');
Expand All @@ -145,7 +154,11 @@ describe('Snapshot', () => {

// Now let's check again if everything still passes.
// If this test doesn't pass, some snapshot data was not properly escaped.
result = runJest('snapshot-escape', ['snapshot-test.js']);
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-test.js',
]);
stderr = result.stderr.toString();

expect(stderr).not.toMatch('Snapshot Summary');
Expand All @@ -155,14 +168,22 @@ describe('Snapshot', () => {

it('works with escaped regex', () => {
// Write the first snapshot
let result = runJest('snapshot-escape', ['snapshot-escape-regex.js']);
let result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-escape-regex.js',
]);
let stderr = result.stderr.toString();

expect(stderr).toMatch('2 snapshots written in 1 test suite.');
expect(result.status).toBe(0);
expect(extractSummary(stderr).summary).toMatchSnapshot();

result = runJest('snapshot-escape', ['snapshot-escape-regex.js']);
result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-escape-regex.js',
]);
stderr = result.stderr.toString();

// Make sure we aren't writing a snapshot this time which would
Expand All @@ -175,6 +196,8 @@ describe('Snapshot', () => {
it('works with template literal subsitutions', () => {
// Write the first snapshot
let result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-escape-substitution-test.js',
]);
let stderr = result.stderr.toString();
Expand All @@ -184,6 +207,8 @@ describe('Snapshot', () => {
expect(extractSummary(stderr).summary).toMatchSnapshot();

result = runJest('snapshot-escape', [
'-w=1',
'--ci=false',
'snapshot-escape-substitution-test.js',
]);
stderr = result.stderr.toString();
Expand All @@ -200,8 +225,19 @@ describe('Snapshot', () => {
fs.writeFileSync(copyOfTestPath, originalTestContent);
});

it('does not save snapshots in CI mode by default', () => {
const result = runJest.json('snapshot', ['-w=1', '--ci=true']);

expect(result.json.success).toBe(false);
expect(result.json.numTotalTests).toBe(9);
expect(result.json.snapshot.added).toBe(0);
expect(result.json.snapshot.total).toBe(9);
const stderr = result.stderr.toString();
expect(extractSummary(stderr).summary).toMatchSnapshot();
});

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

const content = require(snapshotOfCopy);
expect(content).not.toBe(undefined);
Expand All @@ -220,12 +256,12 @@ describe('Snapshot', () => {
});

it('deletes the snapshot if the test suite has been removed', () => {
const firstRun = runJest.json('snapshot', []);
const firstRun = runJest.json('snapshot', ['-w=1', '--ci=false']);
fs.unlinkSync(copyOfTestPath);

const content = require(snapshotOfCopy);
expect(content).not.toBe(undefined);
const secondRun = runJest.json('snapshot', ['-u']);
const secondRun = runJest.json('snapshot', ['-w=1', '--ci=false', '-u']);

expect(firstRun.json.numTotalTests).toBe(9);
expect(secondRun.json.numTotalTests).toBe(5);
Expand All @@ -240,10 +276,10 @@ describe('Snapshot', () => {
});

it('deletes a snapshot when a test does removes all the snapshots', () => {
const firstRun = runJest.json('snapshot', []);
const firstRun = runJest.json('snapshot', ['-w=1', '--ci=false']);

fs.writeFileSync(copyOfTestPath, emptyTest);
const secondRun = runJest.json('snapshot', ['-u']);
const secondRun = runJest.json('snapshot', ['-w=1', '--ci=false', '-u']);
fs.unlinkSync(copyOfTestPath);

expect(firstRun.json.numTotalTests).toBe(9);
Expand All @@ -259,7 +295,7 @@ describe('Snapshot', () => {
});

it('updates the snapshot when a test removes some snapshots', () => {
const firstRun = runJest.json('snapshot', []);
const firstRun = runJest.json('snapshot', ['-w=1', '--ci=false']);
fs.unlinkSync(copyOfTestPath);
const beforeRemovingSnapshot = getSnapshotOfCopy();

Expand All @@ -270,7 +306,7 @@ describe('Snapshot', () => {
'.not.toBe(undefined)',
),
);
const secondRun = runJest.json('snapshot', ['-u']);
const secondRun = runJest.json('snapshot', ['-w=1', '--ci=false', '-u']);
fs.unlinkSync(copyOfTestPath);

expect(firstRun.json.numTotalTests).toBe(9);
Expand Down
53 changes: 25 additions & 28 deletions integration_tests/__tests__/toMatchSnapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,81 +26,82 @@ test('basic support', () => {

{
makeTests(TESTS_DIR, {[filename]: template(['{apple: "original value"}'])});
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('1 snapshot written in 1 test suite.');
expect(status).toBe(0);
}

{
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Snapshots: 1 passed, 1 total');
expect(stderr).not.toMatch('1 snapshot written in 1 test suite.');
expect(status).toBe(0);
}

{
makeTests(TESTS_DIR, {[filename]: template(['{apple: "updated value"}'])});
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Received value does not match stored snapshot');
expect(status).toBe(1);
}

{
const {stderr, status} = runJest(DIR, [filename, '-u']);
const {stderr, status} = runJest(DIR, [
'-w=1',
'--ci=false',
filename,
'-u',
]);
expect(stderr).toMatch('1 snapshot updated in 1 test suite.');
expect(status).toBe(0);
}
});

test('error thrown before snapshot', () => {
const filename = 'error-thrown-before-snapshot-test.js';
const template = makeTemplate(
`test('snapshots', () => {
const template = makeTemplate(`test('snapshots', () => {
expect($1).toBeTruthy();
expect($2).toMatchSnapshot();
});`,
);
});`);

{
makeTests(TESTS_DIR, {[filename]: template(['true', '{a: "original"}'])});
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('1 snapshot written in 1 test suite.');
expect(status).toBe(0);
}

{
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Snapshots: 1 passed, 1 total');
expect(status).toBe(0);
}

{
makeTests(TESTS_DIR, {[filename]: template(['false', '{a: "original"}'])});
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).not.toMatch('1 obsolete snapshot found');
expect(status).toBe(1);
}
});

test('first snapshot fails, second passes', () => {
const filename = 'first-snapshot-fails-second-passes-test.js';
const template = makeTemplate(
`test('snapshots', () => {
const template = makeTemplate(`test('snapshots', () => {
expect($1).toMatchSnapshot();
expect($2).toMatchSnapshot();
});`,
);
});`);

{
makeTests(TESTS_DIR, {[filename]: template([`'apple'`, `'banana'`])});
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('2 snapshots written in 1 test suite.');
expect(status).toBe(0);
}

{
makeTests(TESTS_DIR, {[filename]: template([`'kiwi'`, `'banana'`])});
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Received value does not match stored snapshot');
expect(stderr).toMatch('- "apple"\n + "kiwi"');
expect(stderr).not.toMatch('1 obsolete snapshot found');
Expand All @@ -110,44 +111,40 @@ test('first snapshot fails, second passes', () => {

test('does not mark snapshots as obsolete in skipped tests', () => {
const filename = 'no-obsolete-if-skipped-test.js';
const template = makeTemplate(
`test('snapshots', () => {
const template = makeTemplate(`test('snapshots', () => {
expect(true).toBe(true);
});
$1('will be skipped', () => {
expect({a: 6}).toMatchSnapshot();
});
`,
);
`);

{
makeTests(TESTS_DIR, {[filename]: template(['test'])});
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('1 snapshot written in 1 test suite.');
expect(status).toBe(0);
}

{
makeTests(TESTS_DIR, {[filename]: template(['test.skip'])});
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).not.toMatch('1 obsolete snapshot found');
expect(status).toBe(0);
}
});

test('accepts custom snapshot name', () => {
const filename = 'accept-custom-snapshot-name-test.js';
const template = makeTemplate(
`test('accepts custom snapshot name', () => {
const template = makeTemplate(`test('accepts custom snapshot name', () => {
expect(true).toMatchSnapshot('custom-name');
});
`,
);
`);

{
makeTests(TESTS_DIR, {[filename]: template()});
const {stderr, status} = runJest(DIR, [filename]);
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('1 snapshot written in 1 test suite.');
expect(status).toBe(0);
}
Expand Down
Loading

0 comments on commit 4ff1379

Please sign in to comment.