如何确定 JEST 是否正在运行代码? [英] How to determine if JEST is running the code or not?

查看:26
本文介绍了如何确定 JEST 是否正在运行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的 react-native 项目创建一个 JS 测试.我专门将 firebase 用于 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 会将其设置为值test".这可能是另一种检查方式.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