如何确定JEST是否正在运行代码? [英] How to determine if JEST is running the code or not?
问题描述
我正在我的react-native项目上创建一个JS测试。我特意使用firebase for react native,如果JS正在运行我的类的代码,我想用mockfirebase实例替换firebase实例。
I am creating a JS test on my react-native project. I'm specifically using firebase for react native, in which I would like to replace firebase instance with a mockfirebase instance if JS is running the code of my class.
例如我有类似下面的课程设置。
For example I have class setup like below.
import firebase from 'react-native-firebase';
class Database() {
/// use the firebase instance
}
我想检查jest是否是运行环境然后我用适当的模拟类替换导入行。
I'd like to have a check if jest is the running environment then I'd replace the import line with appropriate mock class.
推荐答案
jest设置一个名为JEST_WORKER_ID的环境变量,以便检查是否已设置:
jest sets an environment variable called JEST_WORKER_ID so you check if this is set:
function areWeTestingWithJest() {
return process.env.JEST_WORKER_ID !== undefined;
}
我还看到如果未设置NODE_ENV,则jest CLI将其设置为价值'测试'。这可能是另一种检查方式。
I also see that if NODE_ENV is not set the jest CLI sets it to the value 'test'. This might be another way to check.
这篇关于如何确定JEST是否正在运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!