snapshot
snapshot copied to clipboard
Snapshot file required, deleting it fails the test, even when readFileMaybe is plugged in
How is this working for anyone?
https://github.com/cypress-io/snapshot/blob/2678cd27cbb5e8509efb8ebc100cccb49c847a37/src/index.js#L77
If the file isn't present then I get an error about the file missing. I use the recommended readFileMaybe() returns null which will always fail the is.string(js) assertion. Shouldn't that assertion above be the following?
la(!js || is.string(js), 'expected JavaScript snapshot source', js)
Alternatively, I guess readFileMaybe() can return "null"; instead, so that it is a string content that will eval() to falsey, but that's not a good solution.
Your assumptions are correct, I am experiencing the same thing cy.writeFile is now returning null, which breaks the logic here.
Here is the commit which broke it, https://github.com/cypress-io/cypress/commit/ce8f0ee84f01c2f50fe166f642d5e7720b49d864
Okie. I added this to my cypress/support/index.js
Cypress.Commands.overwrite("writeFile", (origFn, ...args) => {
return origFn(...args).then(() => args[1]);
});
YMMV. This is returning cy.writeFile back to its old behavior which would be a regression in the case of https://github.com/cypress-io/cypress/issues/2466
This issue is probably solved by Pull Request #151
@richclingman any chance that PR can be merged?
Dead repo is dead?
I think this project might be dead. I used this as a hacky workaround to move past this issue. If the file does not exist, I create it with {} as the contents and return '{}'.
on('task', {
readFileMaybe(filename) {
if (fs.existsSync(filename)) {
return fs.readFileSync(filename, 'utf8');
} else if (filename.endsWith('snapshots.js')) { // or whatever name you specify in the config
// Hacky fix to workaround https://github.com/cypress-io/snapshot/issues/122
const emptyContents = '{}';
fs.writeFile(filename, emptyContents, { encoding: 'utf8' });
return emptyContents;
}
return null;
},
});
Since fs.writeFile creates the file asynchronously, you cannot return fs.readFileSync(filename, 'utf8'); because the file may not be created by the time this plugin needs to read it. But it should be created long before the plugin may need to write to it.