-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[WIP] Replace jest-snapshot #1223
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const isEqual = require('lodash.isequal'); | ||
const mkdirp = require('mkdirp'); | ||
const globals = require('./globals'); | ||
|
||
class Snapshot { | ||
constructor(testPath, options) { | ||
if (!testPath) { | ||
throw new TypeError('Test file path is required'); | ||
} | ||
|
||
this.dirPath = path.join(path.dirname(testPath), '__snapshots__'); | ||
this.filePath = path.join(this.dirPath, path.basename(testPath) + '.snap'); | ||
this.tests = {}; | ||
this.options = options || {}; | ||
|
||
if (fs.existsSync(this.filePath)) { | ||
this.tests = JSON.parse(fs.readFileSync(this.filePath)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just read the file and ignore any errors: let contents
try {
contents = fs.readFileSync(this.filePath);
} catch (err) {}
this.tests = contents ? JSON.parse(contents) : {} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
} | ||
|
||
save() { | ||
mkdirp.sync(this.dirPath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add something like: let hasTests = false;
for(var key in obj) {
if(obj.hasOwnProperty(key))
hasTests = true;
break;
}
}
if (!hasTests) return; at the beginning of this function. That way we don't end up making a bunch of empty files/divs. We also need to ask ourselves what we want to do if someone deletes a test. Should we delete the snapshots too? Or leave them there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, I forked off and tried this out and it works quite well: TzviPM@c95962a |
||
fs.writeFileSync(this.filePath, JSON.stringify(this.tests, null, ' ')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
match(testTitle, actual) { | ||
const expected = this.tests[testTitle]; | ||
if (!expected || this.options.update) { | ||
this.tests[testTitle] = actual; | ||
|
||
return {pass: true}; | ||
} | ||
|
||
const isMatch = isEqual(actual, expected); | ||
if (isMatch) { | ||
return {pass: true}; | ||
} | ||
|
||
return { | ||
pass: false, | ||
actual, | ||
expected | ||
}; | ||
} | ||
} | ||
|
||
const x = module.exports = Snapshot; | ||
|
||
Snapshot.getSnapshot = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why export the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. Perhaps export both Alternatively you could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to change anything. What is the benefit of this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it better separates the "public" API ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, but the thing is, both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know. Just voicing my preference 😄 |
||
if (!x.snapshot) { | ||
x.snapshot = new Snapshot(globals.options.file, {update: globals.options.updateSnapshots}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you document that this is only loaded in a worker process, so we only need one snapshot instance for the file that worker is testing? |
||
} | ||
|
||
return x.snapshot; | ||
}; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jest convention aside, is there a reason we should stick to this format? Our naming scheme is
helpers
andfixtures
, not__fixtures__
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I'd prefer
snapshots
too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be configurable though? Folks who follow a
__tests__
scheme will want to use__snapshots__
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would really prefer not introducing more options. Can we make it
__snapshots__
if inside a__tests__
directory, otherwisesnapshots
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea that sounds great to me.