Skip to content

Commit

Permalink
tests for all three cases per example
Browse files Browse the repository at this point in the history
  • Loading branch information
jbolda committed Jun 7, 2024
1 parent 6ed87f6 commit 19bad96
Show file tree
Hide file tree
Showing 6 changed files with 524 additions and 0 deletions.
93 changes: 93 additions & 0 deletions packages/foundation/tests/extensive-startEveryParallel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { simulation } from "../example/extensiveServer";

/*
* In this test file, we expect each test to start up and shut down it's own server.
* This means that all of the tests in this file can run completely in parallel, but
* It requires a helper to determine a different port per test, see `getBaseUrl()`.
*/

let basePort = 9050;
let host = "http://localhost";
let getPort = (task) => {
let taskID = task.id;
let endNumberAsString = taskID.split("_").at(-1);
if (!endNumberAsString)
throw new Error(
`taskID ${taskID} of ${task.name} does not end with an integer`
);
let endNumber = parseInt(endNumberAsString, 10);
return basePort + endNumber;
};
let getBaseUrl = (task) => {
return `${host}:${getPort(task)}`;
};

describe("single file server - startup in every test", () => {
let server;
beforeEach(async (context) => {
let app = await simulation;
server = await app.listen(getPort(context.task));
});
afterEach(() => {
server.close();
});

it("returns", async ({ task }) => {
console.dir(getBaseUrl(task));
let request = await fetch(`${getBaseUrl(task)}/api/pets`);
let response = await request.json();
expect(response).toEqual([
{ id: 1, name: "Garfield" },
{ id: 2, name: "Odie" },
]);
});

it("adds one dog", async ({ task }) => {
console.dir(getBaseUrl(task));
// note calling this endpoint increments the number of dogs expected
await fetch(`${getBaseUrl(task)}/api/more-dogs`);

let request = await fetch(`${getBaseUrl(task)}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 1 });
});

it("adds five dogs", async ({ task }) => {
console.dir(getBaseUrl(task));
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);

let request = await fetch(`${getBaseUrl(task)}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 5 });
});

it("adds three dogs", async ({ task }) => {
console.dir(getBaseUrl(task));
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);

let request = await fetch(`${getBaseUrl(task)}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 3 });
});

it("adds six dogs", async ({ task }) => {
console.dir(getBaseUrl(task));
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);

let request = await fetch(`${getBaseUrl(task)}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 6 });
});
});
84 changes: 84 additions & 0 deletions packages/foundation/tests/extensive-startEverySequential.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { simulation } from "../example/extensiveServer";

/*
* In this test file, we expect each test to start up and shut down the server.
* As such, we use `sequential` ensure these tests run separately, one after another.
*/

let basePort = 9049;
let host = "http://localhost";
let url = `${host}:${basePort}`;

describe.sequential("single file server - startup in every test", () => {
let server;
beforeEach(async () => {
let app = await simulation;
server = await app.listen(basePort);
});
afterEach(async () => {
await server.ensureClose();
});

it("returns", async () => {
let request = await fetch(`${url}/api/pets`);
let response = await request.json();
expect(response).toEqual([
{ id: 1, name: "Garfield" },
{ id: 2, name: "Odie" },
]);
});

it("returns again", async () => {
let request = await fetch(`${url}/api/pets`);
let response = await request.json();
expect(response).toEqual([
{ id: 1, name: "Garfield" },
{ id: 2, name: "Odie" },
]);
});

it("adds one dog", async () => {
// note calling this endpoint increments the number of dogs expected
await fetch(`${url}/api/more-dogs`);

let request = await fetch(`${url}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 1 });
});

it("adds five dogs", async () => {
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);

let request = await fetch(`${url}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 5 });
});

it("adds three dogs", async () => {
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);

let request = await fetch(`${url}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 3 });
});

it("adds six dogs", async () => {
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);

let request = await fetch(`${url}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 6 });
});
});
85 changes: 85 additions & 0 deletions packages/foundation/tests/extensive-startOnce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { simulation } from "../example/extensiveServer";

