我想用jest.runCLI()在gulp执行只是改变了测试.
我怎样才能做到这一点?
我怎样才能运行一个测试jest.runCLI()?
我想测试一个非常简单的JS函数
export function displaySpinner() {
const loadingOverlayDOM = document.createElement('DIV');
const spinner = document.createElement('IMG');
loadingOverlayDOM.id = 'overlay-spinner';
loadingOverlayDOM.className = 'content-overlay';
spinner.className = 'is-spinning';
spinner.setAttribute('src', '/assets/img/svg/icons/spinner.svg');
l loadingOverlayDOM.insertAdjacentElement('beforeend', spinner);
document.body.insertAdjacentElement('beforeend', loadingOverlayDOM);
}
Run Code Online (Sandbox Code Playgroud)
与此(出于简化此问题的目的)Jest测试代码:
test('displaySpinner displays the spinner overlay in the current page', () => {
utils.displaySpinner();
});
Run Code Online (Sandbox Code Playgroud)
但是测试对我大叫:
FAIL app/helper/utils.test.js
? utils › displaySpinner displays the spinner overlay in the current page
TypeError: loadingOverlayDOM.insertAdjacentElement is not a function
at Object.displaySpinner (app/helper/utils.js:185:439)
at Object.<anonymous> (app/helper/utils.test.js:87:15)
at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
at process._tickCallback (internal/process/next_tick.js:109:7)
Run Code Online (Sandbox Code Playgroud)
这是Jest的错误,还是我在这里错过了一些东西?
运行时出现以下错误gulp test:
Error: Please run node with the --harmony flag!
Run Code Online (Sandbox Code Playgroud)
这是我的gulp任务:
var jest = require('gulp-jest');
gulp.task('test', function() {
return gulp.src('src/**/__tests__').pipe(jest({
rootDir: 'src',
scriptPreprocessor: '../node_modules/6to5-jest',
unmockedModulePathPatterns: [ 'react' ]
}));
});
Run Code Online (Sandbox Code Playgroud)
重现:https://github.com/SEEK-Jobs/react-playground
请注意,它npm test正常工作(测试按预期gulp test失败),但失败并出现上述错误.双方npm test并gulp test具有相同的配置对象:
{
rootDir: 'src',
scriptPreprocessor: '../node_modules/6to5-jest',
unmockedModulePathPatterns: [ 'react' ]
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在尝试使用 gulp-jest 用 gulp 运行 jest 测试。从 npm 安装这些包:
"gulp-jest": "^4.0.3",
"jest-cli": "^25.3.0"
Run Code Online (Sandbox Code Playgroud)
并为 jest 提供了以下配置:
"jest": {
"setupFilesAfterEnv": [
"<rootDir>/jest.setup.js"
],
"collectCoverage": true,
"coverageDirectory": "./test/unit-test-coverage",
"coverageReporters": [
"text",
"lcov"
]
}
Run Code Online (Sandbox Code Playgroud)
我在我的 gulpfile.js 中编写了一个基本方法,引用了这个链接:
function runJsTests() {
process.env.NODE_ENV = 'test';
const __dirname = "Features/Components";
return gulp.src(__dirname).pipe(jest({
}));
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行 gulp 任务时出现以下错误:
类型错误:无法读取未定义的属性“runCLI”