测试

您将在下面找到一些带有常见测试库的代码示例:

mocha示例

安装:npm i -D mocha chai

  1. // with { "type": "module" } in your package.json
  2. import { createServer } from "http";
  3. import { io as Client } from "socket.io-client";
  4. import { Server } from "socket.io";
  5. import { assert } from "chai";
  6. // with { "type": "commonjs" } in your package.json
  7. // const { createServer } = require("http");
  8. // const { Server } = require("socket.io");
  9. // const Client = require("socket.io-client");
  10. // const assert = require("chai").assert;
  11. describe("my awesome project", () => {
  12. let io, serverSocket, clientSocket;
  13. before((done) => {
  14. const httpServer = createServer();
  15. io = new Server(httpServer);
  16. httpServer.listen(() => {
  17. const port = httpServer.address().port;
  18. clientSocket = new Client(`http://localhost:${port}`);
  19. io.on("connection", (socket) => {
  20. serverSocket = socket;
  21. });
  22. clientSocket.on("connect", done);
  23. });
  24. });
  25. after(() => {
  26. io.close();
  27. clientSocket.close();
  28. });
  29. it("should work", (done) => {
  30. clientSocket.on("hello", (arg) => {
  31. assert.equal(arg, "world");
  32. done();
  33. });
  34. serverSocket.emit("hello", "world");
  35. });
  36. it("should work (with ack)", (done) => {
  37. serverSocket.on("hi", (cb) => {
  38. cb("hola");
  39. });
  40. clientSocket.emit("hi", (arg) => {
  41. assert.equal(arg, "hola");
  42. done();
  43. });
  44. });
  45. });

jest示例

安装:npm i -D jest

  1. const { createServer } = require("http");
  2. const { Server } = require("socket.io");
  3. const Client = require("socket.io-client");
  4. describe("my awesome project", () => {
  5. let io, serverSocket, clientSocket;
  6. beforeAll((done) => {
  7. const httpServer = createServer();
  8. io = new Server(httpServer);
  9. httpServer.listen(() => {
  10. const port = httpServer.address().port;
  11. clientSocket = new Client(`http://localhost:${port}`);
  12. io.on("connection", (socket) => {
  13. serverSocket = socket;
  14. });
  15. clientSocket.on("connect", done);
  16. });
  17. });
  18. afterAll(() => {
  19. io.close();
  20. clientSocket.close();
  21. });
  22. test("should work", (done) => {
  23. clientSocket.on("hello", (arg) => {
  24. expect(arg).toBe("world");
  25. done();
  26. });
  27. serverSocket.emit("hello", "world");
  28. });
  29. test("should work (with ack)", (done) => {
  30. serverSocket.on("hi", (cb) => {
  31. cb("hola");
  32. });
  33. clientSocket.emit("hi", (arg) => {
  34. expect(arg).toBe("hola");
  35. done();
  36. });
  37. });
  38. });

tape示例

安装:npm i -D tape

  1. const { createServer } = require("http");
  2. const { Server } = require("socket.io");
  3. const Client = require("socket.io-client");
  4. const test = require("tape");
  5. let io, serverSocket, clientSocket;
  6. test("setup", (t) => {
  7. const httpServer = createServer();
  8. io = new Server(httpServer);
  9. httpServer.listen(() => {
  10. const port = httpServer.address().port;
  11. clientSocket = new Client(`http://localhost:${port}`);
  12. io.on("connection", (socket) => {
  13. serverSocket = socket;
  14. });
  15. clientSocket.on("connect", t.end);
  16. });
  17. });
  18. test("it works", (t) => {
  19. t.plan(1);
  20. clientSocket.on("hello", (arg) => {
  21. t.equal(arg, "world");
  22. });
  23. serverSocket.emit("hello", "world");
  24. });
  25. test("it works (with ack)", (t) => {
  26. t.plan(1);
  27. serverSocket.on("hi", (cb) => {
  28. cb("hola");
  29. });
  30. clientSocket.emit("hi", (arg) => {
  31. t.equal(arg, "hola");
  32. });
  33. });
  34. test.onFinish(() => {
  35. io.close();
  36. clientSocket.close();
  37. });