diff --git a/packages/gatsby-plugin-fastify/jest.config.js b/packages/gatsby-plugin-fastify/jest.config.js new file mode 100644 index 000000000..db1c85abf --- /dev/null +++ b/packages/gatsby-plugin-fastify/jest.config.js @@ -0,0 +1,19 @@ +// For a detailed explanation regarding each configuration property, visit: +// https://jestjs.io/docs/en/configuration.html + +module.exports = { + // Automatically clear mock calls and instances between every test + clearMocks: true, + + // A set of global variables that need to be available in all test environments + globals: { + __PATH_PREFIX__: ``, + }, + + // An array of directory names to be searched recursively up from the requiring module's location + moduleDirectories: [`node_modules`], + + modulePathIgnorePatterns: [`/test-site/`], + // The test environment that will be used for testing + testEnvironment: `node`, +}; diff --git a/packages/gatsby-plugin-fastify/package.json b/packages/gatsby-plugin-fastify/package.json index e6d6bf6bb..01074fd18 100644 --- a/packages/gatsby-plugin-fastify/package.json +++ b/packages/gatsby-plugin-fastify/package.json @@ -25,6 +25,8 @@ }, "scripts": { "build": "babel src --out-dir . --ignore \"**/__tests__,**/*.d.ts\" --extensions \".ts,.js\"", + "test": "jest --detectOpenHandles", + "test:watch": "jest --watch", "watch": "yarn build --watch" }, "dependencies": { @@ -38,7 +40,9 @@ "@babel/cli": "^7.15.7", "@babel/core": "^7.15.8", "@types/fs-extra": "^9.0.13", + "@types/jest": "^27.0.2", "@types/node": "^14.17.21", + "babel-jest": "^27.2.5", "babel-preset-gatsby-package": "^1.14.0", "cross-env": "^7.0.3", "fastify": "^3.22.0", @@ -46,7 +50,8 @@ "fastify-plugin": "^3.0.0", "fastify-static": "^4.4.0", "gatsby": "^3.14.2", - "gatsby-plugin-utils": "^1.14.0" + "gatsby-plugin-utils": "^1.14.0", + "jest": "^27.2.5" }, "peerDependencies": { "fastify": "^3.19.0", diff --git a/packages/gatsby-plugin-fastify/src/__tests__/__snapshots__/serve.js.snap b/packages/gatsby-plugin-fastify/src/__tests__/__snapshots__/serve.js.snap new file mode 100644 index 000000000..605614c36 --- /dev/null +++ b/packages/gatsby-plugin-fastify/src/__tests__/__snapshots__/serve.js.snap @@ -0,0 +1,18 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Test Gatsby Server Should work 1`] = ` +" + + + + + + Gatsby Plugin Fastify + + + +

Hello World

+ + +" +`; diff --git a/packages/gatsby-plugin-fastify/src/__tests__/public/index.html b/packages/gatsby-plugin-fastify/src/__tests__/public/index.html new file mode 100644 index 000000000..cf3c3aeb5 --- /dev/null +++ b/packages/gatsby-plugin-fastify/src/__tests__/public/index.html @@ -0,0 +1,14 @@ + + + + + + + Gatsby Plugin Fastify + + + +

Hello World

