ReferenceError:尝试测试AsyncStorage时未定义__DEV__

时间:2019-05-07 21:27:13

标签: unit-testing react-native jestjs

我正在使用Jest测试我的React Native应用程序。我正在尝试模拟对AsyncStorage的调用,并且正在使用mock-async-storage包。按照他们的指示,我已经设置了他们的简单测试文件,如下所示:

import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import "react-native";
import MockAsyncStorage from "mock-async-storage";
import { AsyncStorage as storage } from "react-native";

import Station from "../src/models/Station";
import * as actions from "../src/redux/actions/stationActions";

const mockStore = configureMockStore([thunk]);

/* ...other tests  */

describe("async fetching actions", () => {
  describe("using MockAsyncStorage", () => {
    const mock = () => {
      const mockImpl = new MockAsyncStorage();
      jest.mock("AsyncStorage", () => mockImpl);
    };

    mock();

    it("Mock Async Storage working", async () => {
      await storage.setItem("myKey", "myValue");
      const value = await storage.getItem("myKey");
      expect(value).toBe("myValue");
    });
  });
});

按照软件包仓库中的示例,我的tests文件夹有一个__mocks__文件夹,其中包含以下AsyncStorage.js文件:

import MockAsyncStorage from '../../../lib/mockAsyncStorage';

export default new MockAsyncStorage();

以及这些文件在tests文件夹中:

// AsyncStorage.js
export default {}

// UseStorage.js
import AsyncStorage from './AsyncStorage';

export const save = (k, v) => AsyncStorage.setItem(k,v);

export const get = k => AsyncStorage.getItem(k); 

运行测试时,出现以下错误:


    ReferenceError: __DEV__ is not defined

      63 | 
      64 |     it("Mock Async Storage working", async () => {
    > 65 |       await storage.setItem("myKey", "myValue");
         |             ^
      66 |       const value = await storage.getItem("myKey");
      67 |       expect(value).toBe("myValue");
      68 |     });

      at Object.__DEV__ (node_modules/react-native/Libraries/Performance/Systrace.js:27:28)
      at Object.require (node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:14:18)
      at Object.require (node_modules/react-native/Libraries/BatchedBridge/BatchedBridge.js:13:22)
      at Object.require (node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:13:23)
      at Object.require (node_modules/react-native/Libraries/Storage/AsyncStorage.js:15:23)
      at Object.require [as AsyncStorage] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:180:12)
      at storage (tests/fetchStations.test.js:65:13)
      at tryCatch (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
      at Generator.invoke [as _invoke] (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:271:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
      at tryCatch (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
      at invoke (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:135:20)
      at node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:170:11
      at callInvokeWithMethodAndArg (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:169:16)
      at AsyncIterator.enqueue (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:192:13)
      at AsyncIterator.prototype.(anonymous function) [as next] (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
      at Object.<anonymous>.exports.async (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:216:14)
      at Object._callee (tests/fetchStations.test.js:64:38)

我尝试了其他答案中建议的许多方法,但没有任何帮助:

  • babel-preset-react-native添加到我的开发依赖项中
  • "globals": { "__DEV__": true }添加到“ {jest”下的我的package.json
  • 将我的jest presetjest-expo切换到react-native
  • 在测试文件的顶部添加/* global __DEV__ */

我如何进行这项工作?

小更新:这实际上可能与模拟没有任何关系。我已经删除了所有模拟AsyncStorage的尝试,并尝试仅测试使用AsyncStorage的方法。我所做的就是:

describe("fetchStations(useCache)", () => {
    beforeEach(async () => {
      await store.dispatch(actions.fetchStations({ useCache: true }));
    });

    xit("should return an object with the stations in a 'stations' key", () => {
      expect(store.getActions()).toEqual(
        expect.arrayContaining(expectedGetActions)
      );
    });
  });

actions.fetchStations包含对AsyncStorage.getItem的调用。而且我遇到相同的__DEV__ is not defined错误。

1 个答案:

答案 0 :(得分:0)

如果您configure jest to use the react-native preset,它将为您设置react native javascript environment,包括全局变量 Var2.1 Var2.2 Var2.3 0 4.0 3.0 NaN 1 4.0 NaN NaN 2 4.0 3.0 2.0