给定以下代码:
const fetch = require('isomorphic-fetch')
module.exports = function suggestions (...args) {
// some error handling code
return fetch(MY_ENDPOINT)
}我发现在像这样使用fetch-mock时:
require('isomorphic-fetch')
const fetchMock = require('fetch-mock/es5/server')
fetchMock.mock(MY_ENDPOINT, [{}, {}])
describe('My spec', () => {
it('fakes a request to my endpoint', async () => {
const myData = await myCode(...args) // calls fetch with my endpoint
expect(myData).toEqual([{}, {}])
})
})我希望模拟程序会给出我设置的响应,即[{},{}],而不是调用真正的API并返回它的响应。
此外,如果我尝试使用fetch-mock而不是fetch-mock/es5/server,我会得到错误:
/Users/localuser/lendi/ldp-domain-integration/node_modules/fetch-mock/src/lib/fetch-handler.js:57
FetchMock.generateResponse = async function (response, url, opts) {
^^^^^^^^
SyntaxError: Unexpected token function我将jest与babel和babel-jest以及node v6.13.0一起使用。
发布于 2018-03-01 18:29:09
语法错误的原因是Node版本6不支持异步函数。
请尝试将Node更新为最新版本。
https://stackoverflow.com/questions/48998625
复制相似问题