Skip to content

Commit 2816fdb

Browse files
authored
refactor(msw): Switch to V2 + Vite (#494)
1 parent 84d33c0 commit 2816fdb

File tree

13 files changed

+162
-67
lines changed

13 files changed

+162
-67
lines changed

msw/.eslintrc.cjs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* This is intended to be a basic starting point for linting in your app.
3+
* It relies on recommended configs out of the box for simplicity, but you can
4+
* and should modify this configuration to best suit your team's needs.
5+
*/
6+
7+
/** @type {import('eslint').Linter.Config} */
8+
module.exports = {
9+
root: true,
10+
parserOptions: {
11+
ecmaVersion: "latest",
12+
sourceType: "module",
13+
ecmaFeatures: {
14+
jsx: true,
15+
},
16+
},
17+
env: {
18+
browser: true,
19+
commonjs: true,
20+
es6: true,
21+
},
22+
ignorePatterns: ["!**/.server", "!**/.client"],
23+
24+
// Base config
25+
extends: ["eslint:recommended"],
26+
27+
overrides: [
28+
// React
29+
{
30+
files: ["**/*.{js,jsx,ts,tsx}"],
31+
plugins: ["react", "jsx-a11y"],
32+
extends: [
33+
"plugin:react/recommended",
34+
"plugin:react/jsx-runtime",
35+
"plugin:react-hooks/recommended",
36+
"plugin:jsx-a11y/recommended",
37+
],
38+
settings: {
39+
react: {
40+
version: "detect",
41+
},
42+
formComponents: ["Form"],
43+
linkComponents: [
44+
{ name: "Link", linkAttribute: "to" },
45+
{ name: "NavLink", linkAttribute: "to" },
46+
],
47+
"import/resolver": {
48+
typescript: {},
49+
},
50+
},
51+
},
52+
53+
// Typescript
54+
{
55+
files: ["**/*.{ts,tsx}"],
56+
plugins: ["@typescript-eslint", "import"],
57+
parser: "@typescript-eslint/parser",
58+
settings: {
59+
"import/internal-regex": "^~/",
60+
"import/resolver": {
61+
node: {
62+
extensions: [".ts", ".tsx"],
63+
},
64+
typescript: {
65+
alwaysTryTypes: true,
66+
},
67+
},
68+
},
69+
extends: [
70+
"plugin:@typescript-eslint/recommended",
71+
"plugin:import/recommended",
72+
"plugin:import/typescript",
73+
],
74+
},
75+
76+
// Node
77+
{
78+
files: [".eslintrc.cjs"],
79+
env: {
80+
node: true,
81+
},
82+
},
83+
],
84+
};

msw/.eslintrc.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

msw/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ node_modules
22

33
/.cache
44
/build
5-
/public/build
65
.env

msw/README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@ We can mock the HTTP calls using MSW, which intercepts the API calls from the se
1919

2020
You can read more about the use cases of MSW [here](https://mswjs.io/docs/#when-to-mock-api)
2121

22-
## Gotchas
23-
24-
MSW currently does not support intercepting requests made by [undici](https://undici.nodejs.org/#/). For local development, Cloudflare Workers and Pages simulates the production environment using [wrangler](https://developers.cloudflare.com/workers/cli-wrangler), which runs [miniflare](https://github.com/cloudflare/miniflare) internally. `Miniflare` implements `fetch` using `undici` instead of `node-fetch`. You can follow this issue [#159](https://github.com/mswjs/interceptors/issues/159) to track the progress.
25-
2622
## Relevant files
2723

28-
- [mocks](./mocks/index.js) - registers the Node HTTP mock server
29-
- [handlers](./mocks/handlers.js) - describes the HTTP mocks
30-
- [root](./app/root.tsx)
24+
- [mocks](./mocks/index.cjs) - registers the Node HTTP mock server
25+
- [handlers](./mocks/handlers.cjs) - describes the HTTP mocks
3126
- [package.json](./package.json)
3227

3328
## Related Links

msw/app/root.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
import type { MetaFunction } from "@remix-run/node";
21
import {
32
Links,
4-
LiveReload,
53
Meta,
64
Outlet,
75
Scripts,
86
ScrollRestoration,
97
} from "@remix-run/react";
108

11-
export const meta: MetaFunction = () => ({
12-
charset: "utf-8",
13-
title: "New Remix App",
14-
viewport: "width=device-width,initial-scale=1",
15-
});
16-
17-
export default function App() {
9+
export function Layout({ children }: { children: React.ReactNode }) {
1810
return (
1911
<html lang="en">
2012
<head>
13+
<meta charSet="utf-8" />
14+
<meta name="viewport" content="width=device-width, initial-scale=1" />
2115
<Meta />
2216
<Links />
2317
</head>
2418
<body>
25-
<Outlet />
19+
{children}
2620
<ScrollRestoration />
2721
<Scripts />
28-
<LiveReload />
2922
</body>
3023
</html>
3124
);
3225
}
26+
27+
export default function App() {
28+
return <Outlet />;
29+
}

msw/mocks/handlers.cjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { http, HttpResponse } = require("msw");
2+
3+
const handlers = [
4+
http.get("https://my-mock-api.com", () => {
5+
return HttpResponse.json({ message: "from msw" });
6+
}),
7+
];
8+
9+
module.exports = handlers;

msw/mocks/handlers.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { setupServer } = require("msw/node");
22

3-
const handlers = require("./handlers");
3+
const handlers = require("./handlers.cjs");
44

55
const server = setupServer(...handlers);
66
server.listen({ onUnhandledRequest: "warn" });
7-
console.info("MSW initialised");
7+
console.info("MSW initialized");

msw/package.json

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
11
{
2+
"name": "msw",
23
"private": true,
34
"sideEffects": false,
5+
"type": "module",
46
"scripts": {
5-
"build": "remix build",
6-
"dev": "binode --require ./mocks -- @remix-run/dev:remix dev",
7-
"start": "remix-serve build",
7+
"build": "remix vite:build",
8+
"dev": "binode --require ./mocks/index.cjs -- @remix-run/dev:remix vite:dev",
9+
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
10+
"start": "remix-serve ./build/server/index.js",
811
"typecheck": "tsc"
912
},
1013
"dependencies": {
11-
"@remix-run/node": "^1.19.3",
12-
"@remix-run/react": "^1.19.3",
13-
"@remix-run/serve": "^1.19.3",
14-
"isbot": "^3.6.5",
14+
"@remix-run/node": "^2.9.2",
15+
"@remix-run/react": "^2.9.2",
16+
"@remix-run/serve": "^2.9.2",
17+
"isbot": "^4.1.0",
1518
"react": "^18.2.0",
1619
"react-dom": "^18.2.0"
1720
},
1821
"devDependencies": {
19-
"@remix-run/dev": "^1.19.3",
20-
"@remix-run/eslint-config": "^1.19.3",
21-
"@types/react": "^18.0.25",
22-
"@types/react-dom": "^18.0.8",
22+
"@remix-run/dev": "^2.9.2",
23+
"@types/react": "^18.2.20",
24+
"@types/react-dom": "^18.2.7",
25+
"@typescript-eslint/eslint-plugin": "^6.7.4",
26+
"@typescript-eslint/parser": "^6.7.4",
2327
"binode": "^1.0.5",
24-
"eslint": "^8.27.0",
25-
"msw": "^0.47.3",
26-
"typescript": "^4.8.4"
28+
"eslint": "^8.38.0",
29+
"eslint-import-resolver-typescript": "^3.6.1",
30+
"eslint-plugin-import": "^2.28.1",
31+
"eslint-plugin-jsx-a11y": "^6.7.1",
32+
"eslint-plugin-react": "^7.33.2",
33+
"eslint-plugin-react-hooks": "^4.6.0",
34+
"msw": "^2.3",
35+
"typescript": "^5.1.6",
36+
"vite": "^5.1.0",
37+
"vite-tsconfig-paths": "^4.2.1"
2738
},
2839
"engines": {
29-
"node": ">=16.0.0"
40+
"node": ">=20.0.0"
3041
}
3142
}

msw/remix.config.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)