meinaart / cypress-plugin-snapshots

Plugin for snapshot tests in Cypress.io
MIT License
489 stars 116 forks source link

fix(crypto): replace with rand-token dependency #146

Closed thompsonsj closed 3 years ago

thompsonsj commented 3 years ago

Fixes #143

Remove the dependency on crypto and replace functionality with sehrope/node-rand-token.

Background

I created a test in Jest to confirm that createToken works as expected.

However, when running Cypress, the crypto object has an undefined value on the randomBytes property. This is what causes the error described in #143 .

Because the test passes in Jest, I thought this may be due to crypto running in node but not in the browser. As a result, I attempted to use the randomBytes method from https://github.com/crypto-browserify. However, this returned an empty object for the randomBytes property in Cypress.

Finally, I decided to remove the dependency on crypto entirely and look for the best alternative that can perform the same functionality of generating a token.

A note on Cypress 5 compatibility

This is only one step in achieving compatibility with Cypress 5. With this PR, Cypress gets past createToken but runs into another error as follows.

This looks similar in the sense that the error comes from a node builtin.

The following error originated from your test code, not from Cypress.

  > util.inherits is not a function

node_modules/assert/assert.js:164:1
  162 | 
  163 | // assert.AssertionError instanceof Error
> 164 | util.inherits(assert.AssertionError, Error);
      | ^
  165 | 
  166 | function truncate(s, n) {
  167 |   if (typeof s === 'string') {

Node builtin support

Looking at https://github.com/cypress-io/cypress/pull/8273, it seems Cypress adds support for specific node builtins. Perhaps this is related, and investigating node builtin support could lead to a better solution.

JessicaSachs commented 3 years ago

@meinaart can you do a release? 1.4.4 is broken with Cypress >= 5 still.