【问题标题】:Watch and rerun Jest JS tests观看并重新运行 Jest JS 测试
【发布时间】:2014-10-17 19:43:19
【问题描述】:

Jest 文档建议使用npm test 执行测试。

有没有办法在相关文件发生更改时查看源代码和测试以自动重新运行 Jest 测试?

【问题讨论】:

    标签: npm jestjs


    【解决方案1】:

    作为补充建议,您可以添加“--watchAll” 像这样进入你的 package.json 文件:

    "scripts": {       
      "test": "jest --watchAll"
    },
    

    每次运行 npm test 都会默认开启 watch 模式。

    欲了解更多信息npm CLI docs

    【讨论】:

      【解决方案2】:

      我个人使用npm包jest-watch-typeahead

      你需要做3个步骤:

      1. 安装 npm 包:

      npm install --save-dev jest jest-watch-typeahead

      1. 添加到 jest.config.js 下一个代码:
      module.exports = {
        watchPlugins: [
          'jest-watch-typeahead/filename',
          'jest-watch-typeahead/testname',
        ],
      };
      
      1. 在手表模式下运行 Jest

      yarn jest --watch

      【讨论】:

        【解决方案3】:

        如果您想在监视模式下运行单个文件

        yarn run test --watch FileName.test.jsx
        

        【讨论】:

          【解决方案4】:

          感谢 Erin Stanfill 的指出,Jest 已经支持 automatically re-runningpackage.json 的更好配置是

          {
            "scripts": {
              "test": "jest"
            }
          }
          

          要打开手表模式,只需使用

          $ npm run test -- --watch
          

          或者

          $ yarn run test --watch
          

          【讨论】:

          • 需要指出的重要一点是,如果在测试运行时发生更改,nodemon 将中断测试并重新启动。 grunt watch 不会,它将继续当前的测试运行,忽略测试运行时发生的更改。
          • 删除第一个答案可能会更好?编辑对我来说非常有用,乍一看,将两者一起阅读会让人感到困惑。
          • 我有一个名为 string.utility.spec.js 的文件,当我执行“npm run test”时,它会被运行,但当我运行“npm run test - - -手表”。将文件重命名为“string.spec.js”将导致它被拾取并运行。很奇怪。
          • @VishalAnand npm 会将 -- 之后的所有参数直接传递给脚本。 link.
          • watch 也可以添加到 "scripts": { "test": "jest --watch" },然后就可以使用 npm run test 了
          【解决方案5】:
          1. 安装几个 Grunt 包:

            npm install grunt-contrib-watch grunt-exec --save-dev
            
          2. 使用以下内容创建Gruntfile.js

            module.exports = function(grunt) {
                grunt.initConfig({
                    exec: {
                        jest: 'node node_modules/jest-cli/bin/jest'
                    },
                    watch: {
                        files: ['**/*.js'],
                        tasks: ['exec:jest']
                    }
                });
                grunt.loadNpmTasks('grunt-contrib-watch');
                grunt.loadNpmTasks('grunt-exec');
            }
            
          3. 然后简单地运行:

            grunt watch
            

          【讨论】:

            【解决方案6】:

            在观察模式下开始测试。

            jest --watch fileName.test.js
            

            根据文档

            运行与此规范名称匹配的测试(基本上与 describetest 中的名称匹配)。

            jest -t name-of-spec
            // or in watch mode
            jest --watch -t="TestName"
            

            【讨论】:

              【解决方案7】:

              如果你配置了npm test,你可以直接运行npm test -- --watch

              【讨论】:

                【解决方案8】:

                This example 展示了如何使用 gulp 运行您的 Jest 测试,使用 jest-cli,以及一个 tdd gulp 任务来监视文件并在文件更改时重新运行 Jest 测试:

                var gulp = require('gulp');
                var jest = require('jest-cli');
                
                var jestConfig = {
                    rootDir: 'source'
                };
                
                gulp.task('test', function(done) {
                    jest.runCLI({ config : jestConfig }, ".", function() {
                        done();
                    });
                });
                
                gulp.task('tdd', function(done) {
                    gulp.watch([ jestConfig.rootDir + "/**/*.js" ], [ 'test' ]);
                });
                
                gulp.task('default', function() {
                    // place code for your default task here
                });
                

                【讨论】:

                • 出于某种原因,尽管我使用默认的__tests__ 文件夹,但它不适用于此jestConfig。在阅读了docs 并播放了一下这对我有用:var jestConfig = { rootDir: "__tests__" }
                猜你喜欢
                • 2017-12-18
                • 1970-01-01
                • 1970-01-01
                • 2019-03-02
                • 1970-01-01
                • 2020-11-16
                • 2021-03-06
                • 1970-01-01
                • 2018-04-28
                相关资源
                最近更新 更多