From 51aa60f50aed4ee1c72de535edf13ada23dd7d0b Mon Sep 17 00:00:00 2001 From: inyourtime Date: Tue, 26 Nov 2024 13:34:06 +0700 Subject: [PATCH 1/2] chore: migrate from tap to node:test and c8 --- .taprc | 2 - package.json | 4 +- test/index.test.js | 408 ++++++++++++++++++++++----------------------- 3 files changed, 203 insertions(+), 211 deletions(-) delete mode 100644 .taprc diff --git a/.taprc b/.taprc deleted file mode 100644 index 6a6271b..0000000 --- a/.taprc +++ /dev/null @@ -1,2 +0,0 @@ -files: - - 'test/**/*.test.js' diff --git a/package.json b/package.json index 16b2dfc..1ba85bf 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "mongo": "docker run --rm -d -p 27017:27017 mongo:5.0.0", "test": "npm run test:unit && npm run test:typescript", "test:typescript": "tsd", - "test:unit": "tap" + "test:unit": "c8 --100 node --test" }, "repository": { "type": "git", @@ -34,9 +34,9 @@ "devDependencies": { "@fastify/pre-commit": "^2.1.0", "@types/node": "^22.0.0", + "c8": "^10.1.2", "fastify": "^5.0.0", "standard": "^17.1.0", - "tap": "^18.7.1", "tsd": "^0.31.0" }, "dependencies": { diff --git a/test/index.test.js b/test/index.test.js index ec730b3..e25c64a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,13 +1,11 @@ 'use strict' -const t = require('tap') -const { test } = t +const { test } = require('node:test') const Fastify = require('fastify') const fastifyMongo = require('..') const { ObjectId } = require('..') const mongodb = require('mongodb') -const assert = require('node:assert') const NO_DATABASE_MONGODB_URL = 'mongodb://127.0.0.1' const DATABASE_NAME = 'test' @@ -21,55 +19,51 @@ function objectIdTest (t, ObjectId, message) { message = message || 'expect ObjectId value' const obj1 = new ObjectId() - t.ok(obj1) + t.assert.ok(obj1) const obj2 = new ObjectId(obj1) - t.ok(obj2) - t.ok(obj1.equals(obj2)) + t.assert.ok(obj2) + t.assert.ok(obj1.equals(obj2)) const obj3 = new ObjectId() - t.ok(!obj1.equals(obj3)) + t.assert.ok(!obj1.equals(obj3)) } -function clientTest (t, client, message) { +async function clientTest (t, client, message) { t.plan(1) message = message || 'expect client' const db = client.db(DATABASE_NAME) const col = db.collection(COLLECTION_NAME) - t.resolves(async () => { - const r = await col.insertMany([{ a: 1 }]) - assert.strictEqual(1, r.insertedCount) - }, message) + const r = await col.insertMany([{ a: 1 }]) + t.assert.strictEqual(1, r.insertedCount) } -function databaseTest (t, db, message) { +async function databaseTest (t, db, message) { t.plan(1) message = message || 'expect database' const col = db.collection(COLLECTION_NAME) - t.resolves(async () => { - const r = await col.insertMany([{ a: 1 }]) - assert.deepEqual(1, r.insertedCount) - }, message) + const r = await col.insertMany([{ a: 1 }]) + t.assert.strictEqual(1, r.insertedCount) } test('re-export ObjectId', async (t) => { t.plan(1) - t.test(async t => objectIdTest(t, fastifyMongo.ObjectId)) + await t.test(async t => objectIdTest(t, fastifyMongo.ObjectId)) }) test('re-export ObjectId destructured', async (t) => { t.plan(1) - t.test(async t => objectIdTest(t, ObjectId)) + await t.test(async t => objectIdTest(t, ObjectId)) }) test('export of mongodb', async (t) => { t.plan(2) - t.same(typeof fastifyMongo.mongodb.ObjectId, 'function') - t.same(fastifyMongo.mongodb.BSONType.array, 4) + t.assert.strictEqual(typeof fastifyMongo.mongodb.ObjectId, 'function') + t.assert.strictEqual(fastifyMongo.mongodb.BSONType.array, 4) }) test('{ url: NO_DATABASE_MONGODB_URL }', async (t) => { @@ -77,246 +71,246 @@ test('{ url: NO_DATABASE_MONGODB_URL }', async (t) => { const fastify = await register(t, { url: NO_DATABASE_MONGODB_URL }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.notOk(fastify.mongo.db) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(!fastify.mongo.db) - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) }) test('{ url: MONGODB_URL }', async (t) => { t.plan(8) const fastify = await register(t, { url: MONGODB_URL }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.ok(fastify.mongo.db) - t.equal(fastify.mongo.db.databaseName, DATABASE_NAME) - - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => databaseTest(t, fastify.mongo.db)) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(fastify.mongo.db) + t.assert.strictEqual(fastify.mongo.db.databaseName, DATABASE_NAME) + + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) }) test('{ url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME }', async (t) => { t.plan(11) const fastify = await register(t, { url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.notOk(fastify.mongo.db) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(!fastify.mongo.db) - t.ok(fastify.mongo[CLIENT_NAME].client) - t.ok(fastify.mongo[CLIENT_NAME].ObjectId) - t.notOk(fastify.mongo[CLIENT_NAME].db) + t.assert.ok(fastify.mongo[CLIENT_NAME].client) + t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId) + t.assert.ok(!fastify.mongo[CLIENT_NAME].db) - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) - t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) + await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) }) test('{ url: MONGODB_URL, name: CLIENT_NAME }', async (t) => { t.plan(15) const fastify = await register(t, { url: MONGODB_URL, name: CLIENT_NAME }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.ok(fastify.mongo.db) - t.equal(fastify.mongo.db.databaseName, DATABASE_NAME) - - t.ok(fastify.mongo[CLIENT_NAME].client) - t.ok(fastify.mongo[CLIENT_NAME].ObjectId) - t.ok(fastify.mongo[CLIENT_NAME].db) - t.equal(fastify.mongo[CLIENT_NAME].db.databaseName, DATABASE_NAME) - - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => databaseTest(t, fastify.mongo.db)) - - t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) - t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) - t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db)) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(fastify.mongo.db) + t.assert.strictEqual(fastify.mongo.db.databaseName, DATABASE_NAME) + + t.assert.ok(fastify.mongo[CLIENT_NAME].client) + t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId) + t.assert.ok(fastify.mongo[CLIENT_NAME].db) + t.assert.strictEqual(fastify.mongo[CLIENT_NAME].db.databaseName, DATABASE_NAME) + + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) + + await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) + await t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db)) }) test('{ url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', async (t) => { t.plan(15) const fastify = await register(t, { url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.ok(fastify.mongo.db) - t.equal(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) - - t.ok(fastify.mongo[CLIENT_NAME].client) - t.ok(fastify.mongo[CLIENT_NAME].ObjectId) - t.ok(fastify.mongo[CLIENT_NAME].db) - t.equal(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME) - - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => databaseTest(t, fastify.mongo.db)) - - t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) - t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) - t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db)) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(fastify.mongo.db) + t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) + + t.assert.ok(fastify.mongo[CLIENT_NAME].client) + t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId) + t.assert.ok(fastify.mongo[CLIENT_NAME].db) + t.assert.strictEqual(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME) + + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) + + await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) + await t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db)) }) test('{ url: MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', async (t) => { t.plan(15) const fastify = await register(t, { url: MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.ok(fastify.mongo.db) - t.equal(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) - - t.ok(fastify.mongo[CLIENT_NAME].client) - t.ok(fastify.mongo[CLIENT_NAME].ObjectId) - t.ok(fastify.mongo[CLIENT_NAME].db) - t.equal(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME) - - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => databaseTest(t, fastify.mongo.db)) - - t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) - t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) - t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db)) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(fastify.mongo.db) + t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) + + t.assert.ok(fastify.mongo[CLIENT_NAME].client) + t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId) + t.assert.ok(fastify.mongo[CLIENT_NAME].db) + t.assert.strictEqual(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME) + + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) + + await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) + await t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db)) }) test('{ url: NO_DATABASE_MONGODB_URL, database: ANOTHER_DATABASE_NAME }', async (t) => { t.plan(8) const fastify = await register(t, { url: NO_DATABASE_MONGODB_URL, database: ANOTHER_DATABASE_NAME }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.ok(fastify.mongo.db) - t.equal(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) - - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => databaseTest(t, fastify.mongo.db)) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(fastify.mongo.db) + t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) + + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) }) test('{ url: MONGODB_URL, database: ANOTHER_DATABASE_NAME }', async (t) => { t.plan(8) const fastify = await register(t, { url: MONGODB_URL, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.ok(fastify.mongo.db) - t.equal(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) - - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => databaseTest(t, fastify.mongo.db)) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(fastify.mongo.db) + t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) + + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) }) test('{ client: client }', async (t) => { t.plan(6) const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL) - t.teardown(client.close.bind(client)) + t.after(() => client.close()) const fastify = await register(t, { client }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.notOk(fastify.mongo.db) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(!fastify.mongo.db) - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) }) test('{ client: client, database: DATABASE_NAME }', async (t) => { t.plan(8) const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL) - t.teardown(client.close.bind(client)) + t.after(() => client.close()) const fastify = await register(t, { client, database: ANOTHER_DATABASE_NAME }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.ok(fastify.mongo.db) - t.equal(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) - - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => databaseTest(t, fastify.mongo.db)) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(fastify.mongo.db) + t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) + + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) }) test('{ client: client, name: CLIENT_NAME }', async (t) => { t.plan(11) const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL) - t.teardown(client.close.bind(client)) + t.after(() => client.close()) const fastify = await register(t, { client, name: CLIENT_NAME }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.notOk(fastify.mongo.db) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(!fastify.mongo.db) - t.ok(fastify.mongo[CLIENT_NAME].client) - t.ok(fastify.mongo[CLIENT_NAME].ObjectId) - t.notOk(fastify.mongo[CLIENT_NAME].db) + t.assert.ok(fastify.mongo[CLIENT_NAME].client) + t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId) + t.assert.ok(!fastify.mongo[CLIENT_NAME].db) - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) - t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) + await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) }) test('{ client: client, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }', async (t) => { t.plan(15) const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL) - t.teardown(client.close.bind(client)) + t.after(() => client.close()) const fastify = await register(t, { client, name: CLIENT_NAME, database: ANOTHER_DATABASE_NAME }) - t.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.ok(fastify.mongo.db) - t.equal(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) - - t.ok(fastify.mongo[CLIENT_NAME].client) - t.ok(fastify.mongo[CLIENT_NAME].ObjectId) - t.ok(fastify.mongo[CLIENT_NAME].db) - t.equal(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME) - - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => databaseTest(t, fastify.mongo.db)) - - t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) - t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) - t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db)) + t.assert.ok(fastify.mongo) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(fastify.mongo.db) + t.assert.strictEqual(fastify.mongo.db.databaseName, ANOTHER_DATABASE_NAME) + + t.assert.ok(fastify.mongo[CLIENT_NAME].client) + t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId) + t.assert.ok(fastify.mongo[CLIENT_NAME].db) + t.assert.strictEqual(fastify.mongo[CLIENT_NAME].db.databaseName, ANOTHER_DATABASE_NAME) + + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) + + await t.test(async t => objectIdTest(t, fastify.mongo[CLIENT_NAME].ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo[CLIENT_NAME].client)) + await t.test(async t => databaseTest(t, fastify.mongo[CLIENT_NAME].db)) }) test('{ client: client } does not set onClose', async (t) => { const client = await mongodb.MongoClient.connect(NO_DATABASE_MONGODB_URL) - t.teardown(client.close.bind(client)) + t.after(() => client.close()) const fastify = Fastify() fastify.register(fastifyMongo, { client, database: DATABASE_NAME }) await fastify.ready() await fastify.close() - t.test(async t => databaseTest(t, fastify.mongo.db)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) }) test('{ }', async (t) => { @@ -324,8 +318,8 @@ test('{ }', async (t) => { try { await register(t, {}) } catch (err) { - t.ok(err) - t.equal(err.message, '`url` parameter is mandatory if no client is provided') + t.assert.ok(err) + t.assert.strictEqual(err.message, '`url` parameter is mandatory if no client is provided') } }) @@ -334,8 +328,8 @@ test('{ url: "unknown://protocol" }', async (t) => { try { await register(t, { url: 'unknown://protocol' }) } catch (err) { - t.ok(err) - t.match(err.message, /expected connection string/) + t.assert.ok(err) + t.assert.match(err.message, /expected connection string/) } }) @@ -343,7 +337,7 @@ test('double register without name', async (t) => { t.plan(2) const fastify = Fastify() - t.teardown(fastify.close.bind(fastify)) + t.after(() => fastify.close()) try { await fastify @@ -351,8 +345,8 @@ test('double register without name', async (t) => { .register(fastifyMongo, { url: MONGODB_URL }) .ready() } catch (err) { - t.ok(err) - t.equal(err.message, 'fastify-mongodb has already registered') + t.assert.ok(err) + t.assert.strictEqual(err.message, 'fastify-mongodb has already registered') } }) @@ -360,48 +354,48 @@ test('double register with different name', async (t) => { t.plan(22) const fastify = Fastify() - t.teardown(fastify.close.bind(fastify)) + t.after(() => fastify.close()) await fastify .register(fastifyMongo, { url: MONGODB_URL, name: 'client1' }) .register(fastifyMongo, { url: NO_DATABASE_MONGODB_URL, name: 'client2', database: ANOTHER_DATABASE_NAME }) .ready() - t.ok(fastify.mongo) + t.assert.ok(fastify.mongo) - t.ok(fastify.mongo.client) - t.ok(fastify.mongo.ObjectId) - t.ok(fastify.mongo.db) - t.equal(fastify.mongo.db.databaseName, DATABASE_NAME) + t.assert.ok(fastify.mongo.client) + t.assert.ok(fastify.mongo.ObjectId) + t.assert.ok(fastify.mongo.db) + t.assert.strictEqual(fastify.mongo.db.databaseName, DATABASE_NAME) - t.ok(fastify.mongo.client1.client) - t.ok(fastify.mongo.client1.ObjectId) - t.ok(fastify.mongo.client1.db) - t.equal(fastify.mongo.client1.db.databaseName, DATABASE_NAME) + t.assert.ok(fastify.mongo.client1.client) + t.assert.ok(fastify.mongo.client1.ObjectId) + t.assert.ok(fastify.mongo.client1.db) + t.assert.strictEqual(fastify.mongo.client1.db.databaseName, DATABASE_NAME) - t.ok(fastify.mongo.client2.client) - t.ok(fastify.mongo.client2.ObjectId) - t.ok(fastify.mongo.client2.db) - t.equal(fastify.mongo.client2.db.databaseName, ANOTHER_DATABASE_NAME) + t.assert.ok(fastify.mongo.client2.client) + t.assert.ok(fastify.mongo.client2.ObjectId) + t.assert.ok(fastify.mongo.client2.db) + t.assert.strictEqual(fastify.mongo.client2.db.databaseName, ANOTHER_DATABASE_NAME) - t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client)) - t.test(async t => databaseTest(t, fastify.mongo.db)) + await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client)) + await t.test(async t => databaseTest(t, fastify.mongo.db)) - t.test(async t => objectIdTest(t, fastify.mongo.client1.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client1.client)) - t.test(async t => databaseTest(t, fastify.mongo.client1.db)) + await t.test(async t => objectIdTest(t, fastify.mongo.client1.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client1.client)) + await t.test(async t => databaseTest(t, fastify.mongo.client1.db)) - t.test(async t => objectIdTest(t, fastify.mongo.client2.ObjectId)) - t.test(async t => clientTest(t, fastify.mongo.client2.client)) - t.test(async t => databaseTest(t, fastify.mongo.client2.db)) + await t.test(async t => objectIdTest(t, fastify.mongo.client2.ObjectId)) + await t.test(async t => clientTest(t, fastify.mongo.client2.client)) + await t.test(async t => databaseTest(t, fastify.mongo.client2.db)) }) test('double register with the same name', async (t) => { t.plan(2) const fastify = Fastify() - t.teardown(fastify.close.bind(fastify)) + t.after(() => fastify.close()) try { await fastify @@ -409,8 +403,8 @@ test('double register with the same name', async (t) => { .register(fastifyMongo, { url: MONGODB_URL, name: CLIENT_NAME }) .ready() } catch (err) { - t.ok(err) - t.equal(err.message, 'Connection name already registered: ' + CLIENT_NAME) + t.assert.ok(err) + t.assert.strictEqual(err.message, 'Connection name already registered: ' + CLIENT_NAME) } }) @@ -419,7 +413,7 @@ test('Immutable options', async (t) => { const given = { url: MONGODB_URL, name: CLIENT_NAME, database: DATABASE_NAME } await register(t, given) - t.same(given, { + t.assert.deepStrictEqual(given, { url: MONGODB_URL, name: CLIENT_NAME, database: DATABASE_NAME @@ -430,21 +424,21 @@ test('timeout', async (t) => { t.plan(2) const fastify = Fastify() - t.teardown(fastify.close.bind(fastify)) + t.after(() => fastify.close()) try { await fastify .register(fastifyMongo, { url: 'mongodb://127.0.0.1:9999' }) .ready() } catch (err) { - t.ok(err) - t.equal(err.message, 'connect ECONNREFUSED 127.0.0.1:9999') + t.assert.ok(err) + t.assert.strictEqual(err.message, 'connect ECONNREFUSED 127.0.0.1:9999') } }) async function register (t, options) { const fastify = Fastify() - t.teardown(fastify.close.bind(fastify)) + t.after(() => fastify.close()) fastify.register(fastifyMongo, options) From 9ffbe8fe56cbbaf9262da073980f32eb46c8fd1a Mon Sep 17 00:00:00 2001 From: inyourtime Date: Tue, 26 Nov 2024 22:02:20 +0700 Subject: [PATCH 2/2] chore: use ifError assert undefined --- test/index.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index e25c64a..89b7df8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -74,7 +74,7 @@ test('{ url: NO_DATABASE_MONGODB_URL }', async (t) => { t.assert.ok(fastify.mongo) t.assert.ok(fastify.mongo.client) t.assert.ok(fastify.mongo.ObjectId) - t.assert.ok(!fastify.mongo.db) + t.assert.ifError(fastify.mongo.db) await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) await t.test(async t => clientTest(t, fastify.mongo.client)) @@ -102,11 +102,11 @@ test('{ url: NO_DATABASE_MONGODB_URL, name: CLIENT_NAME }', async (t) => { t.assert.ok(fastify.mongo) t.assert.ok(fastify.mongo.client) t.assert.ok(fastify.mongo.ObjectId) - t.assert.ok(!fastify.mongo.db) + t.assert.ifError(fastify.mongo.db) t.assert.ok(fastify.mongo[CLIENT_NAME].client) t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId) - t.assert.ok(!fastify.mongo[CLIENT_NAME].db) + t.assert.ifError(fastify.mongo[CLIENT_NAME].db) await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) await t.test(async t => clientTest(t, fastify.mongo.client)) @@ -227,7 +227,7 @@ test('{ client: client }', async (t) => { t.assert.ok(fastify.mongo) t.assert.ok(fastify.mongo.client) t.assert.ok(fastify.mongo.ObjectId) - t.assert.ok(!fastify.mongo.db) + t.assert.ifError(fastify.mongo.db) await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) await t.test(async t => clientTest(t, fastify.mongo.client)) @@ -261,11 +261,11 @@ test('{ client: client, name: CLIENT_NAME }', async (t) => { t.assert.ok(fastify.mongo) t.assert.ok(fastify.mongo.client) t.assert.ok(fastify.mongo.ObjectId) - t.assert.ok(!fastify.mongo.db) + t.assert.ifError(fastify.mongo.db) t.assert.ok(fastify.mongo[CLIENT_NAME].client) t.assert.ok(fastify.mongo[CLIENT_NAME].ObjectId) - t.assert.ok(!fastify.mongo[CLIENT_NAME].db) + t.assert.ifError(fastify.mongo[CLIENT_NAME].db) await t.test(async t => objectIdTest(t, fastify.mongo.ObjectId)) await t.test(async t => clientTest(t, fastify.mongo.client))