/*
* In this test file, we start the server once for the whole suite.
* The server state ends up being shared between the tests.
* With `sequential`, we note the shared state increments with each test.
*/

let basePort = 9048;
let host = "http://localhost";
let url = `${host}:${basePort}`;

describe.sequential("single file server - startup in every test", () => {
let server;
beforeAll(async () => {
let app = await simulation;
server = await app.listen(basePort);
});
afterAll(async () => {
await server.ensureClose();
});

it("returns", async () => {
let request = await fetch(`${url}/api/pets`);
let response = await request.json();
expect(response).toEqual([
{ id: 1, name: "Garfield" },
{ id: 2, name: "Odie" },
]);
});

it("returns again", async () => {
let request = await fetch(`${url}/api/pets`);
let response = await request.json();
expect(response).toEqual([
{ id: 1, name: "Garfield" },
{ id: 2, name: "Odie" },
]);
});

it("adds one dog", async () => {
// note calling this endpoint increments the number of dogs expected
await fetch(`${url}/api/more-dogs`);

let request = await fetch(`${url}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 1 });
});

it("adds five more dogs", async () => {
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);

let request = await fetch(`${url}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 6 });
});

it("adds three more dogs", async () => {
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);

let request = await fetch(`${url}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 9 });
});

it("adds six more dogs", async () => {
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);
await fetch(`${url}/api/more-dogs`);

let request = await fetch(`${url}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 15 });
});
});
93 changes: 93 additions & 0 deletions packages/foundation/tests/singleFile-startEveryParallel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { simulation } from "../example/singleFileServer";

/*
* In this test file, we expect each test to start up and shut down it's own server.
* This means that all of the tests in this file can run completely in parallel, but
* It requires a helper to determine a different port per test, see `getBaseUrl()`.
*/

let basePort = 9010;
let host = "http://localhost";
let getPort = (task) => {
let taskID = task.id;
let endNumberAsString = taskID.split("_").at(-1);
if (!endNumberAsString)
throw new Error(
`taskID ${taskID} of ${task.name} does not end with an integer`
);
let endNumber = parseInt(endNumberAsString, 10);
return basePort + endNumber;
};
let getBaseUrl = (task) => {
return `${host}:${getPort(task)}`;
};

describe("single file server - startup in every test", () => {
let server;
beforeEach(async (context) => {
let app = await simulation;
server = await app.listen(getPort(context.task));
});
afterEach(() => {
server.close();
});

it("returns", async ({ task }) => {
console.dir(getBaseUrl(task));
let request = await fetch(`${getBaseUrl(task)}/api/pets`);
let response = await request.json();
expect(response).toEqual([
{ id: 1, name: "Garfield" },
{ id: 2, name: "Odie" },
]);
});

it("adds one dog", async ({ task }) => {
console.dir(getBaseUrl(task));
// note calling this endpoint increments the number of dogs expected
await fetch(`${getBaseUrl(task)}/api/more-dogs`);

let request = await fetch(`${getBaseUrl(task)}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 1 });
});

it("adds five dogs", async ({ task }) => {
console.dir(getBaseUrl(task));
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);

let request = await fetch(`${getBaseUrl(task)}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 5 });
});

it("adds three dogs", async ({ task }) => {
console.dir(getBaseUrl(task));
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);

let request = await fetch(`${getBaseUrl(task)}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 3 });
});

it("adds six dogs", async ({ task }) => {
console.dir(getBaseUrl(task));
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);
await fetch(`${getBaseUrl(task)}/api/more-dogs`);

let request = await fetch(`${getBaseUrl(task)}/api/dogs`);
let response = await request.json();
expect(response).toEqual({ dogs: 6 });
});
});
Loading

0 comments on commit 19bad96

Please sign in to comment.