Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(msw): use TS mock files #539

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions msw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ You can read more about the use cases of MSW [here](https://mswjs.io/docs/#when-

## Relevant files

- [mocks](./mocks/index.cjs) - registers the Node HTTP mock server
- [handlers](./mocks/handlers.cjs) - describes the HTTP mocks
- [mocks](./mocks/index.ts) - registers the Node HTTP mock server
- [handlers](./mocks/handlers.ts) - describes the HTTP mocks
- [package.json](./package.json)

## Related Links
Expand Down
9 changes: 0 additions & 9 deletions msw/mocks/handlers.cjs

This file was deleted.

8 changes: 8 additions & 0 deletions msw/mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { HttpHandler } from "msw";
import { http, HttpResponse } from "msw";

export const handlers = [
http.get("https://my-mock-api.com", () => {
return HttpResponse.json({ message: "from msw" });
}),
] satisfies HttpHandler[];
5 changes: 2 additions & 3 deletions msw/mocks/index.cjs → msw/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { setupServer } = require("msw/node");

const handlers = require("./handlers.cjs");
import { setupServer } from "msw/node";
import { handlers } from "./handlers.js";

const server = setupServer(...handlers);
server.listen({ onUnhandledRequest: "warn" });
Expand Down
3 changes: 2 additions & 1 deletion msw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"build": "remix vite:build",
"dev": "binode --require ./mocks/index.cjs -- @remix-run/dev:remix vite:dev",
"dev": "binode --import=tsx --import ./mocks/index.ts -- @remix-run/dev:remix vite:dev",
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
"start": "remix-serve ./build/server/index.js",
"typecheck": "tsc"
Expand All @@ -32,6 +32,7 @@
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"msw": "^2.3",
"tsx": "^4.19.1",
"typescript": "^5.1.6",
"vite": "^5.1.0",
"vite-tsconfig-paths": "^4.2.1"
Expand Down