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

feat(coral): Setup msw to mock api [#144] #172

Merged
merged 20 commits into from
Nov 3, 2022
Merged

Conversation

programmiri
Copy link
Contributor

@programmiri programmiri commented Nov 2, 2022

Note

I created a follow up ticket for this epic to use the mocked API in Login instead of HomePage. I removed that from the scope of this PR because it already has a lot of changes and I didn't want to make it too big.

About this change - What it does

  • introduces msw library to mock apis during development and in tests
  • Adds a mocked api for user authentication
  • uses mocked api in HomePage
  • adds test for HomePage to proof test setup for msw
  • ⚠️ TODO: add documentation about usage of msw

Resolves: #144

Why this way

  • I added a public folder with favicon because I first hat the mockServiceWorker in public. That was removed later, but the commit was one of the first, so I let it stay in.

There where a few tweaks needed to make it work properly with Vite:

  • mockServiceWorker.js is not in public since everything in public is in prod build
  • main.tsx follows solutions from this discussion

Note: I checked the build files and msw is correctly not introduced there 🎉

@programmiri programmiri linked an issue Nov 2, 2022 that may be closed by this pull request
@programmiri programmiri force-pushed the 144-setup-msw-as-mock-api branch 2 times, most recently from 924ffdd to a9a4532 Compare November 2, 2022 12:57
@programmiri programmiri marked this pull request as ready for review November 2, 2022 14:02
@programmiri programmiri requested review from a team as code owners November 2, 2022 14:02
@programmiri programmiri force-pushed the 144-setup-msw-as-mock-api branch 2 times, most recently from f874a80 to 52337df Compare November 2, 2022 14:19
@programmiri programmiri self-assigned this Nov 2, 2022
Copy link
Contributor

@SmuliS SmuliS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of improvement suggestion of following areas:

  • Implement deferred mounting for browser environment
  • Define mocked handlers closer where they are used / needed instead in the API domain.
  • Move api-mocks from domain into services.

Also, we could discuss about if the mock server used in tests should have handlers that catch everything and raise an exception. That would prevent all outgoing API requests and nudge the developer to implement a proper mock. The following handlers could be initialized in the startup:

rest.get('*', throwError),
rest.put('*', throwError),
rest.post('*', throwError),
rest.delete('*', throwError),

coral/src/app/main.tsx Outdated Show resolved Hide resolved
coral/src/domain/api-mocks/server.ts Outdated Show resolved Hide resolved
coral/src/app/pages/index.test.tsx Show resolved Hide resolved
coral/package.json Outdated Show resolved Hide resolved
coral/package.json Show resolved Hide resolved
coral/mockServiceWorker.js Show resolved Hide resolved
coral/index.html Outdated Show resolved Hide resolved
coral/src/domain/api-mocks/browser.ts Outdated Show resolved Hide resolved
@programmiri programmiri changed the title 144 setup msw as mock api feat(coral): Setup msw to mock api [#144] Nov 3, 2022
@programmiri programmiri force-pushed the 144-setup-msw-as-mock-api branch from efebc8c to 427c00e Compare November 3, 2022 10:35
- `mockServiceWorker.js` is not in `public` to avoid it being build for prod
- `main.tsx` required a special handling to make sure msw is not build for prod
- to proof that setup works for app and tests.
- files and directories match architecture plan closer
- remove unneeded handlers in mock handles
- add typing
- bypass warning for HMR, setting was missing before
(https://mswjs.io/docs/api/setup-worker/start#onunhandledrequest)
- updated msw directory in package.json
- expose a msw instance in global window to enable usage in components
- move mocked api calls to actual api calls
- mock API call in component HomePage directly instead of global
- add custom type for window
- add types for usage of msw
- only run mocked api call in component when msw is set on window
  (indicating a worker running in browser env)
- update test for HomePage
@programmiri programmiri force-pushed the 144-setup-msw-as-mock-api branch from 427c00e to 0a955cd Compare November 3, 2022 10:55
Copy link
Contributor

@SmuliS SmuliS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of more comments, but I think the big picture is now starting to look good 👍

coral/index.html Outdated Show resolved Hide resolved
coral/src/app/main.tsx Outdated Show resolved Hide resolved
coral/src/app/pages/index.tsx Outdated Show resolved Hide resolved
coral/src/domain/auth-user/auth-user-api.msw.ts Outdated Show resolved Hide resolved
coral/src/services/api-mocks/server.ts Outdated Show resolved Hide resolved
@programmiri
Copy link
Contributor Author

programmiri commented Nov 3, 2022

Note
I added a follow up ticket: 🪸 update test setup to prevent api calls#178

Copy link
Contributor

@SmuliS SmuliS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets extend the global window type with msw. After that I will do a final testing on my end and lets get this in :shipit: .

coral/index.html Outdated Show resolved Hide resolved
@programmiri programmiri force-pushed the 144-setup-msw-as-mock-api branch from eaba02e to 1be693b Compare November 3, 2022 15:26
@SmuliS SmuliS merged commit 6f1c6bd into main Nov 3, 2022
@SmuliS SmuliS deleted the 144-setup-msw-as-mock-api branch November 3, 2022 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🪸 Setup MSW as mock API
2 participants