解析TypeError:import_react_native.AppState.removeEventListener?is?not?a?function
Jest React Native Mock AppState TypeError
問(wèn)題記錄
背景:通過(guò) Jest 和 React Testing Library 對(duì) React Native 做自動(dòng)化測(cè)試
問(wèn)題: 代碼中出現(xiàn) AppState 的使用
import { AppState } from 'react-native'; AppState.removeEventListener('change', handleAppStateChange);
報(bào)錯(cuò):TypeError: import_react_native.AppState.removeEventListener is not a function
jest.config.js 配置使用了 React Native 預(yù)設(shè)
module.exports = { preset: 'react-native', ...
經(jīng)過(guò)排查發(fā)現(xiàn)是因?yàn)?React Native 提供的 jest mock 文件缺失部分方法
路徑:*/react-native/jest/setup.js
.mock('react-native/Libraries/AppState/AppState', () => ({ addEventListener: jest.fn(() => ({ remove: jest.fn(), })), }))
解決方案
項(xiàng)目臨時(shí)方案是將 packages/react-native/jest/setup.js 拷貝下來(lái),增加 react-native/Libraries/AppState/AppState 模塊的 removeEventListener 和 currentState方法的mock
.mock('react-native/Libraries/AppState/AppState', () => ({ addEventListener: jest.fn(() => ({ remove: jest.fn(), })), removeEventListener: jest.fn(), currentState: jest.fn(), }))
然后放在項(xiàng)目倉(cāng)庫(kù)中在 jest.setup.js 文件頭部引入
或者在 jest.config.js 的 setupFilesAfterEnv 入口引入
最后,這個(gè)缺陷我已經(jīng)給 React Native 官方倉(cāng)庫(kù)提了Pull Request,已經(jīng)合并主分支了
以上就是解析TypeError:import_react_native.AppState.removeEventListener is not a function的詳細(xì)內(nèi)容,更多關(guān)于AppState TypeError解決的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- JS報(bào)錯(cuò)Uncaught?TypeError:?XXX?is?not?a?function的解決方法
- React/vue開(kāi)發(fā)報(bào)錯(cuò)TypeError:this.getOptions?is?not?a?function的解決
- 解決TypeError: Object of type xxx is not JSON serializable錯(cuò)誤問(wèn)題
- vue?watch報(bào)錯(cuò):Error?in?callback?for?watcher?"xxx":"TypeError的解決方法
- 完美解決vue中報(bào)錯(cuò)?“TypeError:?Cannot?read?properties?of?null?(reading'forEach')“
相關(guān)文章
React?跨端動(dòng)態(tài)化核心技術(shù)實(shí)例分析
這篇文章主要為大家介紹了React?跨端動(dòng)態(tài)化核心技術(shù)實(shí)例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10React tabIndex使非表單元素支持focus和blur事件
這篇文章主要為大家介紹了React使用tabIndex使非表單元素支持focus和blur事件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11react-json-editor-ajrm解析錯(cuò)誤與解決方案
由于歷史原因,項(xiàng)目中 JSON 編輯器使用的是 react-json-editor-ajrm,近期遇到一個(gè)嚴(yán)重的展示錯(cuò)誤,傳入編輯器的數(shù)據(jù)與展示的不一致,這是產(chǎn)品和用戶不可接受的,本文給大家介紹了react-json-editor-ajrm解析錯(cuò)誤與解決方案,需要的朋友可以參考下2024-06-06react中value與defaultValue的區(qū)別及說(shuō)明
這篇文章主要介紹了react中value與defaultValue的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05解決React報(bào)錯(cuò)Property does not exist on 
這篇文章主要為大家介紹了React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12react router4+redux實(shí)現(xiàn)路由權(quán)限控制的方法
本篇文章主要介紹了react router4+redux實(shí)現(xiàn)路由權(quán)限控制的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05