为啥 Jest 在测试“Colyseus”游戏时会出现这个错误?
Posted
技术标签:
【中文标题】为啥 Jest 在测试“Colyseus”游戏时会出现这个错误?【英文标题】:Why does Jest rise this error while testing `Colyseus` game?为什么 Jest 在测试“Colyseus”游戏时会出现这个错误? 【发布时间】:2021-11-03 14:23:19 【问题描述】:我正在使用Jest
作为测试框架和断言库来测试游戏功能。
这是我的测试设置:
const Server = require("colyseus")
const ColyseusTestServer, boot = require("@colyseus/testing");
const Game = require("./index");
describe("Game", () =>
let colyseus;
let TestServer = new Server()
TestServer.define("my_game", Game)
const User_1 =
key: "token_header.token_payload.and_oh"
beforeAll(async () => colyseus = await boot(TestServer));
afterAll(async () => await colyseus.shutdown());
beforeEach(async () => await colyseus.cleanup());
test("Should connect", async () =>
try
// `room` is the server-side Room instance reference.
const room = await colyseus.sdk.joinOrCreate("my_game", ...User_1);
await room.waitForNextPatch();
await room.waitForNextMessage();
// `client1` is the client-side `Room` instance reference (same as javascript SDK)
// make your assertions
expect(room.sessionId).toEqual(room.clients[0].sessionId);
catch(err)
console.log("errr",err)
)
)
所以每次我运行测试时Jest
都会记录这些errors
:
Game
✕ Should connect (8003 ms)
● Game › Should connect
TypeError: decode[type] is not a function
at decodePrimitiveType (node_modules/@colyseus/schema/src/Schema.ts:109:34)
at _.Object.<anonymous>.Schema.decode (node_modules/@colyseus/schema/src/Schema.ts:352:25)
at SchemaSerializer.Object.<anonymous>.SchemaSerializer.setState (node_modules/colyseus.js/src/serializer/SchemaSerializer.ts:10:20)
at Room.Object.<anonymous>.Room.setState (node_modules/colyseus.js/src/Room.ts:225:25)
at Room.Object.<anonymous>.Room.onMessageCallback (node_modules/colyseus.js/src/Room.ts:203:18)
at WebSocket.onMessage (node_modules/ws/lib/event-target.js:132:16)
at Receiver.receiverOnMessage (node_modules/ws/lib/websocket.js:983:20)
at Receiver.dataMessage (node_modules/ws/lib/receiver.js:502:14)
at Receiver.getData (node_modules/ws/lib/receiver.js:435:17)
at Receiver.startLoop (node_modules/ws/lib/receiver.js:143:22)
at Receiver._write (node_modules/ws/lib/receiver.js:78:10)
at Socket.socketOnData (node_modules/ws/lib/websocket.js:1077:35)
● Game › Should connect
thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
18 | beforeEach(async () => await colyseus.cleanup());
19 | jest.setTimeout(8000)
> 20 | test("Should connect", async () =>
| ^
21 | try
22 | // `room` is the server-side Room instance reference.
23 | const room = await colyseus.sdk.joinOrCreate("my_game", ...User_1);
at server/test.js:20:5
at Object.<anonymous> (server/test.js:7:1)
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:401:19)
at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)
我将timeouts
更改为Jest
,但没有任何改变。
这些是我正在使用的 Colyseus
、Jest
和 Node.js
的版本:
"@colyseus/schema": "^1.0.28",
"@colyseus/testing": "^0.14.22",
"colyseus": "^0.14.18",
"jest": "^27.3.1"
node: v14.7
【问题讨论】:
【参考方案1】:此问题是由于已添加到框架的最新版本中的 colyseus
游戏框架的同步 state
的 schema
的未确定字段类型。所以我只是将类型定义为playerId: [string]
并且它解决了没有任何错误。
【讨论】:
以上是关于为啥 Jest 在测试“Colyseus”游戏时会出现这个错误?的主要内容,如果未能解决你的问题,请参考以下文章
为啥在 Jest 测试中将 async() 转换为 async function()?
为啥 Jest 不让我使用节点测试环境,即使他们自己更改了它?
使用 Jest 运行测试时,为啥 Trix 编辑器不会安装在 Vue 组件中?