Cypress浏览器实时自动重新加载不起作用

问题描述 投票:1回答:1

正如标题所述,每次更改代码时,我都必须使用赛普拉斯测试运行器来查看结果。但是按照说明,它应该是自动重新加载并实时显示结果。

OS:Windows 10 Chrome:版本78.0.3904.70

real-time cypress
1个回答
0
投票

这里有两个解决方案,取决于您是否有构建步骤。

  1. 如果您没有构建步骤,那么这很容易:

    在您的cypress/plugins/index.js中:

    1. 获取当前正在运行的规范文件的文件句柄,以便可以在它们上发出rerun事件。
    2. 设置chokidar(或类似的)观察器,侦听文件更改,然后重新运行规范。

      // (1) setup to obtain file handles 
      // ------------------------------------------------------
      
      let openFiles = [];
      
      module.exports = ( on ) => {
          on('file:preprocessor', file => {
              if (
                  /\.spec\.js/.test(file.filePath) &&
                  !openFiles.find(f => f.filePath === file.filePath)
              ) {
      
                  openFiles.push(file);
      
                  file.on('close', () => {
                      openFiles = openFiles.filter(f => f.filePath !== file.filePath);
                  });
              }
      
              // tells cypress to not compile the file. If you normally
              //  compile your spec or support files, then instead of this line,
              //  return whatever you do for compilation
              return file.filePath;
          });
      
          on('before:browser:launch', () => {
              openFiles = [];
          });
      };
      
      // (2) watching and re-running logic
      // ------------------------------------------------------
      
      chokidar.watch([ /* paths/to/watch */ ])
          .on( "change", () => rerunTests());
      
      function rerunTests () {        
          // https://github.com/cypress-io/cypress/issues/3614
          const file = openFiles[0];
          if ( file ) file.emit('rerun');
      }
      
  2. 如果您有构建步骤,则将涉及更多的工作流程。我将不介绍实现细节,而只是概述:

    1. 启动构建步骤监视程序时设置IPC channel,并在保存和编译文件时发出did-compile事件或类似事件。
    2. cypress/plugins/index.js中的逻辑大部分与以前的解决方案相同,但是您将订阅IPC服务器的did-compile事件,而不是chokidar监视程序,并在发出事件时重新运行规范。

有关更多信息,请参见Preprocessors APIcypress-io/cypress-watch-preprocessor

© www.soinside.com 2019 - 2024. All rights reserved.