Jest test test error in reaction-native using an external plugin

I use init-native init to create a reaction project. I am using the https://github.com/andpor/react-native-sqlite-storage library for SQLite bindings.

I have a test file DbConnector.jest-test.js. Content

import DbConnector from '../app/components/DbConnector.js'; // Note: test renderer must be required after react-native. import renderer from 'react-test-renderer'; it('renders correctly', () => { const tree = renderer.create( <DbConnector /> ); }); 

When I run jest, I get the following error, even if by default node_modules are ignored.

 Test suite failed to run ReferenceError: window is not defined at Object.<anonymous> (node_modules/react-native-sqlite-storage/lib/sqlite.core.js:53:10) at Object.<anonymous> (node_modules/react-native-sqlite-storage/sqlite.js:10:12) at Object.<anonymous> (app/components/DbConnector.js:3:31) Test Suites: 1 failed, 1 total 

Import to DbConnector.js looks like this:

 import React, { Component } from 'react' import { AppRegistry, StyleSheet, Text, View, TextInput, Button, Alert, AsyncStorage } from 'react-native' import SQLite from 'react-native-sqlite-storage' 
+5
source share
2 answers

It looks like your window is not defined problem comes from this line: https://github.com/andpor/react-native-sqlite-storage/blob/master/lib/sqlite.core.js#L53

It looks like your test react-native not have react-native imports and should look like this:

 import 'react-native'; import renderer from 'react-test-renderer'; 

If this does not work, you can follow these steps to use JSDOM from this guide (which is from Enzyme, but may help!)

+3
source

Before testing, you should try to make fun of the reaction-native-sqlite-store.

 jest.mock('react-native-sqlite-storage'); 
+1
source

Source: https://habr.com/ru/post/1270184/


All Articles