Skip to content
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

Mocking fs.existsSync (or other fs functions) #8178

Closed
kzaiwo opened this issue Aug 5, 2020 · 2 comments
Closed

Mocking fs.existsSync (or other fs functions) #8178

kzaiwo opened this issue Aug 5, 2020 · 2 comments

Comments

@kzaiwo
Copy link

kzaiwo commented Aug 5, 2020

Since fs is not allowed on Cypress/web browser, I am creating mocks for the fs functions being called in my source (non-test/Electron) code.

For example, fs.existsSync. This method is called many times and will return different responses. So instead of manually mocking and hard-coding all their responses, I just created a task inside the mock so it can be checked dynamically.

window.Cypress.fs = require('fs')
window.Cypress.fs.existsSync = (file) => {
  var retVal = ''
  cy.task('existsSync', file).then((result) => { retVal = result })
  return retVal
}

it('Sample test', () => {
  cy.visit('http://localhost:8080')  
})

And in my plugins.js, the task is simply implemented as:

on('task', {
    existsSync (file) {
      return fs.existsSync(file)
    }
})

But then I am getting an error:

Uncaught Uncaught CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

  > `cy.visit()`

The cy command you invoked inside the promise was:

  > `cy.task()`

fs.existsSync() is being called during 'boot-up'.. So it is called during app creation (during cy.visit())

How can I use fs.existsSync??

Help!!!

@jennifer-shehane
Copy link
Member

cypress/plugins files run within the context of Node.js, so you can require fs directly there like this example: https://on.cypress.io/task#Read-a-file-that-might-not-exist

But, I'm a bit confused about your use case exactly. How this fs.existsSync is called from your application originally.

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Aug 5, 2020
@jennifer-shehane
Copy link
Member

Unfortunately we have to close this issue due to inactivity. Please comment if there is new information to provide concerning the original issue and we can reopen.

@jennifer-shehane jennifer-shehane removed the stage: needs information Not enough info to reproduce the issue label Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants