Skip to content

Commit 2b21104

Browse files
fix error
1 parent 7f63227 commit 2b21104

File tree

1 file changed

+53
-86
lines changed

1 file changed

+53
-86
lines changed

test/integration/node-specific/bson-options/utf8_validation.test.ts

Lines changed: 53 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
type Collection,
99
type MongoClient,
1010
MongoDBResponse,
11-
MongoError,
1211
MongoServerError,
1312
OpMsgResponse
1413
} from '../../../mongodb';
@@ -240,127 +239,95 @@ describe('utf8 validation with cursors', function () {
240239
});
241240
});
242241

243-
async function expectReject(fn: () => Promise<void>, options?: { regex?: RegExp; errorClass }) {
244-
const regex = options?.regex ?? /.*/;
245-
const errorClass = options?.errorClass ?? MongoError;
242+
async function expectReject(fn: () => Promise<void>) {
246243
try {
247244
await fn();
248245
expect.fail('expected the provided callback function to reject, but it did not.');
249246
} catch (error) {
250-
expect(error).to.match(regex);
251-
expect(error).to.be.instanceOf(errorClass);
247+
expect(error).to.match(/Invalid UTF-8 string in BSON document/);
248+
expect(error).to.be.instanceOf(BSONError);
252249
}
253250
}
254251

255252
context('when utf-8 validation is explicitly enabled', function () {
256253
it('a for-await loop throw a BSON error', async function () {
257-
await expectReject(
258-
async () => {
259-
for await (const _doc of collection.find({}, { enableUtf8Validation: true }));
260-
},
261-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
262-
);
254+
await expectReject(async () => {
255+
for await (const _doc of collection.find({}, { enableUtf8Validation: true }));
256+
});
263257
});
264258
it('next() throws a BSON error', async function () {
265-
await expectReject(
266-
async () => {
267-
const cursor = collection.find({}, { enableUtf8Validation: true });
268-
269-
while (await cursor.hasNext()) {
270-
await cursor.next();
271-
}
272-
},
273-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
274-
);
259+
await expectReject(async () => {
260+
const cursor = collection.find({}, { enableUtf8Validation: true });
261+
262+
while (await cursor.hasNext()) {
263+
await cursor.next();
264+
}
265+
});
275266
});
276267

277268
it('toArray() throws a BSON error', async function () {
278-
await expectReject(
279-
async () => {
280-
const cursor = collection.find({}, { enableUtf8Validation: true });
281-
await cursor.toArray();
282-
},
283-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
284-
);
269+
await expectReject(async () => {
270+
const cursor = collection.find({}, { enableUtf8Validation: true });
271+
await cursor.toArray();
272+
});
285273
});
286274

287275
it('.stream() throws a BSONError', async function () {
288-
await expectReject(
289-
async () => {
290-
const cursor = collection.find({}, { enableUtf8Validation: true });
291-
await cursor.stream().toArray();
292-
},
293-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
294-
);
276+
await expectReject(async () => {
277+
const cursor = collection.find({}, { enableUtf8Validation: true });
278+
await cursor.stream().toArray();
279+
});
295280
});
296281

297282
it('tryNext() throws a BSONError', async function () {
298-
await expectReject(
299-
async () => {
300-
const cursor = collection.find({}, { enableUtf8Validation: true });
301-
302-
while (await cursor.hasNext()) {
303-
await cursor.tryNext();
304-
}
305-
},
306-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
307-
);
283+
await expectReject(async () => {
284+
const cursor = collection.find({}, { enableUtf8Validation: true });
285+
286+
while (await cursor.hasNext()) {
287+
await cursor.tryNext();
288+
}
289+
});
308290
});
309291
});
310292

311293
context('utf-8 validation defaults to enabled', function () {
312294
it('a for-await loop throw a BSON error', async function () {
313-
await expectReject(
314-
async () => {
315-
for await (const _doc of collection.find({}));
316-
},
317-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
318-
);
295+
await expectReject(async () => {
296+
for await (const _doc of collection.find({}));
297+
});
319298
});
320299
it('next() throws a BSON error', async function () {
321-
await expectReject(
322-
async () => {
323-
const cursor = collection.find({});
324-
325-
while (await cursor.hasNext()) {
326-
await cursor.next();
327-
}
328-
},
329-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
330-
);
300+
await expectReject(async () => {
301+
const cursor = collection.find({});
302+
303+
while (await cursor.hasNext()) {
304+
await cursor.next();
305+
}
306+
});
331307
});
332308

333309
it('toArray() throws a BSON error', async function () {
334-
await expectReject(
335-
async () => {
336-
const cursor = collection.find({});
337-
await cursor.toArray();
338-
},
339-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
340-
);
310+
await expectReject(async () => {
311+
const cursor = collection.find({});
312+
await cursor.toArray();
313+
});
341314
});
342315

343316
it('.stream() throws a BSONError', async function () {
344-
await expectReject(
345-
async () => {
346-
const cursor = collection.find({});
347-
await cursor.stream().toArray();
348-
},
349-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
350-
);
317+
await expectReject(async () => {
318+
const cursor = collection.find({});
319+
await cursor.stream().toArray();
320+
});
351321
});
352322

353323
it('tryNext() throws a BSONError', async function () {
354-
await expectReject(
355-
async () => {
356-
const cursor = collection.find({}, { enableUtf8Validation: true });
357-
358-
while (await cursor.hasNext()) {
359-
await cursor.tryNext();
360-
}
361-
},
362-
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
363-
);
324+
await expectReject(async () => {
325+
const cursor = collection.find({}, { enableUtf8Validation: true });
326+
327+
while (await cursor.hasNext()) {
328+
await cursor.tryNext();
329+
}
330+
});
364331
});
365332
});
366333
});

0 commit comments

Comments
 (0)