Skip to content

Commit

Permalink
Determine snapshot directory by where the test is located
Browse files Browse the repository at this point in the history
Tests inside a `__tests__` directory have their snapshots written to a
`__snapshots__` directory.

Tests inside a `test` or `tests` directory have their snapshots written
to a `snapshots` directory.

All other tests have their snapshots colocated.
  • Loading branch information
novemberborn committed Jun 25, 2017
1 parent dbc78dc commit ebd572a
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 39 deletions.
13 changes: 7 additions & 6 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ class Runner extends EventEmitter {

compareTestSnapshot(options) {
if (!this.snapshots) {
const name = path.basename(this.file);
// TODO: Only use __snapshots__ directory name if file is inside a
// __tests__ directory (relative to the project root).
const dir = path.join(path.dirname(this.file), '__snapshots__');
const relFile = path.relative(this.projectDir, this.file);
this.snapshots = snapshotManager.load(dir, name, relFile, this.updateSnapshots);
this.snapshots = snapshotManager.load({
name: path.basename(this.file),
projectDir: this.projectDir,
relFile: path.relative(this.projectDir, this.file),
testDir: path.dirname(this.file),
updating: this.updateSnapshots
});
this.emit('dependency', this.snapshots.snapPath);
}

Expand Down
23 changes: 17 additions & 6 deletions lib/snapshot-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,26 @@ class Manager {
}
}

function load(dir, name, relFile, updating) {
const reportFile = `${name}.md`;
const snapFile = `${name}.snap`;
function determineSnapshotDir(projectDir, testDir) {
const parts = new Set(path.relative(projectDir, testDir).split(path.sep));
if (parts.has('__tests__')) {
return path.join(testDir, '__snapshots__');
} else if (parts.has('test') || parts.has('tests')) { // Accept tests, even though it's not in the default test patterns
return path.join(testDir, 'snapshots');
}
return testDir;
}

function load(options) {
const dir = determineSnapshotDir(options.projectDir, options.testDir);
const reportFile = `${options.name}.md`;
const snapFile = `${options.name}.snap`;
const snapPath = path.join(dir, snapFile);

let appendOnly = !updating;
let appendOnly = !options.updating;
let snapshotsByHash;

if (!updating) {
if (!options.updating) {
const buffer = tryRead(snapPath);
if (buffer) {
snapshotsByHash = decodeSnapshots(buffer, snapPath);
Expand All @@ -374,7 +385,7 @@ function load(dir, name, relFile, updating) {
return new Manager({
appendOnly,
dir,
relFile,
relFile: options.relFile,
reportFile,
snapFile,
snapPath,
Expand Down
21 changes: 14 additions & 7 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,16 @@ test('.snapshot()', t => {
// "$(npm bin)"/tap --no-cov -R spec test/assert.js
//
// Ignore errors and make sure not to run tests with the `-b` (bail) option.
const update = false;
const updating = false;

const manager = snapshotManager.load(path.join(__dirname, 'fixture'), 'assert.js', 'test/assert.js', update);
const projectDir = path.join(__dirname, 'fixture');
const manager = snapshotManager.load({
projectDir,
testDir: projectDir,
name: 'assert.js',
relFile: 'test/assert.js',
updating
});
const setup = title => {
const fauxTest = new Test({
title,
Expand All @@ -760,7 +767,7 @@ test('.snapshot()', t => {

{
const executionContext = setup('fails');
if (update) {
if (updating) {
assertions.snapshot.call(executionContext, {foo: 'bar'});
} else {
failsWith(t, () => {
Expand All @@ -784,7 +791,7 @@ test('.snapshot()', t => {

{
const executionContext = setup('fails');
if (update) {
if (updating) {
assertions.snapshot.call(executionContext, {foo: 'bar'}, 'my message');
} else {
failsWith(t, () => {
Expand All @@ -799,7 +806,7 @@ test('.snapshot()', t => {

{
const executionContext = setup('rendered comparison');
if (update) {
if (updating) {
assertions.snapshot.call(executionContext, renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON());
} else {
passes(t, () => {
Expand All @@ -810,7 +817,7 @@ test('.snapshot()', t => {

{
const executionContext = setup('rendered comparison');
if (update) {
if (updating) {
assertions.snapshot.call(executionContext, renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON());
} else {
failsWith(t, () => {
Expand All @@ -825,7 +832,7 @@ test('.snapshot()', t => {

{
const executionContext = setup('element comparison');
if (update) {
if (updating) {
assertions.snapshot.call(executionContext, React.createElement(HelloMessage, {name: 'Sindre'}));
} else {
failsWith(t, () => {
Expand Down
50 changes: 30 additions & 20 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ test('watcher reruns test files when snapshot dependencies change', t => {
} else {
passedFirst = true;
setTimeout(() => {
touch.sync(path.join(__dirname, 'fixture/snapshots/__snapshots__/test.js.snap'));
touch.sync(path.join(__dirname, 'fixture/snapshots/test.js.snap'));
}, 500);
}
}
Expand Down Expand Up @@ -556,29 +556,39 @@ test('promise tests fail if event loop empties before they\'re resolved', t => {
});
});

test('snapshots work', t => {
try {
fs.unlinkSync(path.join(__dirname, 'fixture', 'snapshots', '__snapshots__', 'test.js.snap'));
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
for (const obj of [
{type: 'colocated', rel: '', dir: ''},
{type: '__tests__', rel: '__tests__-dir', dir: '__tests__/__snapshots__'},
{type: 'test', rel: 'test-dir', dir: 'test/snapshots'},
{type: 'tests', rel: 'tests-dir', dir: 'tests/snapshots'}
]) {
test(`snapshots work (${obj.type})`, t => {
const snapPath = path.join(__dirname, 'fixture', 'snapshots', obj.rel, obj.dir, 'test.js.snap');
try {
fs.unlinkSync(snapPath);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}

// Test should pass, and a snapshot gets written
execCli(['--update-snapshots', 'test.js'], {dirname: 'fixture/snapshots'}, err => {
t.ifError(err);

// Test should pass, and the snapshot gets used
execCli(['test.js'], {dirname: 'fixture/snapshots'}, err => {
const dirname = path.join('fixture/snapshots', obj.rel);
// Test should pass, and a snapshot gets written
execCli(['--update-snapshots'], {dirname}, err => {
t.ifError(err);
t.end();
t.true(fs.existsSync(snapPath));

// Test should pass, and the snapshot gets used
execCli([], {dirname}, err => {
t.ifError(err);
t.end();
});
});
});
});
}

test('outdated snapshot version is reported to the console', t => {
const snapPath = path.join(__dirname, 'fixture', 'snapshots', '__snapshots__', 'test.js.snap');
const snapPath = path.join(__dirname, 'fixture', 'snapshots', 'test.js.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0x00, 0x00]));

execCli(['test.js'], {dirname: 'fixture/snapshots'}, (err, stdout, stderr) => {
Expand All @@ -592,7 +602,7 @@ test('outdated snapshot version is reported to the console', t => {
});

test('newer snapshot version is reported to the console', t => {
const snapPath = path.join(__dirname, 'fixture', 'snapshots', '__snapshots__', 'test.js.snap');
const snapPath = path.join(__dirname, 'fixture', 'snapshots', 'test.js.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0xFF, 0xFF]));

execCli(['test.js'], {dirname: 'fixture/snapshots'}, (err, stdout, stderr) => {
Expand All @@ -606,7 +616,7 @@ test('newer snapshot version is reported to the console', t => {
});

test('snapshot corruption is reported to the console', t => {
const snapPath = path.join(__dirname, 'fixture', 'snapshots', '__snapshots__', 'test.js.snap');
const snapPath = path.join(__dirname, 'fixture', 'snapshots', 'test.js.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0x01, 0x00]));

execCli(['test.js'], {dirname: 'fixture/snapshots'}, (err, stdout, stderr) => {
Expand All @@ -620,7 +630,7 @@ test('snapshot corruption is reported to the console', t => {
});

test('legacy snapshot files are reported to the console', t => {
const snapPath = path.join(__dirname, 'fixture', 'snapshots', '__snapshots__', 'test.js.snap');
const snapPath = path.join(__dirname, 'fixture', 'snapshots', 'test.js.snap');
fs.writeFileSync(snapPath, Buffer.from('// Jest Snapshot v1, https://goo.gl/fbAQLP\n'));

execCli(['test.js'], {dirname: 'fixture/snapshots'}, (err, stdout, stderr) => {
Expand Down
3 changes: 3 additions & 0 deletions test/fixture/snapshots/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.snap
*.md
snapshots
__snapshots__
11 changes: 11 additions & 0 deletions test/fixture/snapshots/__tests__-dir/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from '../../../../..';

test('test title', t => {
t.snapshot({foo: 'bar'});

t.snapshot({answer: 42});
});

test('another test', t => {
t.snapshot(new Map());
});
1 change: 1 addition & 0 deletions test/fixture/snapshots/__tests__-dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/fixture/snapshots/test-dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
11 changes: 11 additions & 0 deletions test/fixture/snapshots/test-dir/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from '../../../../..';

test('test title', t => {
t.snapshot({foo: 'bar'});

t.snapshot({answer: 42});
});

test('another test', t => {
t.snapshot(new Map());
});
5 changes: 5 additions & 0 deletions test/fixture/snapshots/tests-dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ava": {
"files": "tests"
}
}
11 changes: 11 additions & 0 deletions test/fixture/snapshots/tests-dir/tests/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from '../../../../..';

test('test title', t => {
t.snapshot({foo: 'bar'});

t.snapshot({answer: 42});
});

test('another test', t => {
t.snapshot(new Map());
});

0 comments on commit ebd572a

Please sign in to comment.