Skip to content

Commit

Permalink
feat: running test server from the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
4lejandrito committed Oct 13, 2023
1 parent e802a51 commit 62c9387
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 147 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FETCHBOOK_TEST=true
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ web_modules/

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
4 changes: 0 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Expect } from "expect";

export type FetchStory = {
name: string;
url: string;
Expand All @@ -13,5 +11,3 @@ export type FetchStory = {
before?: FetchStory[];
after?: FetchStory[];
};

export const expect: Expect;
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
module.exports = {
expect: require("expect").expect,
};
module.exports = {};
8 changes: 7 additions & 1 deletion lib/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FetchStory } from "fetchbook";
import picocolors from "picocolors";
import expect from "expect";
import serialize from "./serialize";
import testServer from "./test-server";

async function readBody(source: Request | Response) {
if (source.headers.get("content-type")?.includes("application/json")) {
Expand All @@ -20,7 +21,12 @@ export default async function run(
let body: string | undefined;
if (!options.dryRun) {
try {
response = await fetch(request.clone());
if (process.env.FETCHBOOK_TEST) await testServer.start();
try {
response = await fetch(request.clone());
} finally {
if (process.env.FETCHBOOK_TEST) testServer.stop();
}
body = await readBody(response);
if (story.expect) {
expect({
Expand Down
2 changes: 1 addition & 1 deletion test/server.ts → lib/test-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
start: async () =>
new Promise<void>((resolve) => {
const app = jsonServer.create();
app.use(jsonServer.defaults());
app.use(jsonServer.defaults({ logger: false }));
app.use(
jsonServer.router({
posts: [
Expand Down
Loading

0 comments on commit 62c9387

Please sign in to comment.