当我编写以下代码时,Jest.js 通过了测试:
test("internalFetch()", async () => {
fetchMock.mock("*", Promise.resolve(JSON.stringify({key1 : "val1"})));
const response = await fetch('http://starMatcher.com', {
method : "post"
, headers : { "content-type" : "application/json" }
});
// the next line fails the test when uncommented
// console.log(await response.json(), `=====response.json()=====`);
await expect(response.json()).resolves.toStrictEqual({key1 : "val1"});
});
取消注释日志语句会立即中断测试并将其显示在终端中。
✕ internalFetch() (56ms)
expect(received).resolves.toStrictEqual()
Received promise rejected instead of resolved
Rejected to value: [TypeError: body used already for: http://starmatcher.com/]
191 | console.log(await response.json(), `=====response.json()=====`);
192 |
> 193 | await expect(response.json()).resolves.toStrictEqual({key1 : "val1"});
| ^
194 | });
195 |
196 |
at expect (node_modules/expect/build/index.js:138:15)
at functions/src/classes/__tests__/ScriptTag.test.ts:193:11
at step (functions/src/classes/__tests__/ScriptTag.test.ts:46:23)
at Object.next (functions/src/classes/__tests__/ScriptTag.test.ts:27:53)
at fulfilled (functions/src/classes/__tests__/ScriptTag.test.ts:18:58)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 skipped, 9 passed, 11 total
任何人都知道为什么日志语句会干扰并导致承诺在(现在)第二次查找时被拒绝response
?
也试过
- 转而
fetchMock.post()
使用.mock()
。
同样的终端错误。
- 删除第二次
.json()
执行并将匹配器切换为非严格
test("internalFetch()", async () => {
fetchMock.mock("*", Promise.resolve(JSON.stringify({key1 : "val1"})));
const response = await fetch('http://starMatcher.com', {
method : "post"
, headers : {
"content-type" : "application/json"
}
});
console.log(await response.json(), `=====response.json()=====`);
await expect(response).toEqual({key1 : "val1"});
});
// terminal
internalFetch()
expect(received).toEqual(expected) // deep equality
- Expected
+ Received
- Object {
- "key1": "val1",
+ Response {
+ "size": 0,
+ "timeout": 0,
+ Symbol(Body internals): Object {
+ "body": Object {
+ "data": Array [
+ 123,
+ 34,
+ 107,
+ 101,
+ 121,
+ 49,
+ 34,
+ 58,
+ 34,
+ 118,
+ 97,
+ 108,
+ 49,
+ 34,
+ 125,
+ ],
+ "type": "Buffer",
+ },
+ "disturbed": true,
+ "error": null,
+ },
+ Symbol(Response internals): Object {
+ "counter": undefined,
+ "headers": Headers {
+ Symbol(map): Object {
+ "Content-Length": Array [
+ "15",
+ ],
+ "Content-Type": Array [
+ "text/plain;charset=UTF-8",
+ ],
+ },
+ },
+ "status": 200,
+ "statusText": "OK",
+ "url": "http://starmatcher.com/",
+ },
}
191 | console.log(await response.json(), `=====response.json()=====`);
192 |
> 193 | await expect(response).toEqual({key1 : "val1"});
| ^
194 | });
195 |
196 |
at functions/src/classes/__tests__/ScriptTag.test.ts:193:28
at step (functions/src/classes/__tests__/ScriptTag.test.ts:46:23)
at Object.next (functions/src/classes/__tests__/ScriptTag.test.ts:27:53)
at fulfilled (functions/src/classes/__tests__/ScriptTag.test.ts:18:58)
console.log functions/src/classes/__tests__/ScriptTag.test.ts:191
{ key1: 'val1' } =====response.json()=====
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 skipped, 9 passed, 11 total
- 上一个示例,但
.json()
在最后一行添加:
await expect(response.json()).toEqual({key1 : "val1"});
// terminal
expect(received).toEqual(expected) // deep equality
- Expected
+ Received
- Object {
- "key1": "val1",
- }
+ Promise {}