Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bruderstein committed Jun 24, 2018
1 parent 111d0f7 commit c3c0ff2
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/helpers/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ const writerOptions = {
};

class UnexpectedSnapshotState {

constructor(snapshotState) {
const files = {};
this._files = files;
}

getSnapshot(testPath, testName, expect, snapshotState) {
let snapshot = this._files[testPath];
if (!snapshot) {
Expand All @@ -64,7 +64,7 @@ class UnexpectedSnapshotState {
return agg;
}, contentOutput)
}

snapshot = this._files[testPath] = {
testCounter: {},
uncheckedKeys: (content && new Set(Object.keys(content))) || new Set(),
Expand All @@ -75,22 +75,22 @@ class UnexpectedSnapshotState {
}
const count = (snapshot.testCounter[testName] || 0) + 1;
snapshot.testCounter[testName] = count;

const keyName = testName + ' ' + count;
snapshot.uncheckedKeys.delete(keyName);
return snapshot.allTests[keyName] || null;
}

saveSnapshot(testPath, testName, tree, expect) {

const snapshotPath = getSnapshotPath(testPath);
const snapshot = this._files[testPath];

// If we've been passed a new tree, update the current snapshot
// Otherwise, we're just saving the file
if (tree) {
const count = snapshot.testCounter[testName] || 1;

snapshot.allTests[testName + ' ' + count] = tree;
snapshot.contentOutput[testName + ' ' + count] = expect.output.clone().annotationBlock(function () {
this.append(expect.inspect(tree));
Expand All @@ -104,15 +104,19 @@ class UnexpectedSnapshotState {
exists = false;
}
if (!exists) {
mkdirp.sync(dir);
try {
mkdirp.sync(dir);
} catch (e) {
// ignore, it may have been created in a separate test
}
}
const fileContent = Object.keys(snapshot.allTests).map(test => {
const display = snapshot.contentOutput[test] || '// Display unavailable (this is probably a bug in unexpected-react, please report it!)';
return `/////////////////// ${test} ///////////////////\n\n${display}\n\nexports[\`${test}\`] = ${jsWriter(snapshot.allTests[test], writerOptions)};\n// ===========================================================================\n`;
}).join('\n\n');
fs.writeFileSync(snapshotPath, fileContent);
}

markTestAsFailed(testPath, testName) {
const snapshot = this._files[testPath];
snapshot.failedTests.add(testName);
Expand All @@ -123,7 +127,7 @@ function getSnapshotPath(testPath) {
const testPathParsed = path.parse(testPath);
testPathParsed.dir = path.join(testPathParsed.dir, '__snapshots__');
testPathParsed.base = testPathParsed.name + '.unexpected-snap';

return path.format(testPathParsed);
}

Expand Down Expand Up @@ -206,22 +210,22 @@ function injectStateHooks() {
return originalGetUncheckedCount.call(snapshotState);
};
const originalRemoveUncheckedKeys = snapshotState.removeUncheckedKeys;

snapshotState.removeUncheckedKeys = function () {
const state = matchers.getState();
let isDirty = false;
const snapshot = state.unexpectedSnapshot && state.unexpectedSnapshot._files[state.testPath];
if (snapshot && snapshot.uncheckedKeys.size) {
snapshot.uncheckedKeys.forEach(key => {
const testName = /(.*)\s[0-9]+$/.exec(key)[1];

if (!snapshot.failedTests.has(testName)) {
isDirty = true;
delete snapshot.allTests[key]
}
});
}

if (!snapshot || Object.keys(snapshot.allTests).length === 0) {
const snapshotPath = getSnapshotPath(state.testPath);
try {
Expand All @@ -231,7 +235,7 @@ function injectStateHooks() {
} catch (e) {
// We're ignoring file-not-found exceptions, and errors deleting
}

if (state.unexpectedSnapshot) {
delete state.unexpectedSnapshot._files[state.testPath];
}
Expand Down

0 comments on commit c3c0ff2

Please sign in to comment.