Ant*_*nto 2 javascript universal reactjs jestjs redux-saga
在我的 React Native 应用程序的 Saga 测试中(可以正常工作),我添加了以下测试,该测试调用执行 POST http 调用 (doScan) 的函数。
describe('Scan the product and register the scanning action', () => {
const it = sagaHelper(scanProductSaga(scanProductAction(item)));
it('logscan via ASL', (result) => {
expect(result).toEqual(cps(ASLogger.logScan, xxx));
return logScanResult;
});
it('should register the product', (result) => {
expect(result).toEqual(call(doScan, logScanResult));
});
});
Run Code Online (Sandbox Code Playgroud)
单独的文件:
const doScan = scanObj =>
toJSON(fetch('https://xxxx.xxxxxx.com/logger/scans', {
method: 'POST',
headers: new Headers(CONTENT_TYPE_HEADERS),
body: JSON.stringify(scanObj),
}));
Run Code Online (Sandbox Code Playgroud)
注意:fetch 函数来自 react-native 库中的“react-native-interfaces.js”。测试失败,错误是由以下异常引起的:
ReferenceError: fetch is not defined
at doScan (/Users/andy/WebstormProjects/ASAP/api/index.js:81:11)....
Run Code Online (Sandbox Code Playgroud)
什么会导致这样的问题?可能的解决方案是什么?
react-native 有fetch默认值,但 node.js 上的测试环境没有fetch.
您可以fetch在测试代码的顶部像下面这样导入。
const fetch = require('node-fetch')
Run Code Online (Sandbox Code Playgroud)
文件 react-native-interface.js 只声明fetch.
declare var fetch: any;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12706 次 |
| 最近记录: |