首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于基本jest测试,接收`globalObj.setTimeout不是函数错误

对于基本jest测试,接收`globalObj.setTimeout不是函数错误
EN

Stack Overflow用户
提问于 2021-11-18 05:13:55
回答 1查看 287关注 0票数 2

我正在尝试测试一个基本的Axios钩子,并接收到:

代码语言:javascript
复制
TypeError: globalObj.setTimeout is not a function

      at setImmediatePolyfill (node_modules/@testing-library/react-native/build/helpers/timers.js:59:20)
      at Object.then (node_modules/@testing-library/react-native/build/flushMicroTasks.js:26:32)

据我所知,预期的正在通过,但由于某些原因,我仍然收到这个错误,这是测试失败。这是react原生的,具有下面的package.json。钩子在我的应用程序上工作,我假设它与测试库有关,但我不确定是什么。

代码语言:javascript
复制
{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "test": "jest"
  },
  "dependencies": {
    "@react-navigation/native": "^6.0.6",
    "@react-navigation/native-stack": "^6.2.5",
    "@testing-library/jest-native": "^4.0.4",
    "@testing-library/react-native": "^8.0.0",
    "axios": "^0.24.0",
    "eslint": "^8.2.0",
    "expo": "^43.0.2",
    "expo-status-bar": "^1.1.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-native": "^0.64.2",
    "react-native-collapsible": "^1.6.0",
    "react-native-safe-area-context": "^3.3.2",
    "react-native-screens": "^3.9.0",
    "react-native-web": "^0.17.5"
  },
  "devDependencies": {
    "@babel/core": "^7.16.0",
    "jest-expo": "^43.0.1",
    "miragejs": "^0.1.42",
    "react-hooks-testing-library": "^0.6.0",
    "react-test-renderer": "^17.0.2",
    "react-timer-mixin": "^0.13.4",
    "xmlhttprequest": "^1.8.0",
    "yarn-upgrade-all": "^0.5.4"
  },
  "private": true,
  "jest": {
    "preset": "react-native"
  }
}

我的Axios挂钩:

代码语言:javascript
复制
// ./src/hooks.js

import { useState, useEffect } from "react";

export const useAPI = (apiFunction, params) => {
    const [data, setData] = useState(null);
    const [isLoading, setIsLoading] = useState(true);
    const [error, setError] = useState(null);

    useEffect(() => {
        apiFunction(params)
            .then(({data}) => {
                setData(data);
                setIsLoading(false);
            })
            .catch((err) => {
                setError("Something went wrong!");
                setIsLoading(false);
            });
    }, [apiFunction, params]);

    return [isLoading, data, error];
};

我的测试:

代码语言:javascript
复制
import { renderHook } from "@testing-library/react-hooks";
import { act } from "@testing-library/react-native";
import axios from "axios";
import { useAPI } from "../../Hooks/useAPI";

jest.mock("axios");
const mockData = {data: [{"test":"test"}]};


const getTestData = () => axios.get("/testEndpoint");

describe("fetch api data", () => {
    describe("on success", () => {
        it("should set data to test data and set isLoading false", async () => {
            axios.get.mockResolvedValueOnce(mockData);
            const {result, waitForNextUpdate } = renderHook(() => useAPI(getTestData));

            await act( async () => {
                await waitForNextUpdate();
            })

            const {data: expectedData} = mockData;

            expect(result.current[0]).toEqual(false);
            expect(result.current[1]).toEqual(expectedData);
            expect(result.current[2]).toEqual(null);
        })
    })
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-19 01:50:38

解决了我自己的问题。我之前在jest.setup.js文件中设置了global.window = {}。@testing-library/react-native使用timers.js文件,在该文件中,它们引用globalObj,该文件从以下位置获得:const globalObj = typeof window === 'undefined' ? global : window;。由于window在技术上不是未定义的,因此globalObj被设置为{},并且无法访问全局功能。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70014862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档