Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Dec 20, 2024
1 parent c504048 commit af84136
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 43 deletions.
10 changes: 5 additions & 5 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ export default [
globals: pluginJest.environments.globals.globals,
},
rules: {
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "error",

...pluginJest.configs["flat/style"].rules,
...pluginJest.configs["flat/recommended"].rules,
"jest/no-done-callback": "off",
"jest/prefer-to-be": "off",
"jest/no-conditional-expect": "off",
// https://github.com/jest-community/eslint-plugin-jest/pull/1688
"jest/valid-expect": "off",
// "jest/valid-expect": [
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

// eslint-disable-next-line n/no-extraneous-require
const { defaults } = require("jest-config");

/** @type {import('jest').Config} */
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"exorcist": "^2.0.0",
"husky": "^9.1.7",
"jest": "^29.7.0",
"jest-config": "^29.7.0",
"jest-extended": "^4.0.2",
"jsdom": "^25.0.1",
"lerna": "^8.1.9",
Expand Down
40 changes: 20 additions & 20 deletions packages/connection/test/close.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
"use strict";

const Connection = require("..");
const { EventEmitter, promise, timeout } = require("@xmpp/events");
const {
EventEmitter,
promise,
timeout,
TimeoutError,
} = require("@xmpp/events");
const xml = require("@xmpp/xml");

test("resets properties on socket close event", () => {
Expand All @@ -15,7 +20,7 @@ test("resets properties on socket close event", () => {
expect(conn.status).toBe("disconnect");
});

test("timeout", (done) => {
test("timeout", async () => {
expect.assertions(2);
const conn = new Connection();
conn.parser = new EventEmitter();
Expand All @@ -31,14 +36,11 @@ test("timeout", (done) => {
conn.on("output", (el) => {
expect(el).toBe("<hello/>");
});
conn.close().catch((err) => {
expect(err.name).toBe("TimeoutError");
done();
});

await expect(conn.close()).rejects.toThrow(new TimeoutError());
});

test("error on status closing", (done) => {
expect.assertions(2);
test("error on status closing", async () => {
const conn = new Connection();
conn.parser = new EventEmitter();
conn.footerElement = () => {
Expand All @@ -51,12 +53,12 @@ test("error on status closing", (done) => {
};

conn.status = "closing";
conn.close().catch((err) => {
expect(err.name).toBe("Error");
expect(err.message).toBe("Connection is closing");
done();
});

conn.parser.emit("end");

await expect(conn.close()).rejects.toThrow(
new Error("Connection is closing"),
);
});

test("resolves", async () => {
Expand Down Expand Up @@ -105,7 +107,7 @@ test("emits closing status", () => {
return p;
});

test("do not emit closing status if parser property is missing", () => {
test("do not emit closing status if parser property is missing", async () => {
expect.assertions(2);
const conn = new Connection();
conn.parser = null;
Expand All @@ -118,12 +120,10 @@ test("do not emit closing status if parser property is missing", () => {
return cb();
};

return Promise.all([
timeout(promise(conn, "status"), 500).catch((err) =>
expect(err.name).toBe("TimeoutError"),
await Promise.all([
expect(timeout(promise(conn, "status"), 500)).rejects.toThrow(
new TimeoutError(),
),
conn.close().catch((err) => {
expect(err).toBeInstanceOf(Error);
}),
expect(conn.close()).rejects.toThrow(),
]);
});
8 changes: 7 additions & 1 deletion packages/connection/test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ test('emits "connecting" status', () => {
});

test("rejects if an error is emitted before connected", async () => {
expect.assertions(2);

const conn = new Connection();
const error = {};

// eslint-disable-next-line func-names
conn.Socket = socket(function () {
this.emit("error", error);
});
conn.on("error", (err) => expect(err).toBe(error));
conn.on("error", (err) => {
expect(err).toBe(error);
});

try {
await conn.connect("url");
expect.fail();
} catch (err) {
expect(err).toBe(error);
}
Expand All @@ -50,4 +55,5 @@ test("resolves if socket connects", async () => {
this.emit("connect");
});
await conn.connect("url");
expect().pass();
});
4 changes: 4 additions & 0 deletions packages/connection/test/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ test("resolves if socket property is undefined", async () => {
conn.footerElement = () => <foo />;
conn.socket = undefined;
await conn.stop();
expect().pass();
});

test("resolves if close rejects", async () => {
const conn = new Connection();
conn.close = () => Promise.reject();
conn.disconnect = () => Promise.resolve();
await conn.stop();
expect().pass();
});

test("resolves if disconnect rejects", async () => {
const conn = new Connection();
conn.disconnect = () => Promise.reject();
conn.close = () => Promise.resolve();
await conn.stop();
expect().pass();
});

test("resolves with the result of close", async () => {
Expand All @@ -35,4 +38,5 @@ test("resolves with the result of close", async () => {
test("does not throw if connection is not established", async () => {
const conn = new Connection();
await conn.stop();
expect().pass();
});
8 changes: 4 additions & 4 deletions packages/jid/test/JID.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
const JID = require("../lib/JID");

test("throws TypeError for invalid domain", () => {
expect(() => new JID("foo")).toThrowError(new TypeError("Invalid domain."));
expect(() => new JID("foo")).toThrow(new TypeError("Invalid domain."));

expect(() => new JID()).toThrowError(new TypeError("Invalid domain."));
expect(() => new JID()).toThrow(new TypeError("Invalid domain."));

expect(() => new JID("foo", "", "r")).toThrowError(
expect(() => new JID("foo", "", "r")).toThrow(
new TypeError("Invalid domain."),
);

expect(() => new JID("foo", "", "r")).toThrowError(
expect(() => new JID("foo", "", "r")).toThrow(
new TypeError("Invalid domain."),
);
});
6 changes: 5 additions & 1 deletion packages/reconnect/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ const _reconnect = require(".");
const EventEmitter = require("events");

test("it schedule a reconnect when disconnect is emitted", () => {
expect.assertions(1);

const entity = new EventEmitter();
const reconnect = _reconnect({ entity });

reconnect.scheduleReconnect = () => {};
reconnect.scheduleReconnect = () => {
expect.pass();
};

entity.emit("disconnect");
});
Expand Down
16 changes: 6 additions & 10 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test("client", async () => {
expect(address.bare().toString()).toBe(JID);
});

test("bad credentials", (done) => {
test("bad credentials", async () => {
expect.assertions(6);

xmpp = client({
Expand All @@ -62,22 +62,18 @@ test("bad credentials", (done) => {
expect().pass();
});

xmpp.on("online", () => done.fail());
xmpp.on("online", () => {
expect().fail();
});

xmpp.on("error", (err) => {
expect(err instanceof Error).toBe(true);
expect(err).toBeInstanceOf(Error);
expect(err.name).toBe("SASLError");
expect(err.condition).toBe("not-authorized");
error = err;
});

xmpp
.start()
.then(() => done.fail())
.catch((err) => {
expect(err).toBe(error);
done();
});
await expect(xmpp.start()).rejects.toThrow(error);
});

test("reconnects when server restarts gracefully", (done) => {
Expand Down

0 comments on commit af84136

Please sign in to comment.