Skip to content

Commit

Permalink
chore: improve test speed (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeadTriXz authored Sep 16, 2023
1 parent bcde63a commit 84ad3f4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 62 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:
build:
name: Build and Test
runs-on: ubuntu-latest
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}

steps:
- name: Checkout repository
Expand Down
115 changes: 53 additions & 62 deletions packages/core/tests/Server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { type Server, FastifyServer, StatusCodes } from "../src/index.js";

import { File } from "node:buffer";
import nacl from "tweetnacl";
import type { ServerRequestHandler } from "../src/Server.js";
import type { MockedFunction } from "vitest";

const SERVER_HOST = "localhost";
const SERVER_PORT = 3000;
Expand Down Expand Up @@ -35,26 +37,29 @@ function getSignatureHeaders(privateKey: string, payload?: any): HeadersInit {
describe("FastifyServer", () => {
let server: Server;
let secretKey: string;
let callback: MockedFunction<ServerRequestHandler>;

beforeEach(() => {
beforeAll(async () => {
const pair = nacl.sign.keyPair();
const publicKey = Buffer.from(pair.publicKey).toString("hex");

callback = vi.fn();
secretKey = Buffer.from(pair.secretKey).toString("hex");
server = new FastifyServer({ publicKey });
server.post("/", callback);

await server.listen(SERVER_PORT, SERVER_HOST);
});

afterEach(async () => {
afterAll(async () => {
await server.close();
});

describe("Receive request", () => {
it("should accept requests with a valid key", async () => {
await server
.post("/", (body, respond) => {
return respond({ status: StatusCodes.OK });
})
.listen(SERVER_PORT, SERVER_HOST);
callback.mockImplementation((body, respond) => {
return respond({ status: StatusCodes.OK });
});

const response = await fetch(SERVER_URL, {
method: "POST",
Expand All @@ -65,11 +70,9 @@ describe("FastifyServer", () => {
});

it("should reject requests with an invalid key", async () => {
await server
.post("/", (body, respond) => {
return respond({ status: StatusCodes.OK });
})
.listen(SERVER_PORT, SERVER_HOST);
callback.mockImplementation((body, respond) => {
return respond({ status: StatusCodes.OK });
});

const response = await fetch(SERVER_URL, {
method: "POST",
Expand All @@ -83,11 +86,9 @@ describe("FastifyServer", () => {
});

it("should reject requests without a key", async () => {
await server
.post("/", (body, respond) => {
return respond({ status: StatusCodes.OK });
})
.listen(SERVER_PORT, SERVER_HOST);
callback.mockImplementation((body, respond) => {
return respond({ status: StatusCodes.OK });
});

const response = await fetch(SERVER_URL, {
method: "POST"
Expand All @@ -97,11 +98,9 @@ describe("FastifyServer", () => {
});

it("should receive the right request body", async () => {
await server
.post("/", (body, respond) => {
return respond({ body });
})
.listen(SERVER_PORT, SERVER_HOST);
callback.mockImplementation((body, respond) => {
return respond({ body });
});

const body = { message: "Hello World" };
const response = await fetch(SERVER_URL, {
Expand All @@ -125,16 +124,14 @@ describe("FastifyServer", () => {
const fileContent = "Hello World";
const fileName = "file.txt";

await server
.post("/", async (body, respond) => {
return respond({
files: [{
name: fileName,
data: Buffer.from(fileContent)
}]
});
})
.listen(SERVER_PORT, SERVER_HOST);
callback.mockImplementation((body, respond) => {
return respond({
files: [{
name: fileName,
data: Buffer.from(fileContent)
}]
});
});

const response = await fetch(SERVER_URL, {
method: "POST",
Expand All @@ -157,16 +154,14 @@ describe("FastifyServer", () => {
const fileContent = "Hello World";
const fileName = "file.txt";

await server
.post("/", async (body, respond) => {
return respond({
files: [{
name: fileName,
data: Buffer.from(fileContent)
}]
});
})
.listen(SERVER_PORT, SERVER_HOST);
callback.mockImplementation((body, respond) => {
return respond({
files: [{
name: fileName,
data: Buffer.from(fileContent)
}]
});
});

const response = await fetch(SERVER_URL, {
method: "POST",
Expand All @@ -191,17 +186,15 @@ describe("FastifyServer", () => {

const payload = { message: "Hello World" };

await server
.post("/", async (body, respond) => {
return respond({
body: payload,
files: [{
name: fileName,
data: Buffer.from(fileContent)
}]
});
})
.listen(SERVER_PORT, SERVER_HOST);
callback.mockImplementation((body, respond) => {
return respond({
body: payload,
files: [{
name: fileName,
data: Buffer.from(fileContent)
}]
});
});

const response = await fetch(SERVER_URL, {
method: "POST",
Expand All @@ -218,15 +211,13 @@ describe("FastifyServer", () => {
});

it("should respond with the specified headers", async () => {
await server
.post("/", async (body, respond) => {
return respond({
headers: {
"x-custom-header": "Hello World"
}
});
})
.listen(SERVER_PORT, SERVER_HOST);
callback.mockImplementation((body, respond) => {
return respond({
headers: {
"x-custom-header": "Hello World"
}
});
});

const response = await fetch(SERVER_URL, {
method: "POST",
Expand Down

0 comments on commit 84ad3f4

Please # to comment.