Skip to content

Commit

Permalink
test: migrate from tap to node:test and c8 (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
inyourtime authored Nov 25, 2024
1 parent f0ba998 commit e3c4fe7
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 312 deletions.
4 changes: 0 additions & 4 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 @@ -19,7 +19,7 @@
"lint:fix": "standard --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap"
"test:unit": "c8 --100 node --test"
},
"keywords": [
"fastify",
Expand All @@ -41,10 +41,10 @@
"@fastify/type-provider-json-schema-to-ts": "^4.0.0",
"@fastify/type-provider-typebox": "^5.0.0",
"@types/node": "^22.0.0",
"c8": "^10.1.2",
"fastify": "^5.0.0",
"rimraf": "^6.0.1",
"standard": "^17.1.0",
"tap": "^18.7.1",
"tsd": "^0.31.1"
},
"dependencies": {
Expand Down
89 changes: 49 additions & 40 deletions test/auth.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifyAuth = require('../auth')

test('registering plugin with invalid default relation', async (t) => {
test('registering plugin with invalid default relation', (t, done) => {
t.plan(2)

const fastify = Fastify()
fastify.register(fastifyAuth, { defaultRelation: 'auth' })

fastify.ready((err) => {
t.ok(err)
t.equal(err.message, 'The value of default relation should be one of [\'or\', \'and\']')
t.assert.ok(err)
t.assert.strictEqual(err.message, 'The value of default relation should be one of [\'or\', \'and\']')
done()
})
})

test('Clean status code through auth pipeline', t => {
test('Clean status code through auth pipeline', (t, done) => {
t.plan(3)

const app = Fastify()
Expand All @@ -33,9 +34,10 @@ test('Clean status code through auth pipeline', t => {
name: 'two'
}
}, (err, res) => {
t.error(err)
t.equal(res.payload, '42')
t.equal(res.statusCode, 200)
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '42')
t.assert.strictEqual(res.statusCode, 200)
done()
})
})

Expand Down Expand Up @@ -68,7 +70,7 @@ test('defaultRelation: used when relation not specified', async (t) => {
method: 'GET',
url: '/welcome'
})
t.equal(response.statusCode, 502)
t.assert.strictEqual(response.statusCode, 502)

const res = await app.inject({
method: 'GET',
Expand All @@ -77,10 +79,10 @@ test('defaultRelation: used when relation not specified', async (t) => {
name: 'two'
}
})
t.equal(res.statusCode, 200)
t.assert.strictEqual(res.statusCode, 200)
})

test('Options: non-array functions input', t => {
test('Options: non-array functions input', (t, done) => {
t.plan(4)

const app = Fastify()
Expand All @@ -89,21 +91,22 @@ test('Options: non-array functions input', t => {
app.addHook('preHandler', app.auth('bogus'))
app.get('/', (req, res) => res.send(42))
} catch (error) {
t.ok(error)
t.equal(error.message, 'You must give an array of functions to the auth function')
t.assert.ok(error)
t.assert.strictEqual(error.message, 'You must give an array of functions to the auth function')
}
})

app.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})

test('Options: empty array functions input', t => {
test('Options: empty array functions input', (t, done) => {
t.plan(4)

const app = Fastify()
Expand All @@ -112,21 +115,22 @@ test('Options: empty array functions input', t => {
app.addHook('preHandler', app.auth([]))
app.get('/', (req, res) => res.send(42))
} catch (error) {
t.ok(error)
t.equal(error.message, 'Missing auth functions')
t.assert.ok(error)
t.assert.strictEqual(error.message, 'Missing auth functions')
}
})

app.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})

test('Options: faulty relation', t => {
test('Options: faulty relation', (t, done) => {
t.plan(4)

const app = Fastify()
Expand All @@ -135,21 +139,22 @@ test('Options: faulty relation', t => {
app.addHook('preHandler', app.auth([successWithCode('one', 201)], { relation: 'foo' }))
app.get('/', (req, res) => res.send(42))
} catch (error) {
t.ok(error)
t.equal(error.message, 'The value of options.relation should be one of [\'or\', \'and\']')
t.assert.ok(error)
t.assert.strictEqual(error.message, 'The value of options.relation should be one of [\'or\', \'and\']')
}
})

app.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})

test('Options: faulty run', t => {
test('Options: faulty run', (t, done) => {
t.plan(4)

const app = Fastify()
Expand All @@ -158,21 +163,22 @@ test('Options: faulty run', t => {
app.addHook('preHandler', app.auth([successWithCode('one', 201)], { run: 'foo' }))
app.get('/', (req, res) => res.send(42))
} catch (error) {
t.ok(error)
t.equal(error.message, 'The value of options.run must be \'all\'')
t.assert.ok(error)
t.assert.strictEqual(error.message, 'The value of options.run must be \'all\'')
}
})

app.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})

test('Avoid status code overwriting', t => {
test('Avoid status code overwriting', (t, done) => {
t.plan(3)

const app = Fastify()
Expand All @@ -189,13 +195,14 @@ test('Avoid status code overwriting', t => {
name: 'two'
}
}, (err, res) => {
t.error(err)
t.equal(res.payload, '42')
t.equal(res.statusCode, 202)
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '42')
t.assert.strictEqual(res.statusCode, 202)
done()
})
})

test('Last win when all failures', t => {
test('Last win when all failures', (t, done) => {
t.plan(2)

const app = Fastify()
Expand All @@ -212,12 +219,13 @@ test('Last win when all failures', t => {
name: 'three'
}
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 502)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 502)
done()
})
})

test('First success win', t => {
test('First success win', (t, done) => {
t.plan(2)

const app = Fastify()
Expand All @@ -234,8 +242,9 @@ test('First success win', t => {
name: 'two'
}
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 202)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 202)
done()
})
})

Expand Down
11 changes: 0 additions & 11 deletions test/example-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,4 @@ function build (opts) {
return fastify
}

if (require.main === module) {
const fastify = build({
logger: {
level: 'info'
}
})
fastify.listen({ port: 3000, host: '0.0.0.0' }, err => {
if (err) throw err
})
}

module.exports = build
Loading

0 comments on commit e3c4fe7

Please sign in to comment.