fatsify核心功能示例测试!!!
This commit is contained in:
29
node_modules/fastify/test/bundler/README.md
generated
vendored
Normal file
29
node_modules/fastify/test/bundler/README.md
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
# Bundlers test stack
|
||||
|
||||
In some cases, developers bundle their apps for several targets such as serverless applications.
|
||||
Even if it's not recommended by Fastify team; we need to ensure we do not break the build process.
|
||||
Please note this might result in features behaving differently, like the version handling check for plugins.
|
||||
|
||||
## Test bundlers
|
||||
|
||||
The bundler test stack has been defined separately from the rest of the Unit testing stack because it's not a
|
||||
part of the fastify lib itself. Note that the tests run in CI only on NodeJs LTS version.
|
||||
Developers do not need to install every bundler to run unit tests.
|
||||
|
||||
To run the bundler tests you will need to install the repository dependencies followed by the bundler
|
||||
stack dependencies. See:
|
||||
|
||||
```bash
|
||||
# path: root of repository /fastify
|
||||
npm i
|
||||
cd test/bundler/webpack
|
||||
npm i
|
||||
npm run test # test command runs bundle before of starting the test
|
||||
```
|
||||
|
||||
## Bundler test development
|
||||
|
||||
To not break the fastify unit testing stack please name test files like this `*-test.js` and not `*.test.js`,
|
||||
otherwise it will be targeted by the regular expression used for unit tests for fastify.
|
||||
Tests need to ensure the build process works and the fastify application can be run,
|
||||
no need to go in deep testing unless an issue is raised.
|
||||
32
node_modules/fastify/test/bundler/esbuild/bundler-test.js
generated
vendored
Normal file
32
node_modules/fastify/test/bundler/esbuild/bundler-test.js
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastifySuccess = require('./dist/success')
|
||||
const fastifyFailPlugin = require('./dist/failPlugin')
|
||||
|
||||
test('Bundled package should work', (t, done) => {
|
||||
t.plan(4)
|
||||
fastifySuccess.ready((err) => {
|
||||
t.assert.ifError(err)
|
||||
fastifySuccess.inject(
|
||||
{
|
||||
method: 'GET',
|
||||
url: '/'
|
||||
},
|
||||
(error, res) => {
|
||||
t.assert.ifError(error)
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
t.assert.deepStrictEqual(res.json(), { hello: 'world' })
|
||||
done()
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
test('Bundled package should not work with bad plugin version', (t, done) => {
|
||||
t.plan(1)
|
||||
fastifyFailPlugin.ready((err) => {
|
||||
t.assert.match(err.message, /expected '9.x' fastify version/i)
|
||||
done()
|
||||
})
|
||||
})
|
||||
10
node_modules/fastify/test/bundler/esbuild/package.json
generated
vendored
Normal file
10
node_modules/fastify/test/bundler/esbuild/package.json
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"bundle": "esbuild success=src/index.js failPlugin=src/fail-plugin-version.js --bundle --outdir=dist --platform=node",
|
||||
"test": "npm run bundle && node bundler-test.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"esbuild": "^0.25.0"
|
||||
}
|
||||
}
|
||||
14
node_modules/fastify/test/bundler/esbuild/src/fail-plugin-version.js
generated
vendored
Normal file
14
node_modules/fastify/test/bundler/esbuild/src/fail-plugin-version.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
'use strict'
|
||||
|
||||
const fp = require('fastify-plugin')
|
||||
const fastify = require('../../../../')()
|
||||
|
||||
fastify.get('/', function (request, reply) {
|
||||
reply.send({ hello: 'world' })
|
||||
})
|
||||
|
||||
fastify.register(fp((instance, opts, done) => {
|
||||
done()
|
||||
}, { fastify: '9.x' }))
|
||||
|
||||
module.exports = fastify
|
||||
9
node_modules/fastify/test/bundler/esbuild/src/index.js
generated
vendored
Normal file
9
node_modules/fastify/test/bundler/esbuild/src/index.js
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
const fastify = require('../../../../')()
|
||||
// Declare a route
|
||||
fastify.get('/', function (request, reply) {
|
||||
reply.send({ hello: 'world' })
|
||||
})
|
||||
|
||||
module.exports = fastify
|
||||
32
node_modules/fastify/test/bundler/webpack/bundler-test.js
generated
vendored
Normal file
32
node_modules/fastify/test/bundler/webpack/bundler-test.js
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const fastifySuccess = require('./dist/success')
|
||||
const fastifyFailPlugin = require('./dist/failPlugin')
|
||||
|
||||
test('Bundled package should work', (t, done) => {
|
||||
t.plan(4)
|
||||
fastifySuccess.ready((err) => {
|
||||
t.assert.ifError(err)
|
||||
fastifySuccess.inject(
|
||||
{
|
||||
method: 'GET',
|
||||
url: '/'
|
||||
},
|
||||
(error, res) => {
|
||||
t.assert.ifError(error)
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
t.assert.deepStrictEqual(res.json(), { hello: 'world' })
|
||||
done()
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
test('Bundled package should not work with bad plugin version', (t, done) => {
|
||||
t.plan(1)
|
||||
fastifyFailPlugin.ready((err) => {
|
||||
t.assert.match(err.message, /expected '9.x' fastify version/i)
|
||||
done()
|
||||
})
|
||||
})
|
||||
11
node_modules/fastify/test/bundler/webpack/package.json
generated
vendored
Normal file
11
node_modules/fastify/test/bundler/webpack/package.json
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"version":"0.0.1",
|
||||
"scripts": {
|
||||
"bundle": "webpack",
|
||||
"test": "npm run bundle && node bundler-test.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"webpack": "^5.49.0",
|
||||
"webpack-cli": "^4.7.2"
|
||||
}
|
||||
}
|
||||
14
node_modules/fastify/test/bundler/webpack/src/fail-plugin-version.js
generated
vendored
Normal file
14
node_modules/fastify/test/bundler/webpack/src/fail-plugin-version.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
'use strict'
|
||||
|
||||
const fp = require('fastify-plugin')
|
||||
const fastify = require('../../../../')()
|
||||
|
||||
fastify.get('/', function (request, reply) {
|
||||
reply.send({ hello: 'world' })
|
||||
})
|
||||
|
||||
fastify.register(fp((instance, opts, done) => {
|
||||
done()
|
||||
}, { fastify: '9.x' }))
|
||||
|
||||
module.exports = fastify
|
||||
9
node_modules/fastify/test/bundler/webpack/src/index.js
generated
vendored
Normal file
9
node_modules/fastify/test/bundler/webpack/src/index.js
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
const fastify = require('../../../../')()
|
||||
// Declare a route
|
||||
fastify.get('/', function (request, reply) {
|
||||
reply.send({ hello: 'world' })
|
||||
})
|
||||
|
||||
module.exports = fastify
|
||||
15
node_modules/fastify/test/bundler/webpack/webpack.config.js
generated
vendored
Normal file
15
node_modules/fastify/test/bundler/webpack/webpack.config.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('node:path')
|
||||
|
||||
module.exports = {
|
||||
entry: { success: './src/index.js', failPlugin: './src/fail-plugin-version.js' },
|
||||
target: 'node',
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
filename: '[name].js',
|
||||
library: {
|
||||
type: 'commonjs2'
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user