From cab895f477233df0ce125401fc62e2aa73bd155d Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 6 Oct 2020 00:19:15 +0200 Subject: [PATCH] refactor: use prettier to format tests --- package.json | 4 +- support/prod.config.js | 10 +- support/webpack.config.js | 16 +- test/connection.js | 575 +++++++++++++++++++++----------------- test/index.js | 12 +- test/socket.js | 144 +++++----- test/support/env.js | 9 +- test/support/server.js | 126 ++++----- test/url.js | 117 ++++---- 9 files changed, 536 insertions(+), 477 deletions(-) diff --git a/package.json b/package.json index a3d3f4c9..a60b7955 100644 --- a/package.json +++ b/package.json @@ -61,8 +61,8 @@ "test:node": "mocha --reporter dot --require test/support/server.js test/index.js", "test:browser": "zuul test/index.js", "build": "webpack --config ./support/webpack.config.js --config ./support/prod.config.js", - "format:check": "prettier --check 'lib/**/*.ts'", - "format:fix": "prettier --write 'lib/**/*.ts'" + "format:check": "prettier --check 'lib/**/*.ts' 'test/**/*.js' 'support/**/*.js'", + "format:fix": "prettier --write 'lib/**/*.ts' 'test/**/*.js' 'support/**/*.js'" }, "contributors": [ { diff --git a/support/prod.config.js b/support/prod.config.js index 9d5fdcba..2913ae4d 100644 --- a/support/prod.config.js +++ b/support/prod.config.js @@ -4,7 +4,7 @@ module.exports = { ...config, output: { ...config.output, - filename: "socket.io.min.js" + filename: "socket.io.min.js", }, mode: "production", module: { @@ -12,8 +12,8 @@ module.exports = { ...config.module.rules, { test: /\.js$/, - loader: "webpack-remove-debug" - } - ] - } + loader: "webpack-remove-debug", + }, + ], + }, }; diff --git a/support/webpack.config.js b/support/webpack.config.js index b7268d4d..1a34b71a 100644 --- a/support/webpack.config.js +++ b/support/webpack.config.js @@ -22,11 +22,11 @@ module.exports = { } else { return Function('return this')(); } - })()` + })()`, }, mode: "development", node: { - Buffer: false + Buffer: false, }, module: { rules: [ @@ -36,11 +36,11 @@ module.exports = { loader: "babel-loader", options: { presets: ["@babel/preset-env"], - plugins: ["@babel/plugin-transform-object-assign"] - } - } - } - ] + plugins: ["@babel/plugin-transform-object-assign"], + }, + }, + }, + ], }, - plugins: [new BannerPlugin(banner)] + plugins: [new BannerPlugin(banner)], }; diff --git a/test/connection.js b/test/connection.js index a527a035..f6d29f36 100644 --- a/test/connection.js +++ b/test/connection.js @@ -1,88 +1,88 @@ -var expect = require('expect.js'); -var io = require('../'); -var hasCORS = require('has-cors'); -var textBlobBuilder = require('text-blob-builder'); -var env = require('./support/env'); +const expect = require("expect.js"); +const io = require("../"); +const hasCORS = require("has-cors"); +const textBlobBuilder = require("text-blob-builder"); +const env = require("./support/env"); -describe('connection', function () { +describe("connection", function () { this.timeout(70000); - it('should connect to localhost', function (done) { - var socket = io({ forceNew: true }); - socket.emit('hi'); - socket.on('hi', function (data) { + it("should connect to localhost", (done) => { + const socket = io({ forceNew: true }); + socket.emit("hi"); + socket.on("hi", (data) => { socket.disconnect(); done(); }); }); - it('should not connect when autoConnect option set to false', function () { - var socket = io({ forceNew: true, autoConnect: false }); + it("should not connect when autoConnect option set to false", () => { + const socket = io({ forceNew: true, autoConnect: false }); expect(socket.io.engine).to.not.be.ok(); socket.disconnect(); }); - it('should start two connections with same path', function () { - var s1 = io('/'); - var s2 = io('/'); + it("should start two connections with same path", () => { + const s1 = io("/"); + const s2 = io("/"); expect(s1.io).to.not.be(s2.io); s1.disconnect(); s2.disconnect(); }); - it('should start two connections with same path and different querystrings', function () { - var s1 = io('/?woot'); - var s2 = io('/'); + it("should start two connections with same path and different querystrings", () => { + const s1 = io("/?woot"); + const s2 = io("/"); expect(s1.io).to.not.be(s2.io); s1.disconnect(); s2.disconnect(); }); - it('should work with acks', function (done) { - var socket = io({ forceNew: true }); - socket.emit('ack'); - socket.on('ack', function (fn) { + it("should work with acks", (done) => { + const socket = io({ forceNew: true }); + socket.emit("ack"); + socket.on("ack", (fn) => { fn(5, { test: true }); }); - socket.on('got it', function () { + socket.on("got it", () => { socket.disconnect(); done(); }); }); - it('should receive date with ack', function (done) { - var socket = io({ forceNew: true }); - socket.emit('getAckDate', { test: true }, function (data) { - expect(data).to.be.a('string'); + it("should receive date with ack", (done) => { + const socket = io({ forceNew: true }); + socket.emit("getAckDate", { test: true }, (data) => { + expect(data).to.be.a("string"); socket.disconnect(); done(); }); }); - it('should work with false', function (done) { - var socket = io({ forceNew: true }); - socket.emit('false'); - socket.on('false', function (f) { + it("should work with false", (done) => { + const socket = io({ forceNew: true }); + socket.emit("false"); + socket.on("false", (f) => { expect(f).to.be(false); socket.disconnect(); done(); }); }); - it('should receive utf8 multibyte characters', function (done) { - var correct = [ - 'てすと', - 'Я Б Г Д Ж Й', - 'Ä ä Ü ü ß', - 'utf8 — string', - 'utf8 — string' + it("should receive utf8 multibyte characters", (done) => { + const correct = [ + "てすと", + "Я Б Г Д Ж Й", + "Ä ä Ü ü ß", + "utf8 — string", + "utf8 — string", ]; - var socket = io({ forceNew: true }); - var i = 0; - socket.on('takeUtf8', function (data) { + const socket = io({ forceNew: true }); + let i = 0; + socket.on("takeUtf8", (data) => { expect(data).to.be(correct[i]); i++; if (i === correct.length) { @@ -90,15 +90,15 @@ describe('connection', function () { done(); } }); - socket.emit('getUtf8'); + socket.emit("getUtf8"); }); - it('should connect to a namespace after connection established', function (done) { - var manager = new io.Manager(); - var socket = manager.socket('/'); - socket.on('connect', function () { - var foo = manager.socket('/foo'); - foo.on('connect', function () { + it("should connect to a namespace after connection established", (done) => { + const manager = new io.Manager(); + const socket = manager.socket("/"); + socket.on("connect", () => { + const foo = manager.socket("/foo"); + foo.on("connect", () => { foo.close(); socket.close(); manager.close(); @@ -107,73 +107,84 @@ describe('connection', function () { }); }); - it('should open a new namespace after connection gets closed', function (done) { - var manager = new io.Manager(); - var socket = manager.socket('/'); - socket.on('connect', function () { - socket.disconnect(); - }).on('disconnect', function () { - var foo = manager.socket('/foo'); - foo.on('connect', function () { - foo.disconnect(); - manager.close(); - done(); + it("should open a new namespace after connection gets closed", (done) => { + const manager = new io.Manager(); + const socket = manager.socket("/"); + socket + .on("connect", () => { + socket.disconnect(); + }) + .on("disconnect", () => { + const foo = manager.socket("/foo"); + foo.on("connect", () => { + foo.disconnect(); + manager.close(); + done(); + }); }); - }); }); - it('should reconnect by default', function (done) { - var socket = io({ forceNew: true }); - socket.io.on('reconnect', function () { + it("should reconnect by default", (done) => { + const socket = io({ forceNew: true }); + socket.io.on("reconnect", () => { socket.disconnect(); done(); }); - setTimeout(function () { + setTimeout(() => { socket.io.engine.close(); }, 500); }); - it('should reconnect manually', function (done) { - var socket = io({ forceNew: true }); - socket.once('connect', function () { - socket.disconnect(); - }).once('disconnect', function () { - socket.once('connect', function () { + it("should reconnect manually", (done) => { + const socket = io({ forceNew: true }); + socket + .once("connect", () => { socket.disconnect(); - done(); + }) + .once("disconnect", () => { + socket.once("connect", () => { + socket.disconnect(); + done(); + }); + socket.connect(); }); - socket.connect(); - }); }); - it('should reconnect automatically after reconnecting manually', function (done) { - var socket = io({ forceNew: true }); - socket.once('connect', function () { - socket.disconnect(); - }).once('disconnect', function () { - socket.on('reconnect', function () { + it("should reconnect automatically after reconnecting manually", (done) => { + const socket = io({ forceNew: true }); + socket + .once("connect", () => { socket.disconnect(); - done(); + }) + .once("disconnect", () => { + socket.on("reconnect", () => { + socket.disconnect(); + done(); + }); + socket.connect(); + setTimeout(() => { + socket.io.engine.close(); + }, 500); }); - socket.connect(); - setTimeout(function () { - socket.io.engine.close(); - }, 500); - }); }); - it('should attempt reconnects after a failed reconnect', function (done) { - var manager = new io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); - var socket = manager.socket('/timeout'); - socket.once('reconnect_failed', function () { - var reconnects = 0; - var reconnectCb = function () { + it("should attempt reconnects after a failed reconnect", (done) => { + const manager = new io.Manager({ + reconnection: true, + timeout: 0, + reconnectionAttempts: 2, + reconnectionDelay: 10, + }); + const socket = manager.socket("/timeout"); + socket.once("reconnect_failed", () => { + let reconnects = 0; + const reconnectCb = () => { reconnects++; }; - manager.on('reconnect_attempt', reconnectCb); - manager.on('reconnect_failed', function failed () { + manager.on("reconnect_attempt", reconnectCb); + manager.on("reconnect_failed", () => { expect(reconnects).to.be(2); socket.close(); manager.close(); @@ -183,28 +194,34 @@ describe('connection', function () { }); }); - it('reconnect delay should increase every time', function (done) { - var manager = new io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 3, reconnectionDelay: 100, randomizationFactor: 0.2 }); - var socket = manager.socket('/timeout'); - var reconnects = 0; - var increasingDelay = true; - var startTime; - var prevDelay = 0; + it("reconnect delay should increase every time", (done) => { + const manager = new io.Manager({ + reconnection: true, + timeout: 0, + reconnectionAttempts: 3, + reconnectionDelay: 100, + randomizationFactor: 0.2, + }); + const socket = manager.socket("/timeout"); + let reconnects = 0; + let increasingDelay = true; + let startTime; + let prevDelay = 0; - socket.on('connect_error', function () { + socket.on("connect_error", () => { startTime = new Date().getTime(); }); - socket.on('reconnect_attempt', function () { + socket.on("reconnect_attempt", () => { reconnects++; - var currentTime = new Date().getTime(); - var delay = currentTime - startTime; + const currentTime = new Date().getTime(); + const delay = currentTime - startTime; if (delay <= prevDelay) { increasingDelay = false; } prevDelay = delay; }); - socket.on('reconnect_failed', function failed () { + socket.on("reconnect_failed", () => { expect(reconnects).to.be(3); expect(increasingDelay).to.be.ok(); socket.close(); @@ -213,51 +230,63 @@ describe('connection', function () { }); }); - it('reconnect event should fire in socket', function (done) { - var socket = io({ forceNew: true }); + it("reconnect event should fire in socket", (done) => { + const socket = io({ forceNew: true }); - socket.on('reconnect', function () { + socket.on("reconnect", () => { socket.disconnect(); done(); }); - setTimeout(function () { + setTimeout(() => { socket.io.engine.close(); }, 500); }); - it('should not reconnect when force closed', function (done) { - var socket = io('/invalid', { forceNew: true, timeout: 0, reconnectionDelay: 10 }); - socket.on('connect_error', function () { - socket.on('reconnect_attempt', function () { + it("should not reconnect when force closed", (done) => { + const socket = io("/invalid", { + forceNew: true, + timeout: 0, + reconnectionDelay: 10, + }); + socket.on("connect_error", () => { + socket.on("reconnect_attempt", () => { expect().fail(); }); socket.disconnect(); // set a timeout to let reconnection possibly fire - setTimeout(function () { + setTimeout(() => { done(); }, 500); }); }); - it('should stop reconnecting when force closed', function (done) { - var socket = io('/invalid', { forceNew: true, timeout: 0, reconnectionDelay: 10 }); - socket.once('reconnect_attempt', function () { - socket.on('reconnect_attempt', function () { + it("should stop reconnecting when force closed", (done) => { + const socket = io("/invalid", { + forceNew: true, + timeout: 0, + reconnectionDelay: 10, + }); + socket.once("reconnect_attempt", () => { + socket.on("reconnect_attempt", () => { expect().fail(); }); socket.disconnect(); // set a timeout to let reconnection possibly fire - setTimeout(function () { + setTimeout(() => { done(); }, 500); }); }); - it('should reconnect after stopping reconnection', function (done) { - var socket = io('/invalid', { forceNew: true, timeout: 0, reconnectionDelay: 10 }); - socket.once('reconnect_attempt', function () { - socket.on('reconnect_attempt', function () { + it("should reconnect after stopping reconnection", (done) => { + const socket = io("/invalid", { + forceNew: true, + timeout: 0, + reconnectionDelay: 10, + }); + socket.once("reconnect_attempt", () => { + socket.on("reconnect_attempt", () => { socket.disconnect(); done(); }); @@ -266,17 +295,17 @@ describe('connection', function () { }); }); - it('should stop reconnecting on a socket and keep to reconnect on another', function (done) { - var manager = new io.Manager(); - var socket1 = manager.socket('/'); - var socket2 = manager.socket('/asd'); + it("should stop reconnecting on a socket and keep to reconnect on another", (done) => { + const manager = new io.Manager(); + const socket1 = manager.socket("/"); + const socket2 = manager.socket("/asd"); - manager.on('reconnect_attempt', function () { - socket1.on('connect', function () { + manager.on("reconnect_attempt", () => { + socket1.on("connect", () => { expect().fail(); }); - socket2.on('connect', function () { - setTimeout(function () { + socket2.on("connect", () => { + setTimeout(() => { socket2.disconnect(); manager.disconnect(); done(); @@ -285,43 +314,53 @@ describe('connection', function () { socket1.disconnect(); }); - setTimeout(function () { + setTimeout(() => { manager.engine.close(); }, 1000); }); - it('should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled', function (done) { - var manager = new io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); - var socket; + it("should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled", (done) => { + const manager = new io.Manager({ + reconnection: true, + timeout: 0, + reconnectionAttempts: 2, + reconnectionDelay: 10, + }); + let socket; - var reconnects = 0; - var reconnectCb = function () { + let reconnects = 0; + const reconnectCb = () => { reconnects++; }; - manager.on('reconnect_attempt', reconnectCb); - manager.on('reconnect_failed', function failed () { + manager.on("reconnect_attempt", reconnectCb); + manager.on("reconnect_failed", () => { expect(reconnects).to.be(2); socket.close(); manager.close(); done(); }); - socket = manager.socket('/timeout'); + socket = manager.socket("/timeout"); }); - it('should fire reconnect_* events on socket', function (done) { - var manager = new io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); - var socket = manager.socket('/timeout_socket'); + it("should fire reconnect_* events on socket", (done) => { + const manager = new io.Manager({ + reconnection: true, + timeout: 0, + reconnectionAttempts: 2, + reconnectionDelay: 10, + }); + const socket = manager.socket("/timeout_socket"); - var reconnects = 0; - var reconnectCb = function (attempts) { + let reconnects = 0; + const reconnectCb = (attempts) => { reconnects++; expect(attempts).to.be(reconnects); }; - socket.on('reconnect_attempt', reconnectCb); - socket.on('reconnect_failed', function failed () { + socket.on("reconnect_attempt", reconnectCb); + socket.on("reconnect_failed", () => { expect(reconnects).to.be(2); socket.close(); manager.close(); @@ -329,34 +368,39 @@ describe('connection', function () { }); }); - it('should fire error on socket', function (done) { - var manager = new io.Manager({ reconnection: true }); - var socket = manager.socket('/timeout_socket'); + it("should fire error on socket", (done) => { + const manager = new io.Manager({ reconnection: true }); + const socket = manager.socket("/timeout_socket"); - socket.on('error', function (data) { - expect(data.code).to.be('test'); + socket.on("error", (data) => { + expect(data.code).to.be("test"); socket.close(); manager.close(); done(); }); - socket.on('connect', function () { - manager.engine.onPacket({ type: 'error', data: 'test' }); + socket.on("connect", () => { + manager.engine.onPacket({ type: "error", data: "test" }); }); }); - it('should fire reconnecting (on socket) with attempts number when reconnecting twice', function (done) { - var manager = new io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 }); - var socket = manager.socket('/timeout_socket'); + it("should fire reconnecting (on socket) with attempts number when reconnecting twice", (done) => { + const manager = new io.Manager({ + reconnection: true, + timeout: 0, + reconnectionAttempts: 2, + reconnectionDelay: 10, + }); + const socket = manager.socket("/timeout_socket"); - var reconnects = 0; - var reconnectCb = function (attempts) { + let reconnects = 0; + const reconnectCb = (attempts) => { reconnects++; expect(attempts).to.be(reconnects); }; - socket.on('reconnecting', reconnectCb); - socket.on('reconnect_failed', function failed () { + socket.on("reconnecting", reconnectCb); + socket.on("reconnect_failed", () => { expect(reconnects).to.be(2); socket.close(); manager.close(); @@ -364,18 +408,21 @@ describe('connection', function () { }); }); - it('should not try to reconnect and should form a connection when connecting to correct port with default timeout', function (done) { - var manager = new io.Manager({ reconnection: true, reconnectionDelay: 10 }); - var cb = function () { + it("should not try to reconnect and should form a connection when connecting to correct port with default timeout", (done) => { + const manager = new io.Manager({ + reconnection: true, + reconnectionDelay: 10, + }); + const cb = () => { socket.close(); expect().fail(); }; - manager.on('reconnect_attempt', cb); + manager.on("reconnect_attempt", cb); - var socket = manager.socket('/valid'); - socket.on('connect', function () { + var socket = manager.socket("/valid"); + socket.on("connect", () => { // set a timeout to let reconnection possibly fire - setTimeout(function () { + setTimeout(() => { socket.close(); manager.close(); done(); @@ -383,12 +430,12 @@ describe('connection', function () { }); }); - it('should connect while disconnecting another socket', function (done) { - var manager = new io.Manager(); - var socket1 = manager.socket('/foo'); - socket1.on('connect', function () { - var socket2 = manager.socket('/asd'); - socket2.on('connect', done); + it("should connect while disconnecting another socket", (done) => { + const manager = new io.Manager(); + const socket1 = manager.socket("/foo"); + socket1.on("connect", () => { + const socket2 = manager.socket("/asd"); + socket2.on("connect", done); socket1.disconnect(); }); }); @@ -396,17 +443,21 @@ describe('connection', function () { // Ignore incorrect connection test for old IE due to no support for // `script.onerror` (see: http://requirejs.org/docs/api.html#ieloadfail) if (!global.document || hasCORS) { - it('should try to reconnect twice and fail when requested two attempts with incorrect address and reconnect enabled', function (done) { - var manager = new io.Manager('http://localhost:3940', { reconnection: true, reconnectionAttempts: 2, reconnectionDelay: 10 }); - var socket = manager.socket('/asd'); - var reconnects = 0; - var cb = function () { + it("should try to reconnect twice and fail when requested two attempts with incorrect address and reconnect enabled", (done) => { + const manager = new io.Manager("http://localhost:3940", { + reconnection: true, + reconnectionAttempts: 2, + reconnectionDelay: 10, + }); + const socket = manager.socket("/asd"); + let reconnects = 0; + const cb = () => { reconnects++; }; - manager.on('reconnect_attempt', cb); + manager.on("reconnect_attempt", cb); - manager.on('reconnect_failed', function () { + manager.on("reconnect_failed", () => { expect(reconnects).to.be(2); socket.disconnect(); manager.close(); @@ -414,169 +465,181 @@ describe('connection', function () { }); }); - it('should not try to reconnect with incorrect port when reconnection disabled', function (done) { - var manager = new io.Manager('http://localhost:9823', { reconnection: false }); - var cb = function () { + it("should not try to reconnect with incorrect port when reconnection disabled", (done) => { + const manager = new io.Manager("http://localhost:9823", { + reconnection: false, + }); + const cb = () => { socket.close(); expect().fail(); }; - manager.on('reconnect_attempt', cb); + manager.on("reconnect_attempt", cb); - manager.on('connect_error', function () { + manager.on("connect_error", () => { // set a timeout to let reconnection possibly fire - setTimeout(function () { + setTimeout(() => { socket.disconnect(); manager.close(); done(); }, 1000); }); - var socket = manager.socket('/invalid'); + var socket = manager.socket("/invalid"); }); - it('should still try to reconnect twice after opening another socket asynchronously', function (done) { - var manager = new io.Manager( - 'http://localhost:9823', - { reconnect: true, reconnectionAttempts: 2 } + it("should still try to reconnect twice after opening another socket asynchronously", (done) => { + const manager = new io.Manager("http://localhost:9823", { + reconnect: true, + reconnectionAttempts: 2, + }); + let delay = Math.floor( + manager.reconnectionDelay() * manager.randomizationFactor() * 0.5 ); - var delay = Math.floor(manager.reconnectionDelay() * manager.randomizationFactor() * 0.5); delay = Math.max(delay, 10); - var reconnects = 0; - var cb = function () { + let reconnects = 0; + const cb = () => { reconnects++; }; - manager.on('reconnect_attempt', cb); + manager.on("reconnect_attempt", cb); - manager.on('reconnect_failed', function () { + manager.on("reconnect_failed", () => { expect(reconnects).to.be(2); socket.disconnect(); manager.close(); done(); }); - var socket = manager.socket('/room1'); + var socket = manager.socket("/room1"); - setTimeout(function () { - manager.socket('/room2'); + setTimeout(() => { + manager.socket("/room2"); }, delay); }); } - it('should emit date as string', function (done) { - var socket = io({ forceNew: true }); - socket.on('takeDate', function (data) { + it("should emit date as string", (done) => { + const socket = io({ forceNew: true }); + socket.on("takeDate", (data) => { socket.close(); - expect(data).to.be.a('string'); + expect(data).to.be.a("string"); done(); }); - socket.emit('getDate'); + socket.emit("getDate"); }); - it('should emit date in object', function (done) { - var socket = io({ forceNew: true }); - socket.on('takeDateObj', function (data) { + it("should emit date in object", (done) => { + const socket = io({ forceNew: true }); + socket.on("takeDateObj", (data) => { socket.close(); - expect(data).to.be.an('object'); - expect(data.date).to.be.a('string'); + expect(data).to.be.an("object"); + expect(data.date).to.be.a("string"); done(); }); - socket.emit('getDateObj'); + socket.emit("getDateObj"); }); if (!global.Blob && !global.ArrayBuffer) { - it('should get base64 data as a last resort', function (done) { - var socket = io({ forceNew: true }); - socket.on('takebin', function (a) { + it("should get base64 data as a last resort", (done) => { + const socket = io({ forceNew: true }); + socket.on("takebin", (a) => { socket.disconnect(); expect(a.base64).to.be(true); - expect(a.data).to.eql('YXNkZmFzZGY='); + expect(a.data).to.eql("YXNkZmFzZGY="); done(); }); - socket.emit('getbin'); + socket.emit("getbin"); }); } if (global.ArrayBuffer) { - var base64 = require('base64-arraybuffer'); + const base64 = require("base64-arraybuffer"); - it('should get binary data (as an ArrayBuffer)', function (done) { - var socket = io({ forceNew: true }); + it("should get binary data (as an ArrayBuffer)", (done) => { + const socket = io({ forceNew: true }); if (env.node) { - socket.io.engine.binaryType = 'arraybuffer'; + socket.io.engine.binaryType = "arraybuffer"; } - socket.emit('doge'); - socket.on('doge', function (buffer) { + socket.emit("doge"); + socket.on("doge", (buffer) => { expect(buffer instanceof ArrayBuffer).to.be(true); socket.disconnect(); done(); }); }); - it('should send binary data (as an ArrayBuffer)', function (done) { - var socket = io({ forceNew: true }); - socket.on('buffack', function () { + it("should send binary data (as an ArrayBuffer)", (done) => { + const socket = io({ forceNew: true }); + socket.on("buffack", () => { socket.disconnect(); done(); }); - var buf = base64.decode('asdfasdf'); - socket.emit('buffa', buf); + const buf = base64.decode("asdfasdf"); + socket.emit("buffa", buf); }); - it('should send binary data (as an ArrayBuffer) mixed with json', function (done) { - var socket = io({ forceNew: true }); - socket.on('jsonbuff-ack', function () { + it("should send binary data (as an ArrayBuffer) mixed with json", (done) => { + const socket = io({ forceNew: true }); + socket.on("jsonbuff-ack", () => { socket.disconnect(); done(); }); - var buf = base64.decode('howdy'); - socket.emit('jsonbuff', {hello: 'lol', message: buf, goodbye: 'gotcha'}); + const buf = base64.decode("howdy"); + socket.emit("jsonbuff", { + hello: "lol", + message: buf, + goodbye: "gotcha", + }); }); - it('should send events with ArrayBuffers in the correct order', function (done) { - var socket = io({ forceNew: true }); - socket.on('abuff2-ack', function () { + it("should send events with ArrayBuffers in the correct order", (done) => { + const socket = io({ forceNew: true }); + socket.on("abuff2-ack", () => { socket.disconnect(); done(); }); - var buf = base64.decode('abuff1'); - socket.emit('abuff1', buf); - socket.emit('abuff2', 'please arrive second'); + const buf = base64.decode("abuff1"); + socket.emit("abuff1", buf); + socket.emit("abuff2", "please arrive second"); }); } - if (global.Blob && null != textBlobBuilder('xxx')) { - it('should send binary data (as a Blob)', function (done) { - var socket = io({ forceNew: true }); - socket.on('back', function () { + if (global.Blob && null != textBlobBuilder("xxx")) { + it("should send binary data (as a Blob)", (done) => { + const socket = io({ forceNew: true }); + socket.on("back", () => { socket.disconnect(); done(); }); - var blob = textBlobBuilder('hello world'); - socket.emit('blob', blob); + const blob = textBlobBuilder("hello world"); + socket.emit("blob", blob); }); - it('should send binary data (as a Blob) mixed with json', function (done) { - var socket = io({ forceNew: true }); - socket.on('jsonblob-ack', function () { + it("should send binary data (as a Blob) mixed with json", (done) => { + const socket = io({ forceNew: true }); + socket.on("jsonblob-ack", () => { socket.disconnect(); done(); }); - var blob = textBlobBuilder('EEEEEEEEE'); - socket.emit('jsonblob', {hello: 'lol', message: blob, goodbye: 'gotcha'}); + const blob = textBlobBuilder("EEEEEEEEE"); + socket.emit("jsonblob", { + hello: "lol", + message: blob, + goodbye: "gotcha", + }); }); - it('should send events with Blobs in the correct order', function (done) { - var socket = io({ forceNew: true }); - socket.on('blob3-ack', function () { + it("should send events with Blobs in the correct order", (done) => { + const socket = io({ forceNew: true }); + socket.on("blob3-ack", () => { socket.disconnect(); done(); }); - var blob = textBlobBuilder('BLOBBLOB'); - socket.emit('blob1', blob); - socket.emit('blob2', 'second'); - socket.emit('blob3', blob); + const blob = textBlobBuilder("BLOBBLOB"); + socket.emit("blob1", blob); + socket.emit("blob2", "second"); + socket.emit("blob3", blob); }); } }); diff --git a/test/index.js b/test/index.js index 83ced1d1..bf69ccdc 100644 --- a/test/index.js +++ b/test/index.js @@ -1,14 +1,12 @@ - -require('./support/env'); +require("./support/env"); // whitelist some globals to avoid warnings if (global.mocha) { - global.mocha.globals(['___eio', 'eio_iframe_*']); + global.mocha.globals(["___eio", "eio_iframe_*"]); } -require('./url'); +require("./url"); // browser only tests -require('./connection'); -require('./socket'); - +require("./connection"); +require("./socket"); diff --git a/test/socket.js b/test/socket.js index 91abd9d8..8dea4d5e 100644 --- a/test/socket.js +++ b/test/socket.js @@ -1,12 +1,12 @@ -var expect = require('expect.js'); -var io = require('../'); +const expect = require("expect.js"); +const io = require("../"); -describe('socket', function () { +describe("socket", function () { this.timeout(70000); - it('should have an accessible socket id equal to the server-side socket id (default namespace)', function (done) { - var socket = io({ forceNew: true }); - socket.on('connect', function () { + it("should have an accessible socket id equal to the server-side socket id (default namespace)", (done) => { + const socket = io({ forceNew: true }); + socket.on("connect", () => { expect(socket.id).to.be.ok(); expect(socket.id).to.eql(socket.io.engine.id); socket.disconnect(); @@ -14,20 +14,20 @@ describe('socket', function () { }); }); - it('should have an accessible socket id equal to the server-side socket id (custom namespace)', function (done) { - var socket = io('/foo', { forceNew: true }); - socket.on('connect', function () { + it("should have an accessible socket id equal to the server-side socket id (custom namespace)", (done) => { + const socket = io("/foo", { forceNew: true }); + socket.on("connect", () => { expect(socket.id).to.be.ok(); - expect(socket.id).to.eql('/foo#' + socket.io.engine.id); + expect(socket.id).to.eql("/foo#" + socket.io.engine.id); socket.disconnect(); done(); }); }); - it('clears socket.id upon disconnection', function (done) { - var socket = io({ forceNew: true }); - socket.on('connect', function () { - socket.on('disconnect', function () { + it("clears socket.id upon disconnection", (done) => { + const socket = io({ forceNew: true }); + socket.on("connect", () => { + socket.on("disconnect", () => { expect(socket.id).to.not.be.ok(); done(); }); @@ -36,43 +36,43 @@ describe('socket', function () { }); }); - it('doesn\'t fire a connect_error if we force disconnect in opening state', function (done) { - var socket = io({ forceNew: true, timeout: 100 }); + it("doesn't fire a connect_error if we force disconnect in opening state", (done) => { + const socket = io({ forceNew: true, timeout: 100 }); socket.disconnect(); - socket.on('connect_error', function () { - throw new Error('Unexpected'); + socket.on("connect_error", () => { + throw new Error("Unexpected"); }); - setTimeout(function () { + setTimeout(() => { done(); }, 300); }); - it('should ping and pong with latency', function (done) { - var socket = io({ forceNew: true }); - socket.on('connect', function () { - var pinged; - socket.once('ping', function () { + it("should ping and pong with latency", (done) => { + const socket = io({ forceNew: true }); + socket.on("connect", () => { + let pinged; + socket.once("ping", () => { pinged = true; }); - socket.once('pong', function (ms) { + socket.once("pong", (ms) => { expect(pinged).to.be(true); - expect(ms).to.be.a('number'); + expect(ms).to.be.a("number"); socket.disconnect(); done(); }); }); }); - it('should change socket.id upon reconnection', function (done) { - var socket = io({ forceNew: true }); - socket.on('connect', function () { - var id = socket.id; + it("should change socket.id upon reconnection", (done) => { + const socket = io({ forceNew: true }); + socket.on("connect", () => { + const id = socket.id; - socket.on('reconnect_attempt', function () { + socket.on("reconnect_attempt", () => { expect(socket.id).to.not.be.ok(); }); - socket.on('reconnect', function () { + socket.on("reconnect", () => { expect(socket.id).to.not.eql(id); socket.disconnect(); done(); @@ -82,96 +82,96 @@ describe('socket', function () { }); }); - it('should enable compression by default', function (done) { - var socket = io({ forceNew: true }); - socket.on('connect', function () { - socket.io.engine.once('packetCreate', function (packet) { + it("should enable compression by default", (done) => { + const socket = io({ forceNew: true }); + socket.on("connect", () => { + socket.io.engine.once("packetCreate", (packet) => { expect(packet.options.compress).to.be(true); socket.disconnect(); done(); }); - socket.emit('hi'); + socket.emit("hi"); }); }); - it('should disable compression', function (done) { - var socket = io({ forceNew: true }); - socket.on('connect', function () { - socket.io.engine.once('packetCreate', function (packet) { + it("should disable compression", (done) => { + const socket = io({ forceNew: true }); + socket.on("connect", () => { + socket.io.engine.once("packetCreate", (packet) => { expect(packet.options.compress).to.be(false); socket.disconnect(); done(); }); - socket.compress(false).emit('hi'); + socket.compress(false).emit("hi"); }); }); - describe('query option', function () { - it('should accept an object (default namespace)', function (done) { - var socket = io('/', { forceNew: true, query: { e: 'f' } }); + describe("query option", () => { + it("should accept an object (default namespace)", (done) => { + const socket = io("/", { forceNew: true, query: { e: "f" } }); - socket.emit('getHandshake', function (handshake) { - expect(handshake.query.e).to.be('f'); + socket.emit("getHandshake", (handshake) => { + expect(handshake.query.e).to.be("f"); socket.disconnect(); done(); }); }); - it('should accept a query string (default namespace)', function (done) { - var socket = io('/?c=d', { forceNew: true }); + it("should accept a query string (default namespace)", (done) => { + const socket = io("/?c=d", { forceNew: true }); - socket.emit('getHandshake', function (handshake) { - expect(handshake.query.c).to.be('d'); + socket.emit("getHandshake", (handshake) => { + expect(handshake.query.c).to.be("d"); socket.disconnect(); done(); }); }); - it('should accept an object', function (done) { - var socket = io('/abc', {query: {a: 'b'}}); + it("should accept an object", (done) => { + const socket = io("/abc", { query: { a: "b" } }); - socket.on('handshake', function (handshake) { - expect(handshake.query.a).to.be('b'); + socket.on("handshake", (handshake) => { + expect(handshake.query.a).to.be("b"); socket.disconnect(); done(); }); }); - it('should accept a query string', function (done) { - var socket = io('/abc?b=c&d=e'); + it("should accept a query string", (done) => { + const socket = io("/abc?b=c&d=e"); - socket.on('handshake', function (handshake) { - expect(handshake.query.b).to.be('c'); - expect(handshake.query.d).to.be('e'); + socket.on("handshake", (handshake) => { + expect(handshake.query.b).to.be("c"); + expect(handshake.query.d).to.be("e"); socket.disconnect(); done(); }); }); - it('should properly encode the parameters', function (done) { - var socket = io('/abc', {query: {'&a': '&=?a'}}); + it("should properly encode the parameters", (done) => { + const socket = io("/abc", { query: { "&a": "&=?a" } }); - socket.on('handshake', function (handshake) { - expect(handshake.query['&a']).to.be('&=?a'); + socket.on("handshake", (handshake) => { + expect(handshake.query["&a"]).to.be("&=?a"); socket.disconnect(); done(); }); }); }); - it('should fire an error event on middleware failure from main namespace', function (done) { - var socket = io('/foo', { forceNew: true, query: { 'fail': true } }); - socket.on('error', function (err) { - expect(err).to.eql('Auth failed (main namespace)'); + it("should fire an error event on middleware failure from main namespace", (done) => { + const socket = io("/foo", { forceNew: true, query: { fail: true } }); + socket.on("error", (err) => { + expect(err).to.eql("Auth failed (main namespace)"); socket.disconnect(); done(); }); }); - it('should fire an error event on middleware failure from custom namespace', function (done) { - var socket = io('/no', { forceNew: true }); - socket.on('error', function (err) { - expect(err).to.eql('Auth failed (custom namespace)'); + it("should fire an error event on middleware failure from custom namespace", (done) => { + const socket = io("/no", { forceNew: true }); + socket.on("error", (err) => { + expect(err).to.eql("Auth failed (custom namespace)"); socket.disconnect(); done(); }); diff --git a/test/support/env.js b/test/support/env.js index c0862307..4c111945 100644 --- a/test/support/env.js +++ b/test/support/env.js @@ -7,10 +7,9 @@ exports.node = !exports.browser; if (!global.location) { global.location = { - protocol: 'http:', - host: 'localhost:3210', - hostname: 'localhost', - port: '3210' + protocol: "http:", + host: "localhost:3210", + hostname: "localhost", + port: "3210", }; } - diff --git a/test/support/server.js b/test/support/server.js index 23dd49f3..1c1671a2 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -1,148 +1,148 @@ - // this is a test server to support tests which make requests -var io = require('socket.io'); -var server = io(process.env.ZUUL_PORT || 3210, { pingInterval: 2000 }); -var expect = require('expect.js'); +const io = require("socket.io"); +const server = io(process.env.ZUUL_PORT || 3210, { pingInterval: 2000 }); +const expect = require("expect.js"); -server.of('/foo').on('connection', function () { +server.of("/foo").on("connection", () => { // register namespace }); -server.of('/timeout_socket').on('connection', function () { +server.of("/timeout_socket").on("connection", () => { // register namespace }); -server.of('/valid').on('connection', function () { +server.of("/valid").on("connection", () => { // register namespace }); -server.of('/asd').on('connection', function () { +server.of("/asd").on("connection", () => { // register namespace }); -server.of('/abc').on('connection', function (socket) { - socket.emit('handshake', socket.handshake); +server.of("/abc").on("connection", (socket) => { + socket.emit("handshake", socket.handshake); }); -server.use(function (socket, next) { - if (socket.request._query.fail) return next(new Error('Auth failed (main namespace)')); +server.use((socket, next) => { + if (socket.request._query.fail) + return next(new Error("Auth failed (main namespace)")); next(); }); -server.of('/no').use(function (socket, next) { - next(new Error('Auth failed (custom namespace)')); +server.of("/no").use((socket, next) => { + next(new Error("Auth failed (custom namespace)")); }); -server.on('connection', function (socket) { +server.on("connection", (socket) => { // simple test - socket.on('hi', function () { - socket.emit('hi'); + socket.on("hi", () => { + socket.emit("hi"); }); // ack tests - socket.on('ack', function () { - socket.emit('ack', function (a, b) { + socket.on("ack", () => { + socket.emit("ack", (a, b) => { if (a === 5 && b.test) { - socket.emit('got it'); + socket.emit("got it"); } }); }); - socket.on('getAckDate', function (data, cb) { + socket.on("getAckDate", (data, cb) => { cb(new Date()); }); - socket.on('getDate', function () { - socket.emit('takeDate', new Date()); + socket.on("getDate", () => { + socket.emit("takeDate", new Date()); }); - socket.on('getDateObj', function () { - socket.emit('takeDateObj', { date: new Date() }); + socket.on("getDateObj", () => { + socket.emit("takeDateObj", { date: new Date() }); }); - socket.on('getUtf8', function () { - socket.emit('takeUtf8', 'てすと'); - socket.emit('takeUtf8', 'Я Б Г Д Ж Й'); - socket.emit('takeUtf8', 'Ä ä Ü ü ß'); - socket.emit('takeUtf8', 'utf8 — string'); - socket.emit('takeUtf8', 'utf8 — string'); + socket.on("getUtf8", () => { + socket.emit("takeUtf8", "てすと"); + socket.emit("takeUtf8", "Я Б Г Д Ж Й"); + socket.emit("takeUtf8", "Ä ä Ü ü ß"); + socket.emit("takeUtf8", "utf8 — string"); + socket.emit("takeUtf8", "utf8 — string"); }); // false test - socket.on('false', function () { - socket.emit('false', false); + socket.on("false", () => { + socket.emit("false", false); }); // binary test - socket.on('doge', function () { - var buf = new Buffer('asdfasdf', 'utf8'); - socket.emit('doge', buf); + socket.on("doge", () => { + const buf = Buffer.from("asdfasdf", "utf8"); + socket.emit("doge", buf); }); // expect receiving binary to be buffer - socket.on('buffa', function (a) { - if (Buffer.isBuffer(a)) socket.emit('buffack'); + socket.on("buffa", (a) => { + if (Buffer.isBuffer(a)) socket.emit("buffack"); }); // expect receiving binary with mixed JSON - socket.on('jsonbuff', function (a) { - expect(a.hello).to.eql('lol'); + socket.on("jsonbuff", (a) => { + expect(a.hello).to.eql("lol"); expect(Buffer.isBuffer(a.message)).to.be(true); - expect(a.goodbye).to.eql('gotcha'); - socket.emit('jsonbuff-ack'); + expect(a.goodbye).to.eql("gotcha"); + socket.emit("jsonbuff-ack"); }); // expect receiving buffers in order - var receivedAbuff1 = false; - socket.on('abuff1', function (a) { + let receivedAbuff1 = false; + socket.on("abuff1", (a) => { expect(Buffer.isBuffer(a)).to.be(true); receivedAbuff1 = true; }); - socket.on('abuff2', function (a) { + socket.on("abuff2", (a) => { expect(receivedAbuff1).to.be(true); - socket.emit('abuff2-ack'); + socket.emit("abuff2-ack"); }); // expect sent blob to be buffer - socket.on('blob', function (a) { - if (Buffer.isBuffer(a)) socket.emit('back'); + socket.on("blob", (a) => { + if (Buffer.isBuffer(a)) socket.emit("back"); }); // expect sent blob mixed with json to be buffer - socket.on('jsonblob', function (a) { - expect(a.hello).to.eql('lol'); + socket.on("jsonblob", (a) => { + expect(a.hello).to.eql("lol"); expect(Buffer.isBuffer(a.message)).to.be(true); - expect(a.goodbye).to.eql('gotcha'); - socket.emit('jsonblob-ack'); + expect(a.goodbye).to.eql("gotcha"); + socket.emit("jsonblob-ack"); }); // expect blobs sent in order to arrive in correct order - var receivedblob1 = false; - var receivedblob2 = false; - socket.on('blob1', function (a) { + let receivedblob1 = false; + let receivedblob2 = false; + socket.on("blob1", (a) => { expect(Buffer.isBuffer(a)).to.be(true); receivedblob1 = true; }); - socket.on('blob2', function (a) { + socket.on("blob2", (a) => { expect(receivedblob1).to.be(true); - expect(a).to.eql('second'); + expect(a).to.eql("second"); receivedblob2 = true; }); - socket.on('blob3', function (a) { + socket.on("blob3", (a) => { expect(Buffer.isBuffer(a)).to.be(true); expect(receivedblob1).to.be(true); expect(receivedblob2).to.be(true); - socket.emit('blob3-ack'); + socket.emit("blob3-ack"); }); // emit buffer to base64 receiving browsers - socket.on('getbin', function () { - var buf = new Buffer('asdfasdf', 'utf8'); - socket.emit('takebin', buf); + socket.on("getbin", () => { + const buf = Buffer.from("asdfasdf", "utf8"); + socket.emit("takebin", buf); }); - socket.on('getHandshake', function (cb) { + socket.on("getHandshake", (cb) => { cb(socket.handshake); }); }); diff --git a/test/url.js b/test/url.js index 17844a2a..9462eb6e 100644 --- a/test/url.js +++ b/test/url.js @@ -1,83 +1,82 @@ +const loc = {}; +const { url } = require("../build/url"); +const expect = require("expect.js"); -var loc = {}; -var { url } = require('../build/url'); -var expect = require('expect.js'); - -describe('url', function () { - it('works with undefined', function () { - loc.hostname = 'woot.com'; - loc.protocol = 'https:'; +describe("url", () => { + it("works with undefined", () => { + loc.hostname = "woot.com"; + loc.protocol = "https:"; loc.port = 4005; - loc.host = loc.hostname + ':' + loc.port; - var parsed = url(undefined, loc); - expect(parsed.host).to.be('woot.com'); - expect(parsed.protocol).to.be('https'); - expect(parsed.port).to.be('4005'); + loc.host = loc.hostname + ":" + loc.port; + const parsed = url(undefined, loc); + expect(parsed.host).to.be("woot.com"); + expect(parsed.protocol).to.be("https"); + expect(parsed.port).to.be("4005"); }); - it('works with relative paths', function () { - loc.hostname = 'woot.com'; - loc.protocol = 'https:'; + it("works with relative paths", () => { + loc.hostname = "woot.com"; + loc.protocol = "https:"; loc.port = 3000; - loc.host = loc.hostname + ':' + loc.port; - var parsed = url('/test', loc); - expect(parsed.host).to.be('woot.com'); - expect(parsed.protocol).to.be('https'); - expect(parsed.port).to.be('3000'); + loc.host = loc.hostname + ":" + loc.port; + const parsed = url("/test", loc); + expect(parsed.host).to.be("woot.com"); + expect(parsed.protocol).to.be("https"); + expect(parsed.port).to.be("3000"); }); - it('works with no protocol', function () { - loc.protocol = 'http:'; - var parsed = url('localhost:3000', loc); - expect(parsed.host).to.be('localhost'); - expect(parsed.port).to.be('3000'); - expect(parsed.protocol).to.be('http'); + it("works with no protocol", () => { + loc.protocol = "http:"; + const parsed = url("localhost:3000", loc); + expect(parsed.host).to.be("localhost"); + expect(parsed.port).to.be("3000"); + expect(parsed.protocol).to.be("http"); }); - it('works with no schema', function () { - loc.protocol = 'http:'; - var parsed = url('//localhost:3000', loc); - expect(parsed.host).to.be('localhost'); - expect(parsed.port).to.be('3000'); - expect(parsed.protocol).to.be('http'); + it("works with no schema", () => { + loc.protocol = "http:"; + const parsed = url("//localhost:3000", loc); + expect(parsed.host).to.be("localhost"); + expect(parsed.port).to.be("3000"); + expect(parsed.protocol).to.be("http"); }); - it('forces ports for unique url ids', function () { - var id1 = url('http://google.com:80/'); - var id2 = url('http://google.com/'); - var id3 = url('https://google.com/'); + it("forces ports for unique url ids", () => { + const id1 = url("http://google.com:80/"); + const id2 = url("http://google.com/"); + const id3 = url("https://google.com/"); expect(id1.id).to.be(id2.id); expect(id1.id).to.not.be(id3.id); expect(id2.id).to.not.be(id3.id); }); - it('identifies the namespace', function () { - loc.protocol = 'http:'; - loc.hostname = 'woot.com'; + it("identifies the namespace", () => { + loc.protocol = "http:"; + loc.hostname = "woot.com"; - expect(url('/woot', loc).path).to.be('/woot'); - expect(url('http://google.com').path).to.be('/'); - expect(url('http://google.com/').path).to.be('/'); + expect(url("/woot", loc).path).to.be("/woot"); + expect(url("http://google.com").path).to.be("/"); + expect(url("http://google.com/").path).to.be("/"); }); - it('works with ipv6', function () { - var parsed = url('http://[::1]'); - expect(parsed.protocol).to.be('http'); - expect(parsed.host).to.be('::1'); - expect(parsed.port).to.be('80'); - expect(parsed.id).to.be('http://[::1]:80'); + it("works with ipv6", () => { + const parsed = url("http://[::1]"); + expect(parsed.protocol).to.be("http"); + expect(parsed.host).to.be("::1"); + expect(parsed.port).to.be("80"); + expect(parsed.id).to.be("http://[::1]:80"); }); - it('works with ipv6 location', function () { - loc.protocol = 'http:'; - loc.hostname = '[::1]'; - loc.port = ''; - loc.host = loc.hostname + ':' + loc.port; + it("works with ipv6 location", () => { + loc.protocol = "http:"; + loc.hostname = "[::1]"; + loc.port = ""; + loc.host = loc.hostname + ":" + loc.port; - var parsed = url(undefined, loc); - expect(parsed.protocol).to.be('http'); - expect(parsed.host).to.be('::1'); - expect(parsed.port).to.be('80'); - expect(parsed.id).to.be('http://[::1]:80'); + const parsed = url(undefined, loc); + expect(parsed.protocol).to.be("http"); + expect(parsed.host).to.be("::1"); + expect(parsed.port).to.be("80"); + expect(parsed.id).to.be("http://[::1]:80"); }); });