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.
JavaScript
x
5
1
import firebase from 'react-native-firebase';
2
class Database() {
3
/// use the firebase instance
4
}
5
I’d like to have a check if jest is the running environment then I’d replace the import line with appropriate mock class.
Advertisement
Answer
jest sets an environment variable called JEST_WORKER_ID so you check if this is set:
JavaScript
1
4
1
function areWeTestingWithJest() {
2
return process.env.JEST_WORKER_ID !== undefined;
3
}
4
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.