From 149295670f79cbf1438268f3dbd36ea905d912ca Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Tue, 31 Aug 2021 10:03:03 +0200 Subject: [PATCH] feat: webpack test stack (#3240) * 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 --- .github/workflows/ci.yml | 19 ++++++++++++ test/bundler/README.md | 29 +++++++++++++++++++ test/bundler/webpack/bundler-test.js | 24 +++++++++++++++ test/bundler/webpack/package.json | 11 +++++++ .../webpack/src/fail-plugin-version.js | 14 +++++++++ test/bundler/webpack/src/index.js | 9 ++++++ test/bundler/webpack/webpack.config.js | 13 +++++++++ 7 files changed, 119 insertions(+) create mode 100644 test/bundler/README.md create mode 100644 test/bundler/webpack/bundler-test.js create mode 100644 test/bundler/webpack/package.json create mode 100644 test/bundler/webpack/src/fail-plugin-version.js create mode 100644 test/bundler/webpack/src/index.js create mode 100644 test/bundler/webpack/webpack.config.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 82b0f3ed4c..28a40c88bf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 \ No newline at end of file diff --git a/test/bundler/README.md b/test/bundler/README.md new file mode 100644 index 0000000000..1602469874 --- /dev/null +++ b/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. diff --git a/test/bundler/webpack/bundler-test.js b/test/bundler/webpack/bundler-test.js new file mode 100644 index 0000000000..a945917e93 --- /dev/null +++ b/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) + }) +}) diff --git a/test/bundler/webpack/package.json b/test/bundler/webpack/package.json new file mode 100644 index 0000000000..926a7a8f00 --- /dev/null +++ b/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" + } +} diff --git a/test/bundler/webpack/src/fail-plugin-version.js b/test/bundler/webpack/src/fail-plugin-version.js new file mode 100644 index 0000000000..60153252b2 --- /dev/null +++ b/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 diff --git a/test/bundler/webpack/src/index.js b/test/bundler/webpack/src/index.js new file mode 100644 index 0000000000..822f6aea22 --- /dev/null +++ b/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 diff --git a/test/bundler/webpack/webpack.config.js b/test/bundler/webpack/webpack.config.js new file mode 100644 index 0000000000..e1470690a0 --- /dev/null +++ b/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' + } + } +}