+ + + \ No newline at end of file diff --git a/packages/gatsby-plugin-fastify/src/__tests__/serve.js b/packages/gatsby-plugin-fastify/src/__tests__/serve.js new file mode 100644 index 000000000..df2d0ccee --- /dev/null +++ b/packages/gatsby-plugin-fastify/src/__tests__/serve.js @@ -0,0 +1,74 @@ +const { gatsbyServer } = require("../serve"); +const { ConfigKeyEnum, setConfig } = require("../utils/config"); + +jest.mock("../utils/constants", () => ({ + PATH_TO_PUBLIC: __dirname + "/public/", +})); + +function createCliConfig({ host, port, verbose, open }) { + return { + host, + h: host, + port, + p: port, + verbose, + v: verbose, + open, + o: open, + }; +} + +describe(`Test Gatsby Server`, () => { + beforeAll(() => { + setConfig( + ConfigKeyEnum.CLI, + createCliConfig({ + port: 3000, + host: "127.0.0.1", + verbose: false, + open: false, + }), + ); + + setConfig(ConfigKeyEnum.SERVER, { + clientSideRoutes: [], + redirects: [], + prefix: "", + functions: [], + }); + }); + + it(`Should 404`, async () => { + setConfig(ConfigKeyEnum.SERVER, { + clientSideRoutes: [], + redirects: [], + prefix: "", + functions: [], + }); + + const fastify = await gatsbyServer(); + + const response = await fastify.inject({ + url: "/badRoute", + method: "GET", + }); + + await fastify.close(); + + expect(response.statusCode).toEqual(404); + }); + + it(`Should work`, async () => { + const fastify = await gatsbyServer(); + + const response = await fastify.inject({ + url: "/", + method: "GET", + }); + + await fastify.close(); + + expect(response.statusCode).toEqual(200); + expect(response.payload).toMatchSnapshot(); + }); +}); diff --git a/packages/gatsby-plugin-fastify/src/plugins/static.ts b/packages/gatsby-plugin-fastify/src/plugins/static.ts index ebc69d6f3..e3cf5f219 100644 --- a/packages/gatsby-plugin-fastify/src/plugins/static.ts +++ b/packages/gatsby-plugin-fastify/src/plugins/static.ts @@ -3,11 +3,13 @@ import fastifyStatic, { FastifyStaticOptions } from "fastify-static"; import fp from "fastify-plugin"; import path from "path"; import { isMatch } from "picomatch"; +import { PATH_TO_PUBLIC } from "../utils/constants"; export const handleStatic: FastifyPluginAsync> = fp( async (fastify, opts) => { + console.log("path to public", path.resolve(PATH_TO_PUBLIC)); fastify.register(fastifyStatic, { - root: path.resolve("./public"), + root: path.resolve(PATH_TO_PUBLIC), redirect: true, setHeaders: (reply, path, _stat) => { if ( diff --git a/packages/gatsby-plugin-fastify/src/serve.ts b/packages/gatsby-plugin-fastify/src/serve.ts index fa325f820..953745f0f 100755 --- a/packages/gatsby-plugin-fastify/src/serve.ts +++ b/packages/gatsby-plugin-fastify/src/serve.ts @@ -3,7 +3,7 @@ import Fastify from "fastify"; import { getConfig } from "./utils/config"; import open from "open"; -export function gatsbyServer() { +export async function gatsbyServer() { const { cli: { port, host, open: openBrowser }, server: { prefix }, @@ -13,16 +13,18 @@ export function gatsbyServer() { console.info("Registered Gatsby @ ", prefix || "/"); - fastify.register(serveGatsby); + await fastify.register(serveGatsby); - fastify.listen(port, host, (err, listeningOn) => { - if (err) { - console.error(err); - process.exit(1); - } + try { + const listeningOn = await fastify.listen(port, host); console.log(`listening @ ${listeningOn}`); if (openBrowser) open(listeningOn); - }); + } catch (err) { + console.error("Failed to start Fastify err"); + process.exit(1); + } + + return fastify; } diff --git a/yarn.lock b/yarn.lock index 955236f4b..6fe305959 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4485,6 +4485,16 @@ __metadata: languageName: node linkType: hard +"@types/jest@npm:^27.0.2": + version: 27.0.2 + resolution: "@types/jest@npm:27.0.2" + dependencies: + jest-diff: ^27.0.0 + pretty-format: ^27.0.0 + checksum: 814ad5f5d2f277849f47e52906da4b745758e555630fc8cb46a071bde648eefeffb1b35710c530a8cea7fc4ea7c1d813812c120484bf7902ab6c5e473cdd49c9 + languageName: node + linkType: hard + "@types/json-patch@npm:0.0.30": version: 0.0.30 resolution: "@types/json-patch@npm:0.0.30" @@ -10329,7 +10339,9 @@ __metadata: "@babel/core": ^7.15.8 "@babel/runtime": ^7.15.4 "@types/fs-extra": ^9.0.13 + "@types/jest": ^27.0.2 "@types/node": ^14.17.21 + babel-jest: ^27.2.5 babel-preset-gatsby-package: ^1.14.0 cross-env: ^7.0.3 fastify: ^3.22.0 @@ -10339,6 +10351,7 @@ __metadata: fs-extra: ^10.0.0 gatsby: ^3.14.2 gatsby-plugin-utils: ^1.14.0 + jest: ^27.2.5 open: ^8.3.0 picomatch: ^2.3.0 yargs: ^17.2.1 @@ -13103,7 +13116,7 @@ __metadata: languageName: node linkType: hard -"jest-diff@npm:^27.2.5": +"jest-diff@npm:^27.0.0, jest-diff@npm:^27.2.5": version: 27.2.5 resolution: "jest-diff@npm:27.2.5" dependencies: @@ -17342,7 +17355,7 @@ __metadata: languageName: node linkType: hard -"pretty-format@npm:^27.2.5": +"pretty-format@npm:^27.0.0, pretty-format@npm:^27.2.5": version: 27.2.5 resolution: "pretty-format@npm:27.2.5" dependencies: