【问题标题】:Error removing directory which is not empty删除非空目录时出错
【发布时间】:2022-02-10 01:12:14
【问题描述】:

当所有测试都以 after 结束时,我试图删除一个目录,我收到以下错误:

fs.rmdir is not a function

这是我的代码:

after(() => {
        const fs = require('fs');
        const dir = '../../../../../../downloads';
        
        fs.rmdir(dir, { recursive: true }, (err) => {
            if (err) {
                throw err;
            }
        });
    })

【问题讨论】:

    标签: node.js cypress


    【解决方案1】:

    好像recursive option is deprecated from Node.js 14.14.0(你用的是什么版本?),所以你现在可以使用了:

    fs.rm(dir, { recursive: true, force: true }, (err) => {
      if (err) {
        throw err;
      }
    });
    

    【讨论】:

    • 嗨 @pavelsaman 我正在使用 14.18.1
    • 似乎是更高版本。 fs.rm() 有效还是无效?
    • 不,不工作:(
    【解决方案2】:

    原因是fs 是一个Node 库,但你试图在浏览器中使用它。

    对于节点代码,使用任务

    /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
        },
      })
    }
    

    但是有几个配置项似乎可以为您完成这项工作

    参考Configuration - Downloads

    downloadsFolder - 保存测试期间下载的文件的文件夹路径

    trashAssetsBeforeRuns - 在使用 cypress run 运行测试之前,Cypress 是否会丢弃 downloadsFolder、screenshotsFolder 和 videosFolder 中的资产。

    【讨论】:

    • 您好,感谢您的回复,但现在我收到此错误:cy.clearDownloads is not a function 与此:after(() => { cy.clearDownloads() })
    • 好的,你用cy.task('clearDownloads')调用一个任务——见this page
    猜你喜欢
    • 2013-08-05
    • 2012-05-31
    • 2016-10-16
    • 2018-09-19
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    相关资源
    最近更新 更多