赛普拉斯浏览器无法实时自动重新加载

时间:2019-10-24 12:00:40

标签: real-time cypress

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

操作系统:Windows 10 Chrome:版本78.0.3904.70

1 个答案:

答案 0 :(得分: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