Skip to content

Commit 73cecf9

Browse files
committed
feat(unit-tests): mocked service worker
Added mocked services worker for API mocking. Removed json-server in favor of msw. Added msw handlers that persist the data to localstorage. Installed dependencies: ``` yarn add msw @mswjs/data --dev npx msw init public/ --save ```
1 parent 50ae5e5 commit 73cecf9

File tree

23 files changed

+984
-557
lines changed

23 files changed

+984
-557
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
REACT_APP_POSTS_API_BASE_URL= "http://localhost:8000/posts"
1+
REACT_APP_API_BASE_URL= "http://localhost:8000"
2+
REACT_APP_MSW_ENABLED=true

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
118118
* [Husky](https://typicode.github.io/husky/#/)
119119
* [commitlint](https://commitlint.js.org/#/) - Lint commit messages
120120
* [Standard Version](https://github.com/conventional-changelog/standard-version) - A utility for versioning using semver and CHANGELOG generation powered by Conventional Commits.
121-
121+
* [Mock Service Worker](https://mswjs.io/)
122122
* [Redux Saga Test Plan](https://github.com/jfairbank/redux-saga-test-plan) - Redux Saga Test Plan aims to embrace both integration testing and unit testing approaches to make testing your sagas easy.
123123

124124
## Additional Links
@@ -131,4 +131,6 @@ npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
131131
* [MUI - theme switcher](https://mui.com/customization/dark-mode/)
132132
* [Level up your CSS linting using Stylelint](https://blog.logrocket.com/using-stylelint-improve-lint-css-scss-sass/)
133133
* [Create React App: A quick setup guide](https://blog.logrocket.com/create-react-app-a-quick-setup-guide-b812f0aad03c/)
134-
* [TypeScript Deep Dive](https://basarat.gitbook.io/typescript/)
134+
* [TypeScript Deep Dive](https://basarat.gitbook.io/typescript/)
135+
* [Bulletproof React](https://github.com/alan2207/bulletproof-react)
136+
* [Jest cheat sheet](https://github.com/sapegin/jest-cheat-sheet)

package.json

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"@reduxjs/toolkit": "^1.8.0",
1313
"history": "^5.3.0",
1414
"i18next": "^21.6.14",
15-
"json-server": "^0.17.0",
1615
"react": "^17.0.2",
1716
"react-dom": "^17.0.2",
1817
"react-hook-form": "^7.27.1",
@@ -30,11 +29,10 @@
3029
},
3130
"scripts": {
3231
"start": "react-scripts start",
33-
"start:backend": "json-server --watch db.json --port 8000",
3432
"build": "react-scripts build",
3533
"eject": "react-scripts eject",
3634
"test": "react-scripts test",
37-
"coverage": "react-scripts test --coverage",
35+
"coverage": "react-scripts test --coverage --watchAll=false",
3836
"format": "prettier --write 'src'",
3937
"eslint": "eslint --ext ts,tsx",
4038
"lint": "yarn lint:css && yarn eslint src",
@@ -66,8 +64,7 @@
6664
"devDependencies": {
6765
"@commitlint/cli": "16.2.1",
6866
"@commitlint/config-conventional": "16.2.1",
69-
"@typescript-eslint/eslint-plugin": "5.14.0",
70-
"@typescript-eslint/parser": "5.14.0",
67+
"@mswjs/data": "^0.9.2",
7168
"@testing-library/jest-dom": "^5.14.1",
7269
"@testing-library/react": "^12.0.0",
7370
"@testing-library/user-event": "^13.2.1",
@@ -76,6 +73,8 @@
7673
"@types/react": "^17.0.20",
7774
"@types/react-dom": "^17.0.9",
7875
"@types/redux-logger": "^3.0.9",
76+
"@typescript-eslint/eslint-plugin": "5.14.0",
77+
"@typescript-eslint/parser": "5.14.0",
7978
"eslint": "8.11.0",
8079
"eslint-config-airbnb": "19.0.4",
8180
"eslint-config-airbnb-typescript": "16.1.2",
@@ -88,6 +87,7 @@
8887
"eslint-plugin-react-hooks": "4.3.0",
8988
"husky": "7.0.4",
9089
"lint-staged": "12.3.5",
90+
"msw": "^0.39.2",
9191
"postcss-scss": "4.0.3",
9292
"prettier": "2.5.1",
9393
"redux-saga-test-plan": "^4.0.4",
@@ -98,9 +98,20 @@
9898
"stylelint-config-styled-components": "0.1.1",
9999
"stylelint-processor-styled-components": "1.10.0"
100100
},
101+
"jest": {
102+
"collectCoverageFrom": [
103+
"src/**/*.{js,jsx,ts,tsx}",
104+
"!src/test/**",
105+
"!src/index.tsx",
106+
"!src/reportWebVitals.ts"
107+
]
108+
},
101109
"lint-staged": {
102110
"src/*.{tsx,ts,js,jsx}": [
103111
"yarn lint"
104112
]
113+
},
114+
"msw": {
115+
"workerDirectory": "public"
105116
}
106117
}

0 commit comments

Comments
 (0)