【发布时间】:2014-10-17 19:43:19
【问题描述】:
Jest 文档建议使用npm test 执行测试。
有没有办法在相关文件发生更改时查看源代码和测试以自动重新运行 Jest 测试?
【问题讨论】:
Jest 文档建议使用npm test 执行测试。
有没有办法在相关文件发生更改时查看源代码和测试以自动重新运行 Jest 测试?
【问题讨论】:
作为补充建议,您可以添加“--watchAll” 像这样进入你的 package.json 文件:
"scripts": {
"test": "jest --watchAll"
},
每次运行 npm test 都会默认开启 watch 模式。
欲了解更多信息npm CLI docs
【讨论】:
我个人使用npm包jest-watch-typeahead。
你需要做3个步骤:
npm install --save-dev jest jest-watch-typeahead
module.exports = { watchPlugins: [ 'jest-watch-typeahead/filename', 'jest-watch-typeahead/testname', ], };
yarn jest --watch
【讨论】:
如果您想在监视模式下运行单个文件:
yarn run test --watch FileName.test.jsx
【讨论】:
感谢 Erin Stanfill 的指出,Jest 已经支持 automatically re-running。 package.json 的更好配置是
{
"scripts": {
"test": "jest"
}
}
要打开手表模式,只需使用
$ npm run test -- --watch
或者
$ yarn run test --watch
【讨论】:
grunt watch 不会,它将继续当前的测试运行,忽略测试运行时发生的更改。
安装几个 Grunt 包:
npm install grunt-contrib-watch grunt-exec --save-dev
使用以下内容创建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');
}
然后简单地运行:
grunt watch
【讨论】:
在观察模式下开始测试。
jest --watch fileName.test.js
根据文档
运行与此规范名称匹配的测试(基本上与 describe 或 test 中的名称匹配)。
jest -t name-of-spec
// or in watch mode
jest --watch -t="TestName"
【讨论】:
如果你配置了npm test,你可以直接运行npm test -- --watch。
【讨论】:
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__" }