Skip to content

Commit

Permalink
initial testing setup
Browse files Browse the repository at this point in the history
  • Loading branch information
moonmeister committed Oct 9, 2021
1 parent 61e6878 commit 6390507
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 12 deletions.
19 changes: 19 additions & 0 deletions packages/gatsby-plugin-fastify/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: [`<rootDir>/test-site/`],
// The test environment that will be used for testing
testEnvironment: `node`,
};
7 changes: 6 additions & 1 deletion packages/gatsby-plugin-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -38,15 +40,18 @@
"@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",
"fastify-compress": "^3.6.0",
"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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test Gatsby Server Should work 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"utf-8\\">
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\">
<title>Gatsby Plugin Fastify</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>"
`;
14 changes: 14 additions & 0 deletions packages/gatsby-plugin-fastify/src/__tests__/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gatsby Plugin Fastify</title>
</head>

<body>
<h1>Hello World</h1>
</body>

</html>
74 changes: 74 additions & 0 deletions packages/gatsby-plugin-fastify/src/__tests__/serve.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
4 changes: 3 additions & 1 deletion packages/gatsby-plugin-fastify/src/plugins/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Partial<FastifyStaticOptions>> = 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 (
Expand Down
18 changes: 10 additions & 8 deletions packages/gatsby-plugin-fastify/src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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;
}
17 changes: 15 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 6390507

Please sign in to comment.