Skip to content

Commit c19f6ec

Browse files
authored
test: use @kakang/unit instead of node:test (#103)
* test: use `@kakang/unit` instead of `node:test` * test: fix unit
1 parent 2b1eae4 commit c19f6ec

39 files changed

+748
-691
lines changed

packages/config/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"build": "node ../../scripts/build.mjs --build=\"all\"",
2222
"build:cjs": "node ../../scripts/build.mjs --build='cjs'",
2323
"build:mjs": "node ../../scripts/build.mjs --build='mjs'",
24-
"unit": "node --require ts-node/register ../../scripts/test.mjs",
24+
"unit": "cross-env \"NODE_OPTIONS=--require ts-node/register\" unit",
2525
"test": "npm run lint && npm run unit",
26-
"coverage": "c8 node --require ts-node/register ./test/run.ts",
26+
"coverage": "cross-env \"NODE_OPTIONS=--require ts-node/register\" c8 unit",
2727
"prepublishOnly": "npm run build",
2828
"postpublish": "npm run clean"
2929
},
@@ -38,10 +38,12 @@
3838
"author": "KaKa <[email protected]>",
3939
"license": "MIT",
4040
"devDependencies": {
41+
"@kakang/unit": "^0.0.2",
4142
"@types/node": "^20.12.4",
4243
"@typescript-eslint/eslint-plugin": "^7.5.0",
4344
"@typescript-eslint/parser": "^7.5.0",
4445
"c8": "^9.1.0",
46+
"cross-env": "^7.0.3",
4547
"eslint": "^8.57.0",
4648
"eslint-config-standard-with-typescript": "^43.0.1",
4749
"eslint-plugin-import": "^2.29.1",

packages/config/test/index.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { test } from '@kakang/unit'
12
import Fastify from 'fastify'
2-
import assert from 'node:assert/strict'
33
import { rm, writeFile } from 'node:fs/promises'
44
import { resolve } from 'node:path'
5-
import { test } from 'node:test'
65
import { build } from '../lib'
76

87
test('envfile', async function (t) {
@@ -16,7 +15,9 @@ test('envfile', async function (t) {
1615
await rm(tmpEnv, { force: true })
1716
})
1817

19-
await t.test('', async function () {
18+
t.test('', async function (t) {
19+
const ok: typeof t.ok = t.ok
20+
2021
const { env, plugin } = build({
2122
schema: {
2223
type: 'object',
@@ -28,20 +29,20 @@ test('envfile', async function (t) {
2829
dotenv: { path: tmpEnv }
2930
})
3031

31-
assert.ok(env)
32-
assert.equal(env.FOO, 'BAR')
33-
assert.equal(typeof env.NOT, 'undefined')
34-
assert.equal(typeof plugin, 'function')
32+
ok(env)
33+
t.equal(env.FOO, 'BAR')
34+
t.equal(typeof env.NOT, 'undefined')
35+
t.equal(typeof plugin, 'function')
3536

3637
const fastify = Fastify()
3738
fastify.register(plugin)
3839
await fastify.ready()
3940

4041
// @ts-expect-error we need to types by client
41-
assert.ok(fastify.config)
42+
ok(fastify.config)
4243
// @ts-expect-error we need to types by client
43-
assert.equal(fastify.config.FOO, 'BAR')
44+
t.equal(fastify.config.FOO, 'BAR')
4445
// @ts-expect-error we need to types by client
45-
assert.equal(typeof fastify.config.NOT, 'undefined')
46+
t.equal(typeof fastify.config.NOT, 'undefined')
4647
})
4748
})

packages/cronjob/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
"build": "node ../../scripts/build.mjs --build=\"all\"",
3838
"build:cjs": "node ../../scripts/build.mjs --build='cjs'",
3939
"build:mjs": "node ../../scripts/build.mjs --build='mjs'",
40-
"unit": "node --require ts-node/register ../../scripts/test.mjs --timeout 120000",
40+
"unit": "cross-env \"NODE_OPTIONS=--require ts-node/register\" unit",
4141
"test": "npm run lint && npm run unit",
42-
"coverage": "c8 node --require ts-node/register ./test/run.ts",
42+
"coverage": "cross-env \"NODE_OPTIONS=--require ts-node/register\" c8 unit",
4343
"prepublishOnly": "npm run build",
4444
"postpublish": "npm run clean"
4545
},
@@ -54,10 +54,12 @@
5454
"author": "KaKa <[email protected]>",
5555
"license": "MIT",
5656
"devDependencies": {
57+
"@kakang/unit": "^0.0.2",
5758
"@types/node": "^20.12.4",
5859
"@typescript-eslint/eslint-plugin": "^7.5.0",
5960
"@typescript-eslint/parser": "^7.5.0",
6061
"c8": "^9.1.0",
62+
"cross-env": "^7.0.3",
6163
"eslint": "^8.57.0",
6264
"eslint-config-standard-with-typescript": "^43.0.1",
6365
"eslint-plugin-import": "^2.29.1",

packages/cronjob/test/adapter/mongodb.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { test } from '@kakang/unit'
12
import { parseExpression } from 'cron-parser'
23
import Fastify from 'fastify'
34
import { MongoClient } from 'mongodb'
4-
import assert from 'node:assert/strict'
5-
import { test } from 'node:test'
65
import { fastifyCronJob } from '../../lib'
76
import { MongoDBAdapter, type MongoDBAdapterOptions } from '../../lib/adapter/mongodb'
87

@@ -68,34 +67,34 @@ test('MongoDBAdapter', async function (t) {
6867
if (isJob(task.uid, 'interval')) {
6968
if (ticks[task.uid] >= 2) {
7069
fastify.cronjob.clearInterval(task.uid as string)
71-
assert.equal(ticks[task.uid], 2)
70+
t.equal(ticks[task.uid], 2)
7271
dones[task.uid]()
7372
} else {
7473
timestamps[task.uid][0] = now + task.delay
75-
assert.equal(ticks[task.uid], 1)
74+
t.equal(ticks[task.uid], 1)
7675
}
7776
} else if (isJob(task.uid, 'cron')) {
7877
if (ticks[task.uid] >= 2) {
7978
fastify.cronjob.clearInterval(task.uid as string)
80-
assert.equal(ticks[task.uid] >= 2, true)
79+
t.equal(ticks[task.uid] >= 2, true)
8180
dones[task.uid]()
8281
} else {
8382
timestamps[task.uid][0] = now + task.delay
84-
assert.equal(ticks[task.uid], 1)
83+
t.equal(ticks[task.uid], 1)
8584
}
8685
} else if (isJob(task.uid, 'loop')) {
8786
fastify.cronjob.clearInterval(task.uid as string)
88-
assert.equal(ticks[task.uid] >= 1, true)
87+
t.equal(ticks[task.uid] >= 1, true)
8988
dones[task.uid]()
9089
} else {
9190
fastify.cronjob.clearInterval(task.uid as string)
92-
assert.equal(ticks[task.uid], 1)
91+
t.equal(ticks[task.uid], 1)
9392
dones[task.uid]()
9493
}
9594

9695
if (!isJob(task.uid, 'loop') && !isJob(task.uid, 'cron')) {
97-
assert.equal(_from < now && now < _to, true)
98-
assert.equal(diff < RANDOM_GAP, true)
96+
t.equal(_from < now && now < _to, true)
97+
t.equal(diff < RANDOM_GAP, true)
9998
}
10099
}
101100
})
@@ -147,15 +146,16 @@ test('MongoDBAdapter', async function (t) {
147146
await client.close(true)
148147
})
149148

150-
await t.test('fastify.cronjob', function (t, done) {
151-
assert.ok(fastify.cronjob)
152-
assert.equal(typeof fastify.cronjob.setTimeout, 'function')
153-
assert.equal(typeof fastify.cronjob.setInterval, 'function')
154-
assert.equal(typeof fastify.cronjob.setImmediate, 'function')
149+
t.test('fastify.cronjob', function (t, done) {
150+
const ok: typeof t.ok = t.ok
151+
ok(fastify.cronjob)
152+
t.equal(typeof fastify.cronjob.setTimeout, 'function')
153+
t.equal(typeof fastify.cronjob.setInterval, 'function')
154+
t.equal(typeof fastify.cronjob.setImmediate, 'function')
155155
done()
156156
})
157157

158-
await t.test('interval', async function () {
158+
t.test('interval', async function () {
159159
const promises = [
160160
checkInterval(384),
161161
checkInterval(512),
@@ -171,7 +171,7 @@ test('MongoDBAdapter', async function (t) {
171171
await Promise.allSettled(promises)
172172
})
173173

174-
await t.test('timeout', async function () {
174+
t.test('timeout', async function () {
175175
const promises = [
176176
checkTimeout(384),
177177
checkTimeout(512),
@@ -187,7 +187,7 @@ test('MongoDBAdapter', async function (t) {
187187
await Promise.allSettled(promises)
188188
})
189189

190-
await t.test('cronjob', async function () {
190+
t.test('cronjob', async function () {
191191
const promises = [
192192
checkCronJob('* * * * * *'),
193193
checkCronJob('*/2 * * * * *'),
@@ -198,7 +198,7 @@ test('MongoDBAdapter', async function (t) {
198198
await Promise.allSettled(promises)
199199
})
200200

201-
await t.test('looptask', async function () {
201+
t.test('looptask', async function () {
202202
const promises = [
203203
checkLoopTask(384)
204204
]

packages/cronjob/test/error.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
import { test } from '@kakang/unit'
12
import Fastify from 'fastify'
2-
import assert from 'node:assert/strict'
3-
import { test } from 'node:test'
43
import { fastifyCronJob } from '../lib'
54

65
test('missing Adapter', async function (t) {
6+
const ok: typeof t.ok = t.ok
77
try {
88
const fastify = Fastify()
99
fastify.register(fastifyCronJob)
1010
await fastify.ready()
11-
assert.ok(false, 'should not success')
11+
ok(false, 'should not success')
1212
} catch (err: any) {
13-
assert.ok(err)
14-
assert.equal(err.code, 'FST_CJ_INVALID_OPTION')
13+
ok(err)
14+
t.equal(err.code, 'FST_CJ_INVALID_OPTION')
1515
}
1616
})

packages/mongodb/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"build": "node ../../scripts/build.mjs --build=\"all\"",
2222
"build:cjs": "node ../../scripts/build.mjs --build='cjs'",
2323
"build:mjs": "node ../../scripts/build.mjs --build='mjs'",
24-
"unit": "node --require ts-node/register ../../scripts/test.mjs",
24+
"unit": "cross-env \"NODE_OPTIONS=--require ts-node/register\" unit",
2525
"test": "npm run lint && npm run unit",
26-
"coverage": "c8 node --require ts-node/register ./test/run.ts",
26+
"coverage": "cross-env \"NODE_OPTIONS=--require ts-node/register\" c8 unit",
2727
"prepublishOnly": "npm run build",
2828
"postpublish": "npm run clean"
2929
},
@@ -38,10 +38,12 @@
3838
"author": "KaKa <[email protected]>",
3939
"license": "MIT",
4040
"devDependencies": {
41+
"@kakang/unit": "^0.0.2",
4142
"@types/node": "^20.12.4",
4243
"@typescript-eslint/eslint-plugin": "^7.5.0",
4344
"@typescript-eslint/parser": "^7.5.0",
4445
"c8": "^9.1.0",
46+
"cross-env": "^7.0.3",
4547
"eslint": "^8.57.0",
4648
"eslint-config-standard-with-typescript": "^43.0.1",
4749
"eslint-plugin-import": "^2.29.1",

packages/mongodb/test/index.test.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1+
import { test } from '@kakang/unit'
12
import Fastify from 'fastify'
2-
import assert from 'node:assert/strict'
3-
import test from 'node:test'
43
import { fastifyMongoDB } from '../lib/index'
54

65
const MONGODB_URI = 'mongodb://127.0.0.1:27017/?replicaSet=rs0'
76

87
test('option', async function (t) {
9-
await t.test('missing url', async function (t) {
8+
t.test('missing url', async function (t) {
109
const fastify = Fastify()
1110

1211
fastify.register(fastifyMongoDB)
1312

14-
assert.rejects(async () => {
13+
t.rejects(async () => {
1514
await fastify.ready()
1615
}, Error)
1716
})
1817

19-
await t.test('missing database', async function (t) {
18+
t.test('missing database', async function (t) {
2019
const fastify = Fastify()
2120

2221
// @ts-expect-error - we check for missing option
2322
fastify.register(fastifyMongoDB, { url: 'mongodb://127.0.0.1:27017' })
2423

25-
assert.rejects(async () => {
24+
t.rejects(async () => {
2625
await fastify.ready()
2726
}, Error)
2827
})
2928
})
3029

3130
test('register', async function (t) {
31+
const ok: typeof t.ok = t.ok
32+
3233
const fastify = Fastify()
3334

3435
t.after(async function () {
@@ -42,13 +43,14 @@ test('register', async function (t) {
4243

4344
await fastify.ready()
4445

45-
assert.ok(fastify.mongodb)
46-
assert.ok(fastify.mongodb.client)
47-
assert.ok(fastify.mongodb.db)
48-
assert.equal(typeof fastify.mongodb.withTransaction, 'function')
46+
ok(fastify.mongodb)
47+
ok(fastify.mongodb.client)
48+
ok(fastify.mongodb.db)
49+
t.equal(typeof fastify.mongodb.withTransaction, 'function')
4950
})
5051

5152
test('register', async function (t) {
53+
const ok: typeof t.ok = t.ok
5254
const fastify = Fastify()
5355

5456
t.after(async function () {
@@ -62,10 +64,10 @@ test('register', async function (t) {
6264

6365
await fastify.ready()
6466

65-
assert.ok(fastify.mongodb)
66-
assert.ok(fastify.mongodb.client)
67-
assert.ok(fastify.mongodb.db)
68-
assert.equal(typeof fastify.mongodb.withTransaction, 'function')
67+
ok(fastify.mongodb)
68+
ok(fastify.mongodb.client)
69+
ok(fastify.mongodb.db)
70+
t.equal(typeof fastify.mongodb.withTransaction, 'function')
6971
})
7072

7173
test('functional', async function (t) {
@@ -83,7 +85,7 @@ test('functional', async function (t) {
8385
await fastify.ready()
8486

8587
const result = await fastify.mongodb.db.collection('hello').insertMany([{ foo: 'bar' }])
86-
assert.equal(result.insertedCount, 1)
88+
t.equal(result.insertedCount, 1)
8789
})
8890

8991
test('functional', async function (t) {
@@ -102,14 +104,14 @@ test('functional', async function (t) {
102104

103105
{
104106
const result = await fastify.mongodb.db.collection('hello').insertMany([{ foo: 'bar' }])
105-
assert.equal(result.insertedCount, 1)
107+
t.equal(result.insertedCount, 1)
106108
}
107109

108110
{
109111
const result = await fastify.mongodb.withTransaction(async (session) => {
110112
const result = await fastify.mongodb.db.collection('hello').insertMany([{ foo: 'bar' }], { session })
111113
return result
112114
})
113-
assert.equal(result.insertedCount, 1)
115+
t.equal(result.insertedCount, 1)
114116
}
115117
})

packages/multipart/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@
6868
"build": "node ../../scripts/build.mjs --build=\"all\"",
6969
"build:cjs": "node ../../scripts/build.mjs --build='cjs'",
7070
"build:mjs": "node ../../scripts/build.mjs --build='mjs'",
71-
"unit": "node --require ts-node/register ../../scripts/test.mjs",
71+
"unit": "cross-env \"NODE_OPTIONS=--require ts-node/register\" unit",
7272
"test": "npm run lint && npm run unit",
73-
"coverage": "c8 node --require ts-node/register ./test/run.ts",
73+
"coverage": "cross-env \"NODE_OPTIONS=--require ts-node/register\" c8 unit",
7474
"prepublishOnly": "npm run build",
7575
"postpublish": "npm run clean"
7676
},
@@ -85,12 +85,15 @@
8585
"author": "KaKa <[email protected]>",
8686
"license": "GPL-3.0",
8787
"devDependencies": {
88+
"@kakang/unit": "^0.0.2",
8889
"@types/busboy": "^1.5.3",
8990
"@types/formidable": "^3.4.5",
9091
"@types/node": "^20.12.4",
9192
"@typescript-eslint/eslint-plugin": "7.5.0",
9293
"@typescript-eslint/parser": "7.5.0",
9394
"busboy": "^1.6.0",
95+
"c8": "^9.1.0",
96+
"cross-env": "^7.0.3",
9497
"eslint": "^8.57.0",
9598
"eslint-config-standard-with-typescript": "^43.0.1",
9699
"eslint-plugin-import": "^2.29.1",

0 commit comments

Comments
 (0)