Я использую библиотеку тестирования Jest для моего проектаact.js.Я хочу использовать его для тестирования своего приложения Node.js, я пробовал много примеров, но все же все примеры jest не работают.
Один пример тестирования соединения socket.io с Mocha & Chaiработал, но когда я использую Jest, он успешно подключен к серверу socket.io, и события отправляются, но не могут быть прослушаны.Я также обновил пакет socket.io в обоих случаях.
вот пример использования mocha & chai https://github.com/agconti/socket.io.tests
и вот мой код:
index.js
var express = require("express"),
app = express(),
port = process.env.PORT || 9000,
http = require("http").Server(app),
io = require("socket.io")(http);
io.on("connection", function(socket) {
console.log("connected");
socket.on("message", function(msg) {
console.log("emitted");
io.sockets.emit("message", msg);
});
});
// export the server so it can be easily called for testing
exports.server = http.listen(port);
index.test.js
jest.setTimeout(50000);
var server = require("../index"),
io = require("socket.io-client"),
ioOptions = {
transports: ["websocket"],
forceNew: true,
reconnection: false
},
testMsg = "HelloWorld",
sender,
receiver;
describe("Chat Events", function() {
beforeEach(function(done) {
// start the io server
// server.start();
// connect two io clients
sender = io("http://localhost:9000/", ioOptions);
receiver = io("http://localhost:9000/", ioOptions);
// finish beforeEach setup
done();
});
afterEach(function(done) {
// disconnect io clients after each test
sender.disconnect();
receiver.disconnect();
done();
});
describe("Message Events", function() {
it("Clients should receive a message when the `message` event is emited.", function(done) {
sender.emit("message", testMsg);
receiver.on("message", function(msg) {
console.log("subscribed");
expect(msg).toEqual(testMsg);
done();
});
});
});
});
вот результат теста:
JEST
console.log server/index.js:37
connectedddd
console.log server/index.js:40
emitted
console.log server/index.js:37
connectedddd
console.log server/index.js:37
connectedddd
FAIL server/__tests__/index.test.js (58.299s)
Chat Events
Message Events
✕ Clients should receive a message when the `message` event is emited. (50105ms)
● Chat Events › Message Events › Clients should receive a message when the `message` event is emited.
Timeout - Async callback was not invoked within the 50000ms timeout specified by jest.setTimeout.Error:
130 |
131 | describe("Message Events", function() {
> 132 | it("Clients should receive a message when the `message` event is emited.", function(done) {
| ^
133 | sender.emit("message", testMsg);
134 | receiver.on("message", function(msg) {
135 | console.log("subscribed");
at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
at Suite.it (server/__tests__/index.test.js:132:5)
at Suite.describe (server/__tests__/index.test.js:131:3)
at Object.describe (server/__tests__/index.test.js:113:1)
Моча и чай
> node_socket_test@0.0.0 test /home/hassan/socket.io.tests
> mocha test
Chat Events
Message Events
connectedddd
connectedddd
subscribed
✓ Clients should receive a message when the `message` event is emited.
1 passing (53ms)