Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Add updateSnapshot CLI arg for updating snapshots #75

Merged
merged 1 commit into from
Dec 16, 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
5 changes: 5 additions & 0 deletions build/test-app-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports.TestAppRuntime = function({
watch = false,
match,
env,
updateSnapshot,
configPath,
}) {
const state = {procs: []};
Expand All @@ -27,6 +28,10 @@ module.exports.TestAppRuntime = function({
args.push(match);
}

if (updateSnapshot) {
args.push('--updateSnapshot');
}

return args;
};

Expand Down
23 changes: 21 additions & 2 deletions commands/test-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,34 @@ exports.builder = {
describe:
'Comma-separated list of environments to run tests in. Defaults to running both node and browser tests.',
},
updateSnapshot: {
type: 'boolean',
default: false,
describe: 'Updates snapshots',
},
configPath: {
type: 'string',
default: './node_modules/fusion-cli/build/jest-config.js',
describe: 'Path to the jest configuration',
},
};

exports.run = async function({dir = '.', watch, match, env, configPath}) {
const testRuntime = new TestAppRuntime({dir, watch, match, env, configPath});
exports.run = async function({
dir = '.',
watch,
match,
env,
updateSnapshot,
configPath,
}) {
const testRuntime = new TestAppRuntime({
dir,
watch,
match,
env,
updateSnapshot,
configPath,
});

await testRuntime.run();

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"prettier": "1.4.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-test-renderer": "^16.2.0",
"request-promise": "^4.2.1",
"strip-indent": "^2.0.0",
"tape": "^4.7.0"
Expand Down
33 changes: 33 additions & 0 deletions test/cli/test-app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env node */

const fs = require('fs');
const path = require('path');
const test = require('tape');

Expand Down Expand Up @@ -69,3 +70,35 @@ test('`fusion test-app` expected tests fail when run in browser/node', async t =
t.end();
}
});

test('`fusion test-app` snapshotting', async t => {
const dir = path.resolve(__dirname, '../fixtures/test-jest-app');
const args = `test-app --dir=${dir} --configPath=../../../build/jest-config.js --match=snapshot-no-match`;

const snapshotFile =
__dirname +
'/../fixtures/test-jest-app/__tests__/__snapshots__/snapshot-no-match.js.snap';
const backupSnapshot =
__dirname + '/../fixtures/snapshots/snapshot-no-match.js.snap';

const cmd = `require('${runnerPath}').run('${args}')`;
try {
await exec(`node -e "${cmd}"`);
t.fail('should not succeed');
} catch (e) {
t.notEqual(e.code, 0, 'exits with non-zero status code');
t.equal(countTests(e.message), 2, 'ran 2 tests');
}

const updateSnapshot = `require('${runnerPath}').run('${args} --updateSnapshot')`;
await exec(`node -e "${updateSnapshot}"`);

const newSnapshotCode = fs.readFileSync(snapshotFile);
const originalSnapshotCode = fs.readFileSync(backupSnapshot);
t.notEqual(newSnapshotCode, originalSnapshotCode, 'snapshot is updated');

// Restore the failing snapshot
fs.createReadStream(backupSnapshot).pipe(fs.createWriteStream(snapshotFile));

t.end();
});
3 changes: 3 additions & 0 deletions test/fixtures/snapshots/snapshot-no-match.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
Copy link

Choose a reason for hiding this comment

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

Possible to not have these comments in here? Having some non-jest-specific api for snapshot testing will allow us to change the underlying implementation in the future, if we want to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking into it, but I don't believe so. I bet we could codemod the snapshot engine though if needed.


exports[`Does not match snapshot 1`] = `<div />`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Does not match snapshot 1`] = `<div />`;
10 changes: 10 additions & 0 deletions test/fixtures/test-jest-app/__tests__/snapshot-no-match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import renderer from 'react-test-renderer';
import {test} from 'fusion-test-utils';

test('Does not match snapshot', assert => {
const tree = renderer
.create(<div data-should-fail />)
.toJSON();
assert.matchSnapshot(tree);
});
1 change: 1 addition & 0 deletions test/fixtures/test-jest-app/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5463,7 +5463,7 @@ react-proxy@^3.0.0-alpha.0:
dependencies:
lodash "^4.6.1"

react-test-renderer@^16.0.0-0:
react-test-renderer@^16.0.0-0, react-test-renderer@^16.2.0:
version "16.2.0"
resolved "https://registry.npmjs.org/react-test-renderer/-/react-test-renderer-16.2.0.tgz#bddf259a6b8fcd8555f012afc8eacc238872a211"
dependencies:
Expand Down