是否可以同时使用带有多个预设的 Jest? [英] Is it possible to use Jest with multiple presets at the same time?
问题描述
是否可以将 Jest 与多个预设一起使用,比如 jsdom 和 react-native?
Is it possible to use Jest with multiple presets, say jsdom and react-native?
我想测试一个可以在 Web 和 React Native 环境中工作的 React 组件.问题是该组件可能使用 React Native 库或某些文档的方法.
I'd like to test a React component that can work both on Web and in a React Native environment. The problem is that the component may use either React Native libraries or some document's methods.
当我运行一些测试时,jest 回复:
When I run some tests, jest replies:
无法从react-native-implementation.js"中找到模块NetInfo"
Cannot find module 'NetInfo' from 'react-native-implementation.js'
当我尝试添加时
"jest": {
"preset": "react-native"
}
到 package.json,我得到:
to package.json, I get:
ReferenceError: window is not defined
ReferenceError: window is not defined
推荐答案
预设只是普通的 Javascript 对象,因此在许多情况下您可以简单地合并它们.例如,这就是我同时启用 ts-jest
和 jest-puppeteer
的方式:
Presets are just plain Javascript objects, so in many circumstances you may simply merge them. For example, that's how I'm enabling ts-jest
and jest-puppeteer
simultaneously:
const merge = require('merge')
const ts_preset = require('ts-jest/jest-preset')
const puppeteer_preset = require('jest-puppeteer/jest-preset')
module.exports = merge.recursive(ts_preset, puppeteer_preset, {
globals: {
test_url: `http://${process.env.HOST || '127.0.0.1'}:${process.env.PORT || 3000}`,
},
})
如果某些选项不能像这样合并",只需手动处理这些情况.
If there are certain options that can't be 'merged' like that, just handle these cases manually.
这篇关于是否可以同时使用带有多个预设的 Jest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!