我正在尝试在以下环境中设置测试环境:摩卡咖啡+西农+酶+打字稿。我已经可以通过摩卡咖啡+ sinon +酶进行测试,但是在Typescript支持下我失败了。
以下是我的摘要文件中的配置文件:
/* Package.json */
"test": "mocha --watch --require babel-register.js --require ignore-styles --require setup.js src/test/*.test.js",
/* setup.js */
const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;
function copyProps(src, target) {
const props = Object.getOwnPropertyNames(src)
.filter(prop => typeof target[prop] === 'undefined')
.reduce((result, prop) => ({
...result,
[prop]: Object.getOwnPropertyDescriptor(src, prop),
}), {});
Object.defineProperties(target, props);
}
global.window = window;
global.document = window.document;
global.window.requestAnimationFrame = (cb) => {
return setTimeout(cb, 0);
};
global.window.cancelAnimationFrame = (cb) => {
return setTimeout(cb, 0);
}
global.navigator = {
userAgent: 'node.js',
};
copyProps(window, global);
/* babel.config */
module.exports = function (api) {
//we will keep here all the supplementary babel options
api.cache(false);
return {
"env": {
"test" : {
"compact": false
}
}
}
};
/* babel register */
require("@babel/polyfill");
require("@babel/register")({
// Array of ignore conditions, either a regex or a function. (Optional)
ignore: [
// When a file path matches this regex then it is **not** compiled
/node_modules\/(?!(ol)\/)/
// The file's path is also passed to any ignore functions. It will
// **not** be compiled if `true` is returned.
//function(filepath) {
//return filepath !== "/path/to/es6-file.js";
//},
],
presets : ["react-app"],
plugins : [
"@babel/plugin-transform-modules-commonjs",
"inline-react-svg",
],
//root: __dirname,
// Optional only regex - if any filenames **don't** match this regex then they
// aren't compiled
//only: /my_es6_folder/,
// Setting this will remove the currently hooked extensions of `.es6`, `.es`, `.jsx`, `.mjs`
// and .js so you'll have to add them back if you want them to be used again.
extensions: [".es6", ".es", ".jsx", ".js", ".mjs"],
// Setting this to false will disable the cache.
cache: false,
});
我试图添加@ babel-typescript-preset,但是之后我的测试过程崩溃了。任何建议都是高度推荐的。