This commit is contained in:
2025-05-21 04:18:42 +03:30
commit d5aff06ada
18 changed files with 4718 additions and 0 deletions

35
test/helper.js Normal file
View File

@@ -0,0 +1,35 @@
'use strict'
// This file contains code that we reuse
// between our tests.
const { build: buildApplication } = require('fastify-cli/helper')
const path = require('node:path')
const AppPath = path.join(__dirname, '..', 'app.js')
// Fill in this config with all the configurations
// needed for testing the application
function config () {
return {}
}
// automatically build and tear down our instance
async function build (t) {
// you can set all the options supported by the fastify CLI command
const argv = [AppPath]
// fastify-plugin ensures that all decorators
// are exposed for testing purposes, this is
// different from the production setup
const app = await buildApplication(argv, config())
// close the app after we are done
t.after(() => app.close())
return app
}
module.exports = {
config,
build
}

View File

@@ -0,0 +1,28 @@
'use strict'
const { test } = require('node:test')
const assert = require('node:assert')
const Fastify = require('fastify')
const Support = require('../../plugins/support')
test('support works standalone', async (t) => {
const fastify = Fastify()
fastify.register(Support)
await fastify.ready()
assert.equal(fastify.someSupport(), 'hugs')
})
// You can also use plugin with opts in fastify v2
//
// test('support works standalone', (t) => {
// t.plan(2)
// const fastify = Fastify()
// fastify.register(Support)
//
// fastify.ready((err) => {
// t.error(err)
// assert.equal(fastify.someSupport(), 'hugs')
// })
// })

View File

@@ -0,0 +1,28 @@
'use strict'
const { test } = require('node:test')
const assert = require('node:assert')
const { build } = require('../helper')
test('example is loaded', async (t) => {
const app = await build(t)
const res = await app.inject({
url: '/example'
})
assert.equal(res.payload, 'this is an example')
})
// inject callback style:
//
// test('example is loaded', (t) => {
// t.plan(2)
// const app = await build(t)
//
// app.inject({
// url: '/example'
// }, (err, res) => {
// t.error(err)
// assert.equal(res.payload, 'this is an example')
// })
// })

28
test/routes/root.test.js Normal file
View File

@@ -0,0 +1,28 @@
'use strict'
const { test } = require('node:test')
const assert = require('node:assert')
const { build } = require('../helper')
test('default root route', async (t) => {
const app = await build(t)
const res = await app.inject({
url: '/'
})
assert.deepStrictEqual(JSON.parse(res.payload), { root: true })
})
// inject callback style:
//
// test('default root route', (t) => {
// t.plan(2)
// const app = await build(t)
//
// app.inject({
// url: '/'
// }, (err, res) => {
// t.error(err)
// assert.deepStrictEqual(JSON.parse(res.payload), { root: true })
// })
// })