From e7830647f5c84ad1120c2a9dbf51d915ec95037a Mon Sep 17 00:00:00 2001 From: _sss Date: Tue, 26 Nov 2024 21:39:16 +0700 Subject: [PATCH] chore: migrate from tap to node:test and c8 (#153) --- .taprc | 3 - package.json | 4 +- test/closing.test.js | 30 +++--- test/connection.test.js | 179 +++++++++++++++++------------------- test/initialization.test.js | 88 +++++++++--------- test/pool.promise.test.js | 58 ++++++------ test/pool.test.js | 139 +++++++++++++++------------- 7 files changed, 248 insertions(+), 253 deletions(-) delete mode 100644 .taprc diff --git a/.taprc b/.taprc deleted file mode 100644 index 343ddd5..0000000 --- a/.taprc +++ /dev/null @@ -1,3 +0,0 @@ -disable-coverage: true -files: - - test/**/*.test.js diff --git a/package.json b/package.json index 6db7bf8..4f4cea5 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "mysql:5.5": "docker run -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes --rm mysql:5.5", "mysql:8.0": "docker run -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes --rm mysql:8.0", "test": "npm run test:unit && npm run test:typescript", - "test:unit": "tap", + "test:unit": "c8 --100 node --test", "test:typescript": "tsd" }, "repository": { @@ -38,9 +38,9 @@ }, "devDependencies": { "@types/node": "^22.0.0", + "c8": "^10.1.2", "fastify": "^5.0.0", "standard": "^17.1.0", - "tap": "^19.2.5", "tsd": "^0.31.1" }, "publishConfig": { diff --git a/test/closing.test.js b/test/closing.test.js index d4ccd5a..8588ae6 100644 --- a/test/closing.test.js +++ b/test/closing.test.js @@ -1,10 +1,10 @@ 'use strict' -const test = require('tap').test +const { test } = require('node:test') const Fastify = require('fastify') const fastifyMysql = require('../index') -test('callback connection', (t) => { +test('callback connection', (t, done) => { let fastify = null fastify = Fastify() @@ -12,23 +12,20 @@ test('callback connection', (t) => { type: 'connection', connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.query('SELECT 1 AS `ping`', (err, results) => { - t.error(err) - t.ok(results[0].ping === 1) - - fastify.close((closeErr) => { - t.error(closeErr) - t.end() - }) + t.assert.ifError(err) + t.assert.ok(results[0].ping === 1) + done() }) }) }) -test('promise connection', (t) => { +test('promise connection', (t, done) => { let fastify = null fastify = Fastify() @@ -37,17 +34,14 @@ test('promise connection', (t) => { type: 'connection', connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.query('SELECT 1 AS `ping`').then(([results]) => { - t.ok(results[0].ping === 1) - - fastify.close((closeErr) => { - t.error(closeErr) - t.end() - }) + t.assert.ok(results[0].ping === 1) + done() }) }) }) diff --git a/test/connection.test.js b/test/connection.test.js index 32bf361..0333ae0 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -1,11 +1,11 @@ 'use strict' -const test = require('tap').test +const { test } = require('node:test') const Fastify = require('fastify') const fastifyMysql = require('../index') const { isMySQLPool, isMySQLPromisePool, isMySQLConnection, isMySQLPromiseConnection } = fastifyMysql -test('fastify.mysql namespace should exist', (t) => { +test('fastify.mysql namespace should exist', (t, done) => { const fastify = Fastify() fastify.register(fastifyMysql, { type: 'connection', @@ -14,25 +14,25 @@ test('fastify.mysql namespace should exist', (t) => { user: 'root', database: 'mysql' }) + t.after(() => fastify.close()) fastify.ready((err) => { - t.error(err) - t.ok(fastify.mysql) - t.ok(fastify.mysql.test) - t.ok(fastify.mysql.test.connection) - t.ok(fastify.mysql.test.query) - t.ok(fastify.mysql.test.execute) + t.assert.ifError(err) + t.assert.ok(fastify.mysql) + t.assert.ok(fastify.mysql.test) + t.assert.ok(fastify.mysql.test.connection) + t.assert.ok(fastify.mysql.test.query) + t.assert.ok(fastify.mysql.test.execute) - t.ok(fastify.mysql.test.format) - t.ok(fastify.mysql.test.escape) - t.ok(fastify.mysql.test.escapeId) + t.assert.ok(fastify.mysql.test.format) + t.assert.ok(fastify.mysql.test.escape) + t.assert.ok(fastify.mysql.test.escapeId) - fastify.close() - t.end() + done() }) }) -test('utils should work', (t) => { +test('utils should work', async (t) => { let fastify = null t.beforeEach(() => { fastify = Fastify() @@ -48,70 +48,64 @@ test('utils should work', (t) => { fastify = null }) - t.test('query util', (t) => { + await t.test('query util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.query('SELECT 1 AS `ping`', (err, results) => { - t.error(err) - t.ok(results[0].ping === 1) - - fastify.close() - t.end() + t.assert.ifError(err) + t.assert.ok(results[0].ping === 1) + done() }) }) }) - t.test('execute util', (t) => { + await t.test('execute util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.execute('SELECT ? as `ping`', [1], (err, results) => { - t.error(err) - t.ok(results[0].ping === 1) - - fastify.close() - t.end() + t.assert.ifError(err) + t.assert.ok(results[0].ping === 1) + done() }) }) }) - t.test('format util', (t) => { + await t.test('format util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) const sqlString = fastify.mysql.format('SELECT ? AS `now`', [1]) - t.equal('SELECT 1 AS `now`', sqlString) - t.end() + t.assert.strictEqual('SELECT 1 AS `now`', sqlString) + done() }) }) - t.test('escape util', (t) => { + await t.test('escape util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) const id = 'userId' const sql = 'SELECT * FROM users WHERE id = ' + fastify.mysql.escape(id) - t.equal(sql, `SELECT * FROM users WHERE id = '${id}'`) - t.end() + t.assert.strictEqual(sql, `SELECT * FROM users WHERE id = '${id}'`) + done() }) }) - t.test('escapeId util', (t) => { + await t.test('escapeId util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) const sorter = 'date' const sql = 'SELECT * FROM posts ORDER BY ' + fastify.mysql.escapeId('posts.' + sorter) - t.ok(sql, 'SELECT * FROM posts ORDER BY `posts`.`date`') - t.end() + t.assert.strictEqual(sql, 'SELECT * FROM posts ORDER BY `posts`.`date`') + done() }) }) - - t.end() }) -test('promise connection', (t) => { +test('promise connection', async (t) => { let fastify = null t.beforeEach(() => { fastify = Fastify() @@ -127,97 +121,92 @@ test('promise connection', (t) => { fastify = null }) - t.test('query util', (t) => { + await t.test('query util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.query('SELECT 1 AS `ping`').then(([results]) => { - t.error(err) - - t.ok(results[0].ping === 1) - t.end() + t.assert.ok(results[0].ping === 1) + done() }) }) }) - t.test('execute util', (t) => { + await t.test('execute util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.execute('SELECT ? AS `ping`', [1]).then(([results]) => { - t.error(err) - - t.ok(results[0].ping === 1) - t.end() + t.assert.ok(results[0].ping === 1) + done() }) }) }) - t.test('format util', (t) => { + await t.test('format util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) const sqlString = fastify.mysql.format('SELECT ? AS `now`', [1]) - t.equal('SELECT 1 AS `now`', sqlString) - t.end() + t.assert.strictEqual('SELECT 1 AS `now`', sqlString) + done() }) }) - t.test('escape util', (t) => { + await t.test('escape util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) const id = 'userId' const sql = 'SELECT * FROM users WHERE id = ' + fastify.mysql.escape(id) - t.equal(sql, `SELECT * FROM users WHERE id = '${id}'`) - t.end() + t.assert.strictEqual(sql, `SELECT * FROM users WHERE id = '${id}'`) + done() }) }) - t.test('escapeId util', (t) => { + await t.test('escapeId util', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) const sorter = 'date' const sql = 'SELECT * FROM posts ORDER BY ' + fastify.mysql.escapeId('posts.' + sorter) - t.ok(sql, 'SELECT * FROM posts ORDER BY `posts`.`date`') - t.end() + t.assert.strictEqual(sql, 'SELECT * FROM posts ORDER BY `posts`.`date`') + done() }) }) - t.test('Should throw when mysql2 fail to perform operation', (t) => { + await t.test('Should throw when mysql2 fail to perform operation', (t, done) => { fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) const sorter = 'date' const sql = 'SELECT * FROM posts ORDER BY ' + fastify.mysql.escapeId('posts.' + sorter) - t.ok(sql, 'SELECT * FROM posts ORDER BY `posts`.`date`') - t.end() + t.assert.strictEqual(sql, 'SELECT * FROM posts ORDER BY `posts`.`date`') + done() }) }) - - t.end() }) -test('isMySQLConnection is true', (t) => { +test('isMySQLConnection is true', (t, done) => { t.plan(5) const fastify = Fastify() fastify.register(fastifyMysql, { type: 'connection', connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) + fastify.ready((err) => { - t.error(err) - t.equal(isMySQLConnection(fastify.mysql), true) - t.equal(isMySQLPool(fastify.mysql), false) - t.equal(isMySQLPromiseConnection(fastify.mysql), false) - t.equal(isMySQLPromisePool(fastify.mysql), false) - t.end() + t.assert.ifError(err) + t.assert.strictEqual(isMySQLConnection(fastify.mysql), true) + t.assert.strictEqual(isMySQLPool(fastify.mysql), false) + t.assert.strictEqual(isMySQLPromiseConnection(fastify.mysql), false) + t.assert.strictEqual(isMySQLPromisePool(fastify.mysql), false) + done() }) - fastify.close() }) -test('isMySQLPromiseConnection is true', (t) => { +test('isMySQLPromiseConnection is true', (t, done) => { t.plan(5) const fastify = Fastify() fastify.register(fastifyMysql, { @@ -225,22 +214,23 @@ test('isMySQLPromiseConnection is true', (t) => { type: 'connection', connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) + fastify.ready((err) => { - t.error(err) - t.equal(isMySQLPromiseConnection(fastify.mysql), true) - t.equal(isMySQLPromisePool(fastify.mysql), false) - t.equal(isMySQLConnection(fastify.mysql), false) - t.equal(isMySQLPool(fastify.mysql), false) - t.end() + t.assert.ifError(err) + t.assert.strictEqual(isMySQLPromiseConnection(fastify.mysql), true) + t.assert.strictEqual(isMySQLPromisePool(fastify.mysql), false) + t.assert.strictEqual(isMySQLConnection(fastify.mysql), false) + t.assert.strictEqual(isMySQLPool(fastify.mysql), false) + done() }) - fastify.close() }) -test('Promise: should throw when mysql2 fail to perform operation', (t) => { +test('Promise: should throw when mysql2 fail to perform operation', (t, done) => { t.plan(3) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) fastify.register(fastifyMysql, { type: 'connection', @@ -252,13 +242,14 @@ test('Promise: should throw when mysql2 fail to perform operation', (t) => { }) fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) const sql = 'SELECT fastify FROM fastify' fastify.mysql.test.connection.query(sql).catch((errors) => { - t.ok(errors) - t.equal(errors.message, "Table 'mysql.fastify' doesn't exist") + t.assert.ok(errors) + t.assert.strictEqual(errors.message, "Table 'mysql.fastify' doesn't exist") + done() }) }) }) diff --git a/test/initialization.test.js b/test/initialization.test.js index 02550e5..2e2161c 100644 --- a/test/initialization.test.js +++ b/test/initialization.test.js @@ -1,15 +1,14 @@ 'use strict' -const t = require('tap') -const test = t.test +const { test } = require('node:test') const Fastify = require('fastify') const fastifyMysql = require('../index') -test('Should not throw if registered within different scopes (with and without named instances)', (t) => { +test('Should not throw if registered within different scopes (with and without named instances)', (t, done) => { t.plan(1) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) fastify.register(function scopeOne (instance, opts, next) { instance.register(fastifyMysql, { @@ -34,15 +33,16 @@ test('Should not throw if registered within different scopes (with and without n }) fastify.ready((errors) => { - t.error(errors) + t.assert.ifError(errors) + done() }) }) -test('Should throw when trying to register multiple instances without giving a name', (t) => { +test('Should throw when trying to register multiple instances without giving a name', (t, done) => { t.plan(2) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) fastify.register(fastifyMysql, { connectionString: 'mysql://root@localhost/mysql' @@ -53,16 +53,17 @@ test('Should throw when trying to register multiple instances without giving a n }) fastify.ready((errors) => { - t.ok(errors) - t.equal(errors.message, 'fastify-mysql has already been registered') + t.assert.ok(errors) + t.assert.strictEqual(errors.message, 'fastify-mysql has already been registered') + done() }) }) -test('Should throw with duplicate connection names', (t) => { +test('Should throw with duplicate connection names', (t, done) => { t.plan(2) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) const name = 'test' fastify @@ -76,42 +77,44 @@ test('Should throw with duplicate connection names', (t) => { }) fastify.ready((errors) => { - t.ok(errors) - t.equal(errors.message, `fastify-mysql '${name}' instance name has already been registered`) + t.assert.ok(errors) + t.assert.strictEqual(errors.message, `fastify-mysql '${name}' instance name has already been registered`) + done() }) }) -test('Should throw when mysql2 fail', (t) => { +test('Should throw when mysql2 fail', (t, done) => { t.plan(2) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) const BAD_PORT = 6000 const HOST = '127.0.0.1' - // We try to access throught a wrong port (MySQL listen on port 3306) + // We try to access through a wrong port (MySQL listen on port 3306) fastify.register(fastifyMysql, { host: HOST, port: BAD_PORT }) fastify.ready((errors) => { - t.ok(errors) - t.equal(errors.message, `connect ECONNREFUSED ${HOST}:${BAD_PORT}`) + t.assert.ok(errors) + t.assert.strictEqual(errors.message, `connect ECONNREFUSED ${HOST}:${BAD_PORT}`) + done() }) }) -test('Promise: Should throw when mysql2 fail', (t) => { +test('Promise: Should throw when mysql2 fail', (t, done) => { t.plan(2) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) const BAD_PORT = 6000 const HOST = '127.0.0.1' - // We try to access throught a wrong port (MySQL listen on port 3306) + // We try to access through a wrong port (MySQL listen on port 3306) fastify.register(fastifyMysql, { host: HOST, port: BAD_PORT, @@ -119,21 +122,22 @@ test('Promise: Should throw when mysql2 fail', (t) => { }) fastify.ready((errors) => { - t.ok(errors) - t.equal(errors.message, `connect ECONNREFUSED ${HOST}:${BAD_PORT}`) + t.assert.ok(errors) + t.assert.strictEqual(errors.message, `connect ECONNREFUSED ${HOST}:${BAD_PORT}`) + done() }) }) -test('Connection - Promise: Should throw when mysql2 fail', (t) => { - t.plan(3) +test('Connection - Promise: Should throw when mysql2 fail', (t, done) => { + t.plan(2) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) const BAD_PORT = 6000 const HOST = '127.0.0.1' - // We try to access throught a wrong port (MySQL listen on port 3306) + // We try to access through a wrong port (MySQL listen on port 3306) fastify.register(fastifyMysql, { host: HOST, port: BAD_PORT, @@ -142,17 +146,17 @@ test('Connection - Promise: Should throw when mysql2 fail', (t) => { }) fastify.ready((errors) => { - t.ok(errors) - t.equal(errors.message, `connect ECONNREFUSED ${HOST}:${BAD_PORT}`) - t.pass() + t.assert.ok(errors) + t.assert.strictEqual(errors.message, `connect ECONNREFUSED ${HOST}:${BAD_PORT}`) + done() }) }) -test('Promise - Should throw when trying to register multiple instances without giving a name', (t) => { - t.plan(3) +test('Promise - Should throw when trying to register multiple instances without giving a name', (t, done) => { + t.plan(2) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) fastify.register(fastifyMysql, { connectionString: 'mysql://root@localhost/mysql' @@ -163,17 +167,17 @@ test('Promise - Should throw when trying to register multiple instances without }) fastify.ready((errors) => { - t.ok(errors) - t.equal(errors.message, 'fastify-mysql has already been registered') - t.pass() + t.assert.ok(errors) + t.assert.strictEqual(errors.message, 'fastify-mysql has already been registered') + done() }) }) -test('Promise - Should throw with duplicate connection names', (t) => { - t.plan(3) +test('Promise - Should throw with duplicate connection names', (t, done) => { + t.plan(2) const fastify = Fastify() - t.teardown(() => fastify.close()) + t.after(() => fastify.close()) const name = 'test' fastify @@ -187,8 +191,8 @@ test('Promise - Should throw with duplicate connection names', (t) => { }) fastify.ready((errors) => { - t.ok(errors) - t.equal(errors.message, `fastify-mysql '${name}' instance name has already been registered`) - t.pass() + t.assert.ok(errors) + t.assert.strictEqual(errors.message, `fastify-mysql '${name}' instance name has already been registered`) + done() }) }) diff --git a/test/pool.promise.test.js b/test/pool.promise.test.js index 982ef7a..b8b2860 100644 --- a/test/pool.promise.test.js +++ b/test/pool.promise.test.js @@ -1,11 +1,11 @@ 'use strict' -const test = require('tap').test +const { test } = require('node:test') const Fastify = require('fastify') const fastifyMysql = require('../index') const { isMySQLPromisePool, isMySQLPromiseConnection, isMySQLPool, isMySQLConnection } = fastifyMysql -test('promise pool', (t) => { +test('promise pool', async (t) => { let fastify t.beforeEach(() => { @@ -23,81 +23,83 @@ test('promise pool', (t) => { fastify.close() }) - t.test('mysql.pool.query', (t) => { + await t.test('mysql.pool.query', (t, done) => { t.plan(3) fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.query('SELECT 1 AS `ping`') .then(([results, fields]) => { - t.ok(results[0].ping === 1) - t.ok(fields) + t.assert.ok(results[0].ping === 1) + t.assert.ok(fields) + done() }) }) }) - t.test('mysql.pool.execute', (t) => { + await t.test('mysql.pool.execute', (t, done) => { t.plan(3) fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.execute('SELECT ? AS `ping`', [1]) .then(([results, fields]) => { - t.ok(results[0].ping === 1) - t.ok(fields) + t.assert.ok(results[0].ping === 1) + t.assert.ok(fields) + done() }) }) }) - t.test('promise pool.getConnection', (t) => { + await t.test('promise pool.getConnection', (t, done) => { t.plan(7) fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.getConnection() .then((connection) => { connection.query('SELECT 2 AS `ping`') .then(([results, fields]) => { - t.ok(results[0].ping === 2) - t.ok(fields) + t.assert.ok(results[0].ping === 2) + t.assert.ok(fields) connection.release() }) }) fastify.mysql.query('SELECT 3 AS `ping`') .then(([results, fields]) => { - t.ok(results[0].ping === 3) - t.ok(fields) + t.assert.ok(results[0].ping === 3) + t.assert.ok(fields) }) fastify.mysql.execute('SELECT ? AS `ping`', [3]) .then(([results, fields]) => { - t.ok(results[0].ping === 3) - t.ok(fields) + t.assert.ok(results[0].ping === 3) + t.assert.ok(fields) + done() }) }) }) - - t.end() }) -test('isMySQLPromisePool is true', (t) => { +test('isMySQLPromisePool is true', (t, done) => { t.plan(5) const fastify = Fastify() fastify.register(fastifyMysql, { promise: true, connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) + fastify.ready((err) => { - t.error(err) - t.equal(isMySQLPromisePool(fastify.mysql), true) - t.equal(isMySQLPromiseConnection(fastify.mysql), false) - t.equal(isMySQLPool(fastify.mysql), false) - t.equal(isMySQLConnection(fastify.mysql), false) - t.end() + t.assert.ifError(err) + t.assert.strictEqual(isMySQLPromisePool(fastify.mysql), true) + t.assert.strictEqual(isMySQLPromiseConnection(fastify.mysql), false) + t.assert.strictEqual(isMySQLPool(fastify.mysql), false) + t.assert.strictEqual(isMySQLConnection(fastify.mysql), false) + done() }) - fastify.close() }) diff --git a/test/pool.test.js b/test/pool.test.js index f41fc8a..6466e09 100644 --- a/test/pool.test.js +++ b/test/pool.test.js @@ -1,12 +1,11 @@ 'use strict' -const t = require('tap') -const test = t.test +const { test } = require('node:test') const Fastify = require('fastify') const fastifyMysql = require('../index') const { isMySQLPool, isMySQLPromisePool, isMySQLConnection, isMySQLPromiseConnection } = fastifyMysql -test('fastify.mysql namespace should exist', t => { +test('fastify.mysql namespace should exist', (t, done) => { t.plan(9) const fastify = Fastify() @@ -14,23 +13,25 @@ test('fastify.mysql namespace should exist', t => { fastify.register(fastifyMysql, { connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) fastify.ready(err => { - t.error(err) - - t.ok(fastify.mysql) - t.ok(fastify.mysql.pool) - t.ok(fastify.mysql.query) - t.ok(fastify.mysql.execute) - t.ok(fastify.mysql.getConnection) - t.ok(fastify.mysql.format) - t.ok(fastify.mysql.escape) - t.ok(fastify.mysql.escapeId) - fastify.close() + t.assert.ifError(err) + + t.assert.ok(fastify.mysql) + t.assert.ok(fastify.mysql.pool) + t.assert.ok(fastify.mysql.query) + t.assert.ok(fastify.mysql.execute) + t.assert.ok(fastify.mysql.getConnection) + t.assert.ok(fastify.mysql.format) + t.assert.ok(fastify.mysql.escape) + t.assert.ok(fastify.mysql.escapeId) + + done() }) }) -test('use query util', t => { +test('use query util', (t, done) => { t.plan(3) const fastify = Fastify() @@ -38,20 +39,21 @@ test('use query util', t => { fastify.register(fastifyMysql, { connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) fastify.ready(err => { - t.error(err) + t.assert.ifError(err) fastify.mysql.query('SELECT NOW()', (err, result) => { - t.error(err) + t.assert.ifError(err) - t.ok(result.length) - fastify.close() + t.assert.ok(result.length) + done() }) }) }) -test('use execute util', t => { +test('use execute util', (t, done) => { t.plan(3) const fastify = Fastify() @@ -59,20 +61,21 @@ test('use execute util', t => { fastify.register(fastifyMysql, { connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) fastify.ready(err => { - t.error(err) + t.assert.ifError(err) fastify.mysql.execute('SELECT NOW()', (err, result) => { - t.error(err) + t.assert.ifError(err) - t.ok(result.length) - fastify.close() + t.assert.ok(result.length) + done() }) }) }) -test('use getConnection util', t => { +test('use getConnection util', (t, done) => { t.plan(7) const fastify = Fastify() @@ -83,32 +86,33 @@ test('use getConnection util', t => { database: 'mysql', connectionLimit: 1 }) + t.after(() => fastify.close()) fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) fastify.mysql.getConnection((err, connection) => { - t.error(err) + t.assert.ifError(err) - t.ok(connection) + t.assert.ok(connection) connection.query('SELECT 1 AS `ping`', (err, results) => { - t.error(err) + t.assert.ifError(err) - t.ok(results[0].ping === 1) + t.assert.ok(results[0].ping === 1) connection.release() }) }) // if not call connection.release(), it will block next query fastify.mysql.query('SELECT NOW()', (err, result) => { - t.error(err) + t.assert.ifError(err) - t.ok(result.length) - fastify.close() + t.assert.ok(result.length) + done() }) }) }) -test('fastify.mysql.test namespace should exist', t => { +test('fastify.mysql.test namespace should exist', (t, done) => { t.plan(9) const fastify = Fastify() @@ -117,23 +121,25 @@ test('fastify.mysql.test namespace should exist', t => { name: 'test', connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) fastify.ready(err => { - t.error(err) - - t.ok(fastify.mysql) - t.ok(fastify.mysql.test) - t.ok(fastify.mysql.test.pool) - t.ok(fastify.mysql.test.execute) - t.ok(fastify.mysql.test.getConnection) - t.ok(fastify.mysql.test.format) - t.ok(fastify.mysql.test.escape) - t.ok(fastify.mysql.test.escapeId) - fastify.close() + t.assert.ifError(err) + + t.assert.ok(fastify.mysql) + t.assert.ok(fastify.mysql.test) + t.assert.ok(fastify.mysql.test.pool) + t.assert.ok(fastify.mysql.test.execute) + t.assert.ok(fastify.mysql.test.getConnection) + t.assert.ok(fastify.mysql.test.format) + t.assert.ok(fastify.mysql.test.escape) + t.assert.ok(fastify.mysql.test.escapeId) + + done() }) }) -test('synchronous functions', (t) => { +test('synchronous functions', (t, done) => { const fastify = Fastify() fastify.register(fastifyMysql, { @@ -141,48 +147,49 @@ test('synchronous functions', (t) => { user: 'root', database: 'mysql' }) + t.after(() => fastify.close()) fastify.ready((err) => { - t.error(err) + t.assert.ifError(err) - test('mysql.format', (t) => { + test('mysql.format', (t, done) => { const sqlString = fastify.mysql.format('SELECT ? AS `now`', [1]) - t.equal('SELECT 1 AS `now`', sqlString) - t.end() + t.assert.strictEqual('SELECT 1 AS `now`', sqlString) + done() }) - test('mysql.escape', (t) => { + test('mysql.escape', (t, done) => { const id = 'userId' const sql = 'SELECT * FROM users WHERE id = ' + fastify.mysql.escape(id) - t.equal(sql, `SELECT * FROM users WHERE id = '${id}'`) - t.end() + t.assert.strictEqual(sql, `SELECT * FROM users WHERE id = '${id}'`) + done() }) - test('mysql.escapeId', (t) => { + test('mysql.escapeId', (t, done) => { const sorter = 'date' const sql = 'SELECT * FROM posts ORDER BY ' + fastify.mysql.escapeId('posts.' + sorter) - t.ok(sql, 'SELECT * FROM posts ORDER BY `posts`.`date`') - t.end() + t.assert.strictEqual(sql, 'SELECT * FROM posts ORDER BY `posts`.`date`') + done() }) - fastify.close() - t.end() + done() }) }) -test('isMySQLPool is true', (t) => { +test('isMySQLPool is true', (t, done) => { t.plan(5) const fastify = Fastify() fastify.register(fastifyMysql, { connectionString: 'mysql://root@localhost/mysql' }) + t.after(() => fastify.close()) + fastify.ready((err) => { - t.error(err) - t.equal(isMySQLPool(fastify.mysql), true) - t.equal(isMySQLConnection(fastify.mysql), false) - t.equal(isMySQLPromisePool(fastify.mysql), false) - t.equal(isMySQLPromiseConnection(fastify.mysql), false) - t.end() + t.assert.ifError(err) + t.assert.strictEqual(isMySQLPool(fastify.mysql), true) + t.assert.strictEqual(isMySQLConnection(fastify.mysql), false) + t.assert.strictEqual(isMySQLPromisePool(fastify.mysql), false) + t.assert.strictEqual(isMySQLPromiseConnection(fastify.mysql), false) + done() }) - fastify.close() })