Skip to content

Commit

Permalink
feat: webpack test stack (#3240)
Browse files Browse the repository at this point in the history
* feat: add webpack test stack

* feat: add bundler test version

* fix: set minimum version

* docs: add bundler test doc

* fix: review

Co-authored-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
zekth and mcollina committed Aug 31, 2021
1 parent a04f4c9 commit 1492956
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -64,3 +64,22 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
target: minor

package:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.3.4
- name: Use Node.js
uses: actions/setup-node@v2.1.5
with:
node-version: 14
- name: install fastify
run: |
npm install --ignore-scripts
- name: install bundler stack
run: |
cd test/bundler/webpack && npm install
- name: Test bundle
run: |
cd test/bundler/webpack && npm run test
29 changes: 29 additions & 0 deletions test/bundler/README.md
@@ -0,0 +1,29 @@
# Bundlers test stack

In some cases developers bundle their apps for several targets, eg: serveless 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 feature behaving differently like the version handling check for plugins.

## Test bundlers

The bundler test stack has been set appart than 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 does not need to install every bundler to run unit tests.

To run the bundler tests you'll need to first install the repository dependencies and after 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 can be catched by unit-test regex of fastify.
Test 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.
24 changes: 24 additions & 0 deletions test/bundler/webpack/bundler-test.js
@@ -0,0 +1,24 @@
'use strict'

const t = require('tap')
const test = t.test
const fastifySuccess = require('./dist/success')
const fastifyFailPlugin = require('./dist/failPlugin')

test('Bundled package should work', t => {
t.plan(1)
fastifySuccess.ready((err) => {
t.error(err)
})
})

// In the webpack bundle context the fastify package.json is not read
// Because of this the version is set to `undefined`, this makes the plugin
// version check not able to work properly. By then this test shouldn't work
// in non-bundled environment but works in bundled environment
test('Bundled package should work with bad plugin version, undefined version fallback', t => {
t.plan(1)
fastifyFailPlugin.ready((err) => {
t.error(err)
})
})
11 changes: 11 additions & 0 deletions test/bundler/webpack/package.json
@@ -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 changes: 14 additions & 0 deletions test/bundler/webpack/src/fail-plugin-version.js
@@ -0,0 +1,14 @@
const fp = require('fastify-plugin')
const fastify = require('../../../../')({
logger: true
})

fastify.get('/', function (request, reply) {
reply.send({ hello: 'world' })
})

fastify.register(fp((instance, opts, done) => {
done()
}, { fastify: '9.x' }))

module.exports = fastify
9 changes: 9 additions & 0 deletions test/bundler/webpack/src/index.js
@@ -0,0 +1,9 @@
const fastify = require('../../../../')({
logger: true
})
// Declare a route
fastify.get('/', function (request, reply) {
reply.send({ hello: 'world' })
})

module.exports = fastify
13 changes: 13 additions & 0 deletions test/bundler/webpack/webpack.config.js
@@ -0,0 +1,13 @@
const path = require('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'
}
}
}

0 comments on commit 1492956

Please sign in to comment.