Sea*_*ysa 6 fetch typescript jestjs jest-fetch-mock
jest-fetch-mock 有一个我遵循的 Typescript 设置指南:https ://www.npmjs.com/package/jest-fetch-mock
// package.json
, "devDependencies": {
"@types/jest": "^24.0.23",
"jest": "^24.9.0",
"jest-fetch-mock": "^2.1.2",
"ts-jest": "^24.2.0",
"typescript": "^3.7.2"
},
"jest": {
"automock": false,
"setupFiles": ["./setupJest.ts"]
}
// setupJest.ts
import {GlobalWithFetchMock} from "jest-fetch-mock";
const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock;
customGlobal.fetch = require('jest-fetch-mock');
customGlobal.fetchMock = customGlobal.fetch;
Run Code Online (Sandbox Code Playgroud)
但是当我尝试设置一个虚拟的解析值时:
// ScriptTag.test.ts
test("", async () => {
fetch.mockResponseOnce(JSON.stringify(1));
const data = await fetchMock("x");
await expect(data.json()).resolves.toStrictEqual(2);
})
// terminal
FAIL functions/src/classes/__tests__/ScriptTag.test.ts
? Test suite failed to run
TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
functions/src/classes/__tests__/ScriptTag.test.ts:173:11 - error TS2339: Property 'mockResponseOnce' does not exist on type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
173 fetch.mockResponseOnce(JSON.stringify(1));
~~~~~~~~~~~~~~~~
functions/src/classes/__tests__/ScriptTag.test.ts:174:24 - error TS2349: This expression is not callable.
Type 'typeof import("/home/owner/PhpstormProjects/shopify/buyUsedServer/node_modules/jest-fetch-mock/types/index")' has no call signatures.
174 const data = await fetchMock("x");
~~~~~~~~~
Test Suites: 1 failed, 1 total
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?
| 归档时间: |
|
| 查看次数: |
697 次 |
| 最近记录: |