0

I'm trying to delete a directory when all the tests finish with an after, and I get the following error:

fs.rmdir is not a function

Here is mi code:

after(() => {
        const fs = require('fs');
        const dir = '../../../../../../downloads';
        
        fs.rmdir(dir, { recursive: true }, (err) => {
            if (err) {
                throw err;
            }
        });
    })
TesterDick
  • 3,830
  • 5
  • 18
Mario Ramos García
  • 755
  • 3
  • 8
  • 20

2 Answers2

1

Seems like the recursive option is deprecated from Node.js 14.14.0 (what version do you use?), so you can now use:

fs.rm(dir, { recursive: true, force: true }, (err) => {
  if (err) {
    throw err;
  }
});
pavelsaman
  • 7,399
  • 1
  • 14
  • 32
0

The reason is fs is a Node library, but you are trying to use it in the browser.

For Node code, use a task

/cypress/plugins/index.js

const fs = require('f')

module.exports = (on, config) => {
  on('task', {
    clearDownloads(message) {
      const fs = require('fs');
      const dir = '../../../../../../downloads';
        
      fs.rm(dir, { recursive: true }, (err) => {
        if (err) {
          throw err;
        }
      });
      return null
    },
  })
}

But there's a couple of configuration items that seem to do this job for you

Ref Configuration - Downloads

downloadsFolder - Path to folder where files downloaded during a test are saved

trashAssetsBeforeRuns - Whether Cypress will trash assets within the downloadsFolder, screenshotsFolder, and videosFolder before tests run with cypress run.

Fody
  • 23,754
  • 3
  • 20
  • 37
  • Hi, thank for your response but now i got this error: `cy.clearDownloads is not a function` with this: `after(() => { cy.clearDownloads() })` – Mario Ramos García Feb 10 '22 at 10:29
  • Ok, you call a task with `cy.task('clearDownloads')` - see [this page](https://docs.cypress.io/api/commands/task) – Fody Feb 11 '22 at 04:53