Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest test case error in react-native using external plugin

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

I have a DbConnector.jest-test.js unit tests file. Contents are

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 am running jest, I am getting following error even if by default node_modules are ignored. I am using react-native preset in package.json

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

Imports in DbConnector.js are as:

import React, { Component } from 'react'
import { AppRegistry, StyleSheet, Text, View, TextInput, Button, Alert, 
AsyncStorage } from 'react-native'
import SQLite from 'react-native-sqlite-storage'
like image 807
Vrishank Avatar asked Jul 25 '17 06:07

Vrishank


Video Answer


1 Answers

You should try to mock react-native-sqlite-storage before your test.

jest.mock('react-native-sqlite-storage');
like image 188
Gabriel Diez Avatar answered Sep 22 '22 21:09

Gabriel Diez