snapshot icon indicating copy to clipboard operation
snapshot copied to clipboard

Snapshot file required, deleting it fails the test, even when readFileMaybe is plugged in

Open JasonKleban opened this issue 4 years ago • 7 comments

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.

JasonKleban avatar Feb 19 '21 23:02 JasonKleban

Your assumptions are correct, I am experiencing the same thing cy.writeFile is now returning null, which breaks the logic here.

robby-p avatar Apr 23 '21 20:04 robby-p

Here is the commit which broke it, https://github.com/cypress-io/cypress/commit/ce8f0ee84f01c2f50fe166f642d5e7720b49d864

robby-p avatar Apr 23 '21 20:04 robby-p

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

robby-p avatar Apr 23 '21 20:04 robby-p

This issue is probably solved by Pull Request #151

richclingman avatar Aug 20 '21 03:08 richclingman

@richclingman any chance that PR can be merged?

vamcs avatar Jan 17 '23 18:01 vamcs

Dead repo is dead?

cloakedninjas avatar Mar 09 '23 10:03 cloakedninjas

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.

kevin-donovan-zocdoc avatar Mar 10 '23 19:03 kevin-donovan-zocdoc