reactjs – Jest抛出TypeError:无法读取未定义的属性’fetch’
发布时间:2020-12-15 20:19:48 所属栏目:百科 来源:网络整理
导读:我正在尝试使用Jest在我的react / react-native库上运行一些测试(内部只有一些业务逻辑). 我们正在测试使用fetch函数的操作(使用whatwg-fetch进行polyfill). 我添加了whatwg-fetch(感谢Safari)的反应. 每当我尝试运行测试时,我都会收到此错误: TypeError: C
我正在尝试使用Jest在我的react / react-native库上运行一些测试(内部只有一些业务逻辑).
我们正在测试使用fetch函数的操作(使用whatwg-fetch进行polyfill). 每当我尝试运行测试时,我都会收到此错误: TypeError: Cannot read property 'fetch' of undefined at node_modules/whatwg-fetch/fetch.js:4:11 at Object.<anonymous> (node_modules/whatwg-fetch/fetch.js:461:3) at Object.<anonymous> (node_modules/jest-expo/src/setup.js:138:416) 什么可能导致这个问题?在jest配置中有没有办法避免这种情况? 以下是一些用于调试的文件: 在package.json中进行Jest配置 "jest": { "preset": "jest-expo","moduleFileExtensions": [ "js","jsx","ts","tsx" ],"verbose": true,"transform": { "^.+.(js|ts|tsx)$": "<rootDir>/node_modules/babel-jest" },"testRegex": "(/__tests__/.*|.(test|spec)).(ts|tsx|js)$","testPathIgnorePatterns": [ ".snap$","<rootDir>/node_modules/","<rootDir>/dist/" ],"transformIgnorePatterns": [ "node_modules/?!react-native" ] }, Webpack配置: const config = { entry: [ 'whatwg-fetch',__dirname + '/src/index.ts',],devtool: 'source-map',output: { path: path.join(__dirname,'/dist'),filename: 'index.js',library: 'checkinatwork-module',libraryTarget: 'umd',umdNamedDefine: true,},module: { loaders: [ { test: /.(tsx|ts)?$/,loader: 'ts-loader',exclude: /node_modules/ },resolve: { modules: [ './src','node_modules',extensions: ['.js','.ts','.jsx','.tsx','json'],plugins: [ ],}; 测试文件: import expect from 'expect'; import * as actions from '../../src/components/Checkin/checkin.action'; import * as reducers from '../../src/components/Checkin/checkin.reducer'; import configureMockStore from 'redux-mock-store'; import thunk from 'redux-thunk'; import nock from 'nock'; const middlewares = [ thunk ]; const mockStore = configureMockStore(middlewares); describe('=> ADD CHECKIN ACTIONS',() => { describe('- REQUEST',() => { it('Action: ADD_CHECKIN_REQUEST should request addCawCheckin',() => { const expectedAction = { type: actions.ADD_CHECKIN_REQUEST,isFetching: true,}; expect(actions.addCheckinRequest()) .toEqual(expectedAction); }); it('Reducer: newCheckin should trigger ADD_CHECKIN_REQUEST and initiate loading',() => { const expectedState = { isFetching: true,status: null,}; expect(reducers.newCheckin(reducers.newCheckinDefaultState,actions.addCheckinRequest())) .toEqual(expectedState); }); }); 行动档案: export const getCheckins = (sessionId,date,url,isRefresh) => { const config = { method: 'POST',headers: { 'Content-Type': 'application/json' },body: JSON.stringify({ sessionId: {sessionId},date: {date},}),}; return dispatch => { if (!isRefresh) { dispatch(getCheckinsRequest()); } return fetch(url + 'getCAWCheckIns',config) .then(response => response.json()) .then(({ checkins }) => { dispatch(getCheckinsSuccess(checkins)); }).catch(err => { dispatch(getCheckinsError('Get checkins failed')); console.error('Get checkins failed: ',err); }); }; }; 谢谢! 解决方法
我在规范中做到了:
import { fetch } from 'whatwg-fetch'; global.fetch = fetch; 并且它与Jest一样有效. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Oracle连接关闭分析 Diagnosing the ORA-12537: TNS: conne
- C++继承和派生是什么意思(通俗易懂)
- sqlite – 如何使用x’abc’二进制字符串文字语法显示blob值
- Cocos2d-x 自定义血条及其美化----之游戏开发《赵云要格斗》
- c# – Azure DocumentDB Decimal Truncation
- Swift学习笔记(十)附属脚本
- ruby-on-rails – 安装therubyracer时发生错误(0.10.1)
- 强势推荐:React Native Jsx 自动补全插件
- Oracle Database 12.1中rman show all的新变化
- oracle函数 LENGTHC(c1).LENGTH2(c1).LENGTH4(c1)