├── .eslintrc.js ├── .gitignore ├── package.json ├── server.js └── test └── server.test.js /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "extends": [ 3 | "standard", 4 | 'plugin:jest/recommended' 5 | ], 6 | "plugins": [ 7 | "standard", 8 | "promise", 9 | "jest" 10 | ], 11 | "rules": { 12 | "semi": [2, "always"] 13 | }, 14 | "env": { 15 | "node": true, 16 | 'jest/globals': true 17 | } 18 | }; -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Coverage directory 12 | coverage 13 | 14 | 15 | # Dependency directories 16 | node_modules 17 | 18 | # Optional npm cache directory 19 | .npm 20 | 21 | # mac files 22 | .DS_Store 23 | 24 | # vim swap files 25 | *.swp 26 | 27 | 28 | # vscode 29 | .vscode 30 | *code-workspace 31 | 32 | # lock files 33 | yarn.lock 34 | package-lock.json 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fastify-jest-example", 3 | "version": "1.0.0", 4 | "description": "an example on how integrate jest lib with fastify", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "jest test", 8 | "coverage": "jest --collectCoverageFrom=test/**.js --coverage" 9 | }, 10 | "keywords": [ 11 | "api", 12 | "fastify", 13 | "jest" 14 | ], 15 | "author": "mstocchi", 16 | "license": "ISC", 17 | "dependencies": { 18 | "fastify": "^1.0.0-rc.3" 19 | }, 20 | "devDependencies": { 21 | "eslint": "^4.18.2", 22 | "eslint-config-standard": "^11.0.0", 23 | "eslint-plugin-import": "^2.9.0", 24 | "eslint-plugin-jest": "^21.12.3", 25 | "eslint-plugin-node": "^6.0.1", 26 | "eslint-plugin-promise": "^3.6.0", 27 | "eslint-plugin-standard": "^3.0.1", 28 | "jest": "^22.4.2" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const fastify = require('fastify')(); 2 | 3 | fastify.get('/', async (request, reply) => { 4 | return { 5 | hello: 'world' 6 | }; 7 | }); 8 | 9 | const start = async () => { 10 | try { 11 | await fastify.listen(3000); 12 | console.log(`server listening on ${fastify.server.address().port}`); 13 | } catch (err) { 14 | fastify.log.error(err); 15 | process.exit(1); 16 | } 17 | }; 18 | 19 | process.on('SIGINT', async () => { 20 | console.log('stopping fastify server'); 21 | await fastify.close(); 22 | console.log('fastify server stopped'); 23 | process.exit(0); 24 | }); 25 | 26 | start(); 27 | 28 | module.exports = fastify; 29 | -------------------------------------------------------------------------------- /test/server.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fastify = require('../server'); 4 | 5 | describe('server test', () => { 6 | afterAll(() => { 7 | fastify.close(); 8 | }); 9 | 10 | test('responds with success on request /', async (done) => { 11 | const response = await fastify.inject({ 12 | method: 'GET', 13 | url: '/' 14 | }); 15 | 16 | expect(response.statusCode).toBe(200); 17 | expect(response.payload).toBe('{"hello":"world"}'); 18 | done(); 19 | }); 20 | }); 21 | --------------------------------------------------------------------------------