Skip to content

Commit

Permalink
- Added MOCK environment variable/types
Browse files Browse the repository at this point in the history
- Added `msw` package
- Added/Mocked/Intercepted all favicon requests
- Added/Mocked/Intercepted all HEAD/status requests
  • Loading branch information
michaelschwobe committed Nov 23, 2023
1 parent 17a04b3 commit deb2444
Show file tree
Hide file tree
Showing 15 changed files with 637 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ pnpm-*.yaml

# prisma
/prisma/migrations

# msw
/public/mockServiceWorker.*
24 changes: 17 additions & 7 deletions app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ import { RemixBrowser } from "@remix-run/react";
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";

startTransition(() => {
hydrateRoot(
document,
<StrictMode>
<RemixBrowser />
</StrictMode>,
);
async function prepareApp() {
if (ENV.MOCKS) {
const { worker } = await import("../tests/mocks/browser");
return worker.start();
}
return Promise.resolve();
}

prepareApp().then(() => {
startTransition(() => {
hydrateRoot(
document,
<StrictMode>
<RemixBrowser />
</StrictMode>,
);
});
});
13 changes: 13 additions & 0 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,24 @@ import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { PassThrough } from "node:stream";
import { renderToPipeableStream } from "react-dom/server";
import { server } from "../tests/mocks/node";
import { getEnv, init } from "./utils/env.server";

// Initialize typesafe environment variables and global ENV object.
init();
global.ENV = getEnv();

// Enable API mocking.
if (ENV.MOCKS) {
server.listen();
console.info("[MSW] Mocking enabled.");
if (ENV.MODE === "development") {
server.events.on("request:start", ({ request }) => {
console.info(request.method, "[MSW]", request.url);
});
}
}

const ABORT_DELAY = 5_000;

export default function handleRequest(
Expand Down
2 changes: 1 addition & 1 deletion app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function Document({
theme,
}: {
children: React.ReactNode;
env?: Record<string, string>;
env?: Record<string, boolean | number | string>;
theme?: Theme;
}) {
return (
Expand Down
3 changes: 3 additions & 0 deletions app/utils/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const schema = z.object({
NODE_ENV: z.enum(["production", "development", "test"] as const),
DATABASE_URL: z.string(),
SESSION_SECRET: z.string(),
MOCKS: z.enum(["true", "false"] as const).optional(),
});

declare global {
Expand Down Expand Up @@ -39,6 +40,8 @@ export function getEnv() {
return {
MODE: process.env.NODE_ENV,

MOCKS: process.env.MOCKS === "true",

APP_VERSION: packageJSON.version,

/**
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"enforce-unique": "^1.2.0",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
"msw": "^2.0.8",
"prettier": "^3.1.0",
"prettier-plugin-tailwindcss": "^0.4.1",
"tailwindcss": "^3.3.5",
Expand All @@ -98,5 +99,8 @@
"node": ">=21.2.0",
"pnpm": ">=8.10.2"
},
"packageManager": "[email protected]"
}
"packageManager": "[email protected]",
"msw": {
"workerDirectory": "public"
}
}
Loading

0 comments on commit deb2444

Please sign in to comment.