使用外部插件在react-native中使用Jest测试用例错误

时间:2017-07-25 06:53:33

标签: javascript android sqlite react-native jest

我正在使用react-native init来创建一个react-native项目。我正在使用https://github.com/andpor/react-native-sqlite-storage库来进行SQLite绑定。

我有一个DbConnector.jest-test.js单元测试文件。内容

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 />
);
});

当我正在运行jest时,即使默认情况下node_modules被忽略,我也会收到跟踪错误。我在package.json中使用react-native预设

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

DbConnector.js中的导入如下:

import React, { Component } from 'react'
import { AppRegistry, StyleSheet, Text, View, TextInput, Button, Alert, 
AsyncStorage } from 'react-native'
import SQLite from 'react-native-sqlite-storage'

2 个答案:

答案 0 :(得分:3)

您的问题window is not defined似乎来自此行:https://github.com/andpor/react-native-sqlite-storage/blob/master/lib/sqlite.core.js#L53

您的测试似乎缺少react-native的导入,应该如下所示:

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

如果这不起作用,您可以按照以下步骤使用JSDOM from this guide(来自Enzyme,但可能仍有帮助!)

答案 1 :(得分:2)

您应该在测试之前尝试模拟react-native-sqlite-storage。

from .ocr import *