记录/检查/重放 fetch 财务报告和审定财务报表 WebSocket 会议
用于离线测试
这些执行工作并非专门针对 @exodus/test 并可以单独使用
零依赖性,可捆绑
复制人可以在 V8 / JavaScriptCore / Hermes 中运行, 即使没有执行网络 API
import { fetchRecorder, fetchReplayer, WebSocketRecorder, WebSocketReplayer, prettyJSON } from '@exodus/replay'
生成日志 是人可读
fetchRecorder(log[, { fetch }])
回返a fetch 执行,将所有请求附于提供 log 数组
log 以附加新请求来变异
如果一个基地 fetch 执行没有通过,全球 fetch 用作基础
这可以被推翻,例如. fetchRecorder(log, { fetch: require('node-fetch') })
fetchReplayer(log)
回返a fetch 对所提供请求的答复 log 数组
请求通过“ 第一次匹配” 命令得到服务, 从内部日志克隆中丢弃
log 是要是数组,而不是变异
WebSocketRecorder(log[, { WebSocket }])
回返a WebSocket 执行,将所有会话都附加到所提供的 log 数组
log 以附加新会话来变异
如果一个基地 WebSocket 执行没有通过,全球 WebSocket 用作基础
这可以被推翻,例如. WebSocketRecorder(log, { WebSocket: require('ws') })
WebSocketReplayer(log[, { interval = 0 }])
回返a WebSocket 提供会议的答复 log 数组
会话由“ 第一次匹配” 顺序服务, 从内部日志克隆中丢弃
log 是要是数组,而不是变异
可以选择, interval 可用于控制回放速度:
-
interval = 0(默认) -
interval = Infinity匹配原始记录的事件时间 -
interval = number事件之间的延迟Math.min(number, recorededDelay)
prettyJSON(data, { width = 120 })
用它来打印日志 : prettyJSON(log)
喜欢 JSON.stringify(),但有相当的印刷 的可读性和方便,如果检查
输出可解析为 JSON.parse()
简而言之,装入 width 未保证,但输出在运行之间稳定
样本
fetch 录音
{
"request": {
"resource": "https://jsonplaceholder.typicode.com/users/2",
"options": { "headers": [["accept", "application/json"]] }
},
"status": 200,
"statusText": "OK",
"ok": true,
"headers": [
...
["connection", "keep-alive"],
["content-encoding", "br"],
["content-type", "application/json; charset=utf-8"],
["date", "Sat, 03 Aug 2024 16:57:51 GMT"],
...
],
"url": "https://jsonplaceholder.typicode.com/users/2",
"redirected": false,
"type": "basic",
"bodyType": "json",
"body": {
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "[email protected]",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": { "lat": "-43.9509", "lng": "-34.4618" }
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}
}
WebSocket 录音
{
"url": "wss://javascript.info/article/websocket/demo/hello",
"log": [
{ "type": "get readyState", "at": 24, "value": 0 },
{ "type": "open", "at": 426 },
{ "type": "get readyState", "at": 426, "value": 1 },
{ "type": "get bufferedAmount", "at": 427, "value": 0 },
{ "type": "send()", "at": 427, "data": "Hi there" },
{ "type": "get bufferedAmount", "at": 427, "value": 8 },
{ "type": "message", "at": 543, "data": "Hello from server, there!" },
{ "type": "close()", "at": 543 },
{ "type": "get readyState", "at": 543, "value": 2 },
{ "type": "close", "at": 661, "code": 1005, "reason": "", "wasClean": true },
{ "type": "get readyState", "at": 661, "value": 3 }
]
}