Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate to node test runner #141

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .taprc

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap"
"test:unit": "c8 --100 node --test"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -66,12 +66,12 @@
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"@types/levelup": "^5.1.0",
"c8": "^10.1.3",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"memdown": "^6.0.0",
"neostandard": "^0.12.0",
"rimraf": "^6.0.1",
"tap": "^16.0.0",
"tsd": "^0.31.0"
},
"publishConfig": {
Expand Down
115 changes: 46 additions & 69 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,64 @@
'use strict'

const t = require('tap')
const { test, after } = require('node:test')
const { existsSync } = require('node:fs')
const test = t.test
const rimraf = require('rimraf')
const Fastify = require('fastify')
const memdown = require('memdown')
const fastifyLeveldb = require('..')

t.teardown(() => {
after(() => {
rimraf.sync('./test_db')
rimraf.sync('./foo')
rimraf.sync('./bar')
})

test('level namespace should exist', t => {
t.plan(3)
test('level namespace should exist', async t => {
t.plan(1)

const fastify = Fastify()
fastify
.register(fastifyLeveldb, { name: 'test_db' })
.ready(err => {
t.error(err)
t.ok(fastify.level.test_db)
fastify.close(() => {
t.pass('unlock')
})
})
await fastify.register(fastifyLeveldb, { name: 'test_db' }).ready()

t.assert.ok(fastify.level.test_db)
return fastify.close()
})

test('missing database name', t => {
test('missing database name', async t => {
t.plan(1)

const fastify = Fastify()
fastify
await t.assert.rejects(() => fastify
.register(fastifyLeveldb, { name: undefined })
.ready(err => {
t.equal(err.message, 'Missing database name')
})
.ready(), undefined, 'Missing database name')
})

test('level should support leveldb operations', t => {
t.plan(5)
test('level should support leveldb operations', async t => {
t.plan(1)

const fastify = Fastify()
fastify
await fastify
.register(fastifyLeveldb, { name: 'test_db' })
.ready(err => {
t.error(err)
fastify.level.test_db.put('a', 'b', err => {
t.error(err)
fastify.level.test_db.get('a', (err, val) => {
t.error(err)
t.equal(val, 'b')
fastify.close(() => {
t.pass('unlock')
})
})
})
})
.ready()

await fastify.level.test_db.put('a', 'b')
const val = await fastify.level.test_db.get('a')
t.assert.deepStrictEqual(val, 'b')
return fastify.close()
})

test('level should support other stores (memdown)', t => {
t.plan(5)
test('level should support other stores (memdown)', async t => {
t.plan(1)

const fastify = Fastify()
fastify
await fastify
.register(fastifyLeveldb, { name: 'test_db', options: { store: memdown } })
.ready(err => {
t.error(err)
fastify.level.test_db.put('a', 'b', err => {
t.error(err)
fastify.level.test_db.get('a', (err, val) => {
t.error(err)
t.equal(val, 'b')
fastify.close(() => {
t.pass('unlock')
})
})
})
})
.ready()

await fastify.level.test_db.put('a', 'b')

const val = await fastify.level.test_db.get('a')
t.assert.deepStrictEqual(val, 'b')
return fastify.close()
})

test('level should support leveldb operations (async await)', async t => {
Expand All @@ -88,8 +67,8 @@ test('level should support leveldb operations (async await)', async t => {
await fastify.register(fastifyLeveldb, { name: 'test_db' })
await fastify.level.test_db.put('a', 'b')
const val = await fastify.level.test_db.get('a')
t.equal(val, 'b')
await fastify.close()
t.assert.deepStrictEqual(val, 'b')
return fastify.close()
})

test('namespaces', async t => {
Expand All @@ -99,20 +78,18 @@ test('namespaces', async t => {
await fastify.register(fastifyLeveldb, { name: 'bar' })
await fastify.level.foo.put('a', 'b')
await fastify.level.bar.put('a', 'b')
t.equal(await fastify.level.foo.get('a'), 'b')
t.equal(await fastify.level.bar.get('a'), 'b')
await fastify.close()
t.assert.deepStrictEqual(await fastify.level.foo.get('a'), 'b')
t.assert.deepStrictEqual(await fastify.level.bar.get('a'), 'b')
return fastify.close()
})

test('reuse namespaces', t => {
t.plan(2)
test('reuse namespaces', async t => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifyLeveldb, { name: 'foo' })
fastify.register(fastifyLeveldb, { name: 'foo' })
fastify.ready(err => {
t.equal(err.message, 'Level namespace already used: foo')
fastify.close(() => t.pass('closed'))
})
await t.assert.rejects(() => fastify.ready(), undefined, 'Level namespace already used: foo')
return fastify.close()
})

test('store json', async t => {
Expand All @@ -123,8 +100,8 @@ test('store json', async t => {
options: { valueEncoding: 'json' }
})
await fastify.level.test_db.put('greeting', { hello: 'world' })
t.same(await fastify.level.test_db.get('greeting'), { hello: 'world' })
await fastify.close()
t.assert.deepStrictEqual(await fastify.level.test_db.get('greeting'), { hello: 'world' })
return fastify.close()
})

test('custom path', async t => {
Expand All @@ -134,9 +111,9 @@ test('custom path', async t => {
await fastify.register(fastifyLeveldb, { name: 'second', path: 'bar' })
await fastify.level.first.put('a', 'b')
await fastify.level.second.put('a', 'b')
t.equal(await fastify.level.second.get('a'), 'b')
t.equal(await fastify.level.second.get('a'), 'b')
t.ok(existsSync('./foo'))
t.ok(existsSync('./bar'))
await fastify.close()
t.assert.deepStrictEqual(await fastify.level.second.get('a'), 'b')
t.assert.deepStrictEqual(await fastify.level.second.get('a'), 'b')
t.assert.ok(existsSync('./foo'))
t.assert.ok(existsSync('./bar'))
return fastify.close()
})