【问题标题】:ReferenceError: __DEV__ is not defined when trying to test AsyncStorageReferenceError:尝试测试 AsyncStorage 时未定义 __DEV__
【发布时间】:2019-09-25 14:14:56
【问题描述】:

我正在使用 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");
    });
  });
});

按照包仓库中的示例,我的测试文件夹有一个 __mocks__ 文件夹,其中包含此 AsyncStorage.js 文件:

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

export default new MockAsyncStorage();

以及测试文件夹中的这些文件:

// 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 添加到我的开发依赖项中
  • 在“jest”下将"globals": { "__DEV__": true } 添加到我的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 错误。

【问题讨论】:

    标签: unit-testing react-native jestjs


    【解决方案1】:

    如果您configure jest to use the react-native preset,它将为您设置react native javascript environment,包括全局__DEV__ 变量。

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2017-11-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 2017-10-09
      • 2021-04-11
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多