Skip to content

Commit c67325b

Browse files
Add matrix-org/jest linting (#2973)
1 parent a063ae8 commit c67325b

33 files changed

+418
-520
lines changed

.eslintrc.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
plugins: ["matrix-org", "import", "jsdoc"],
3-
extends: ["plugin:matrix-org/babel", "plugin:import/typescript"],
3+
extends: ["plugin:matrix-org/babel", "plugin:matrix-org/jest", "plugin:import/typescript"],
44
parserOptions: {
55
project: ["./tsconfig.json"],
66
},
@@ -63,6 +63,23 @@ module.exports = {
6363
],
6464
},
6565
],
66+
// Disabled tests are a reality for now but as soon as all of the xits are
67+
// eliminated, we should enforce this.
68+
"jest/no-disabled-tests": "off",
69+
// TODO: There are many tests with invalid expects that should be fixed,
70+
// https://github.com/matrix-org/matrix-js-sdk/issues/2976
71+
"jest/valid-expect": "off",
72+
// TODO: There are many cases to refactor away,
73+
// https://github.com/matrix-org/matrix-js-sdk/issues/2978
74+
"jest/no-conditional-expect": "off",
75+
// Also treat "oldBackendOnly" as a test function.
76+
// Used in some crypto tests.
77+
"jest/no-standalone-expect": [
78+
"error",
79+
{
80+
additionalTestBlockFunctions: ["beforeAll", "beforeEach", "oldBackendOnly"],
81+
},
82+
],
6683
},
6784
overrides: [
6885
{

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@
103103
"eslint-config-prettier": "^8.5.0",
104104
"eslint-import-resolver-typescript": "^3.5.1",
105105
"eslint-plugin-import": "^2.26.0",
106+
"eslint-plugin-jest": "^27.1.6",
106107
"eslint-plugin-jsdoc": "^39.6.4",
107-
"eslint-plugin-matrix-org": "^0.10.0",
108+
"eslint-plugin-matrix-org": "^1.0.0",
108109
"eslint-plugin-tsdoc": "^0.2.17",
109110
"eslint-plugin-unicorn": "^45.0.0",
110111
"exorcist": "^2.0.0",

spec/TestClient.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
1818

19+
// `expect` is allowed in helper functions which are called within `test`/`it` blocks
20+
/* eslint-disable jest/no-standalone-expect */
21+
1922
// load olm before the sdk if possible
2023
import "./olm-loader";
2124

spec/integ/crypto.spec.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,8 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("megolm (%s)", (backend: string,
743743
describe("get|setGlobalErrorOnUnknownDevices", () => {
744744
it("should raise an error if crypto is disabled", () => {
745745
aliceTestClient.client["cryptoBackend"] = undefined;
746-
expect(() => aliceTestClient.client.setGlobalErrorOnUnknownDevices(true)).toThrowError(
747-
"encryption disabled",
748-
);
749-
expect(() => aliceTestClient.client.getGlobalErrorOnUnknownDevices()).toThrowError("encryption disabled");
746+
expect(() => aliceTestClient.client.setGlobalErrorOnUnknownDevices(true)).toThrow("encryption disabled");
747+
expect(() => aliceTestClient.client.getGlobalErrorOnUnknownDevices()).toThrow("encryption disabled");
750748
});
751749

752750
oldBackendOnly("should permit sending to unknown devices", async () => {
@@ -799,12 +797,10 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("megolm (%s)", (backend: string,
799797
describe("get|setGlobalBlacklistUnverifiedDevices", () => {
800798
it("should raise an error if crypto is disabled", () => {
801799
aliceTestClient.client["cryptoBackend"] = undefined;
802-
expect(() => aliceTestClient.client.setGlobalBlacklistUnverifiedDevices(true)).toThrowError(
803-
"encryption disabled",
804-
);
805-
expect(() => aliceTestClient.client.getGlobalBlacklistUnverifiedDevices()).toThrowError(
800+
expect(() => aliceTestClient.client.setGlobalBlacklistUnverifiedDevices(true)).toThrow(
806801
"encryption disabled",
807802
);
803+
expect(() => aliceTestClient.client.getGlobalBlacklistUnverifiedDevices()).toThrow("encryption disabled");
808804
});
809805

810806
oldBackendOnly("should disable sending to unverified devices", async () => {

spec/integ/matrix-client-event-emitter.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ describe("MatrixClient events", function () {
175175
});
176176
});
177177

178-
it("should emit User events", function (done) {
178+
it("should emit User events", async () => {
179179
httpBackend!.when("GET", "/sync").respond(200, SYNC_DATA);
180180
httpBackend!.when("GET", "/sync").respond(200, NEXT_SYNC_DATA);
181181
let fired = false;
@@ -192,10 +192,8 @@ describe("MatrixClient events", function () {
192192
});
193193
client!.startClient();
194194

195-
httpBackend!.flushAllExpected().then(function () {
196-
expect(fired).toBe(true);
197-
done();
198-
});
195+
await httpBackend!.flushAllExpected();
196+
expect(fired).toBe(true);
199197
});
200198

201199
it("should emit Room events", function () {

spec/integ/matrix-client-methods.spec.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,17 @@ describe("MatrixClient", function () {
205205
describe("getFilter", function () {
206206
const filterId = "f1lt3r1d";
207207

208-
it("should return a filter from the store if allowCached", function (done) {
208+
it("should return a filter from the store if allowCached", async () => {
209209
const filter = Filter.fromJson(userId, filterId, {
210210
event_format: "client",
211211
});
212212
store!.storeFilter(filter);
213-
client!.getFilter(userId, filterId, true).then(function (gotFilter) {
214-
expect(gotFilter).toEqual(filter);
215-
done();
216-
});
213+
const gotFilter = await client!.getFilter(userId, filterId, true);
214+
expect(gotFilter).toEqual(filter);
217215
httpBackend!.verifyNoOutstandingRequests();
218216
});
219217

220-
it("should do an HTTP request if !allowCached even if one exists", function (done) {
218+
it("should do an HTTP request if !allowCached even if one exists", async () => {
221219
const httpFilterDefinition = {
222220
event_format: "federation",
223221
};
@@ -230,15 +228,11 @@ describe("MatrixClient", function () {
230228
event_format: "client",
231229
});
232230
store!.storeFilter(storeFilter);
233-
client!.getFilter(userId, filterId, false).then(function (gotFilter) {
234-
expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition);
235-
done();
236-
});
237-
238-
httpBackend!.flush("");
231+
const [gotFilter] = await Promise.all([client!.getFilter(userId, filterId, false), httpBackend!.flush("")]);
232+
expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition);
239233
});
240234

241-
it("should do an HTTP request if nothing is in the cache and then store it", function (done) {
235+
it("should do an HTTP request if nothing is in the cache and then store it", async () => {
242236
const httpFilterDefinition = {
243237
event_format: "federation",
244238
};
@@ -247,20 +241,16 @@ describe("MatrixClient", function () {
247241
httpBackend!
248242
.when("GET", "/user/" + encodeURIComponent(userId) + "/filter/" + filterId)
249243
.respond(200, httpFilterDefinition);
250-
client!.getFilter(userId, filterId, true).then(function (gotFilter) {
251-
expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition);
252-
expect(store!.getFilter(userId, filterId)).toBeTruthy();
253-
done();
254-
});
255-
256-
httpBackend!.flush("");
244+
const [gotFilter] = await Promise.all([client!.getFilter(userId, filterId, true), httpBackend!.flush("")]);
245+
expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition);
246+
expect(store!.getFilter(userId, filterId)).toBeTruthy();
257247
});
258248
});
259249

260250
describe("createFilter", function () {
261251
const filterId = "f1llllllerid";
262252

263-
it("should do an HTTP request and then store the filter", function (done) {
253+
it("should do an HTTP request and then store the filter", async () => {
264254
expect(store!.getFilter(userId, filterId)).toBe(null);
265255

266256
const filterDefinition = {
@@ -276,13 +266,9 @@ describe("MatrixClient", function () {
276266
filter_id: filterId,
277267
});
278268

279-
client!.createFilter(filterDefinition).then(function (gotFilter) {
280-
expect(gotFilter.getDefinition()).toEqual(filterDefinition);
281-
expect(store!.getFilter(userId, filterId)).toEqual(gotFilter);
282-
done();
283-
});
284-
285-
httpBackend!.flush("");
269+
const [gotFilter] = await Promise.all([client!.createFilter(filterDefinition), httpBackend!.flush("")]);
270+
expect(gotFilter.getDefinition()).toEqual(filterDefinition);
271+
expect(store!.getFilter(userId, filterId)).toEqual(gotFilter);
286272
});
287273
});
288274

spec/integ/matrix-client-opts.spec.ts

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ describe("MatrixClient opts", function () {
9494
client.stopClient();
9595
});
9696

97-
it("should be able to send messages", function (done) {
97+
it("should be able to send messages", async () => {
9898
const eventId = "$flibble:wibble";
9999
httpBackend.when("PUT", "/txn1").respond(200, {
100100
event_id: eventId,
101101
});
102-
client.sendTextMessage("!foo:bar", "a body", "txn1").then(function (res) {
103-
expect(res.event_id).toEqual(eventId);
104-
done();
105-
});
106-
httpBackend.flush("/txn1", 1);
102+
const [res] = await Promise.all([
103+
client.sendTextMessage("!foo:bar", "a body", "txn1"),
104+
httpBackend.flush("/txn1", 1),
105+
]);
106+
expect(res.event_id).toEqual(eventId);
107107
});
108108

109109
it("should be able to sync / get new events", async function () {
@@ -149,27 +149,25 @@ describe("MatrixClient opts", function () {
149149
client.stopClient();
150150
});
151151

152-
it("shouldn't retry sending events", function (done) {
152+
it("shouldn't retry sending events", async () => {
153153
httpBackend.when("PUT", "/txn1").respond(
154154
500,
155155
new MatrixError({
156156
errcode: "M_SOMETHING",
157157
error: "Ruh roh",
158158
}),
159159
);
160-
client.sendTextMessage("!foo:bar", "a body", "txn1").then(
161-
function (res) {
162-
expect(false).toBe(true);
163-
},
164-
function (err) {
165-
expect(err.errcode).toEqual("M_SOMETHING");
166-
done();
167-
},
168-
);
169-
httpBackend.flush("/txn1", 1);
160+
try {
161+
await Promise.all([
162+
expect(client.sendTextMessage("!foo:bar", "a body", "txn1")).rejects.toThrow(),
163+
httpBackend.flush("/txn1", 1),
164+
]);
165+
} catch (err) {
166+
expect((<MatrixError>err).errcode).toEqual("M_SOMETHING");
167+
}
170168
});
171169

172-
it("shouldn't queue events", function (done) {
170+
it("shouldn't queue events", async () => {
173171
httpBackend.when("PUT", "/txn1").respond(200, {
174172
event_id: "AAA",
175173
});
@@ -178,30 +176,38 @@ describe("MatrixClient opts", function () {
178176
});
179177
let sentA = false;
180178
let sentB = false;
181-
client.sendTextMessage("!foo:bar", "a body", "txn1").then(function (res) {
179+
const messageASendPromise = client.sendTextMessage("!foo:bar", "a body", "txn1").then(function (res) {
182180
sentA = true;
181+
// We expect messageB to be sent before messageA to ensure as we're
182+
// testing that there is no queueing that blocks each other
183183
expect(sentB).toBe(true);
184184
});
185-
client.sendTextMessage("!foo:bar", "b body", "txn2").then(function (res) {
185+
const messageBSendPromise = client.sendTextMessage("!foo:bar", "b body", "txn2").then(function (res) {
186186
sentB = true;
187+
// We expect messageB to be sent before messageA to ensure as we're
188+
// testing that there is no queueing that blocks each other
187189
expect(sentA).toBe(false);
188190
});
189-
httpBackend.flush("/txn2", 1).then(function () {
190-
httpBackend.flush("/txn1", 1).then(function () {
191-
done();
192-
});
193-
});
191+
// Allow messageB to succeed first
192+
await httpBackend.flush("/txn2", 1);
193+
// Then allow messageA to succeed
194+
await httpBackend.flush("/txn1", 1);
195+
196+
// Now await the message send promises to
197+
await messageBSendPromise;
198+
await messageASendPromise;
194199
});
195200

196-
it("should be able to send messages", function (done) {
201+
it("should be able to send messages", async () => {
197202
httpBackend.when("PUT", "/txn1").respond(200, {
198203
event_id: "foo",
199204
});
200-
client.sendTextMessage("!foo:bar", "a body", "txn1").then(function (res) {
201-
expect(res.event_id).toEqual("foo");
202-
done();
203-
});
204-
httpBackend.flush("/txn1", 1);
205+
const [res] = await Promise.all([
206+
client.sendTextMessage("!foo:bar", "a body", "txn1"),
207+
httpBackend.flush("/txn1", 1),
208+
]);
209+
210+
expect(res.event_id).toEqual("foo");
205211
});
206212
});
207213
});

spec/integ/matrix-client-retrying.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ describe("MatrixClient retrying", function () {
4848
return httpBackend!.stop();
4949
});
5050

51-
xit("should retry according to MatrixScheduler.retryFn", function () {});
51+
it.skip("should retry according to MatrixScheduler.retryFn", function () {});
5252

53-
xit("should queue according to MatrixScheduler.queueFn", function () {});
53+
it.skip("should queue according to MatrixScheduler.queueFn", function () {});
5454

55-
xit("should mark events as EventStatus.NOT_SENT when giving up", function () {});
55+
it.skip("should mark events as EventStatus.NOT_SENT when giving up", function () {});
5656

57-
xit("should mark events as EventStatus.QUEUED when queued", function () {});
57+
it.skip("should mark events as EventStatus.QUEUED when queued", function () {});
5858

5959
it("should mark events as EventStatus.CANCELLED when cancelled", function () {
6060
// send a couple of events; the second will be queued
@@ -130,7 +130,7 @@ describe("MatrixClient retrying", function () {
130130
});
131131

132132
describe("resending", function () {
133-
xit("should be able to resend a NOT_SENT event", function () {});
134-
xit("should be able to resend a sent event", function () {});
133+
it.skip("should be able to resend a NOT_SENT event", function () {});
134+
it.skip("should be able to resend a sent event", function () {});
135135
});
136136
});

0 commit comments

Comments
 (0)