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

Upgrade msw to latest #1100

Merged
merged 33 commits into from
Apr 11, 2023
Merged

Upgrade msw to latest #1100

merged 33 commits into from
Apr 11, 2023

Conversation

vmarta
Copy link
Contributor

@vmarta vmarta commented Mar 31, 2023

This PR does 2 main things:

  • upgraded msw to the next major version v1 ← 5% of the work.. it was quite straightforward, thankfully.
  • fixed checkout, cart, and AppConfig tests that were failing

The only breaking change with msw v1 does not affect us because retail-react-app does not use those types in Typescript.

Another good thing is that some tests that were intermittently failing fail consistently now across CI and locally. So it’s now easier to reproduce the issues and debug them. For example, the AppConfig test with its error "Test suite failed to run. Call retries were exceeded".

  • all the tests passing for a few consecutive rounds

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • (change1)

How to Test-Drive This PR

  • (step1)

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

vmarta added 27 commits March 31, 2023 16:16
My guess of why there was an error is that there's nothing left to wait for. Perhaps jest already shuts down the test, while react-testing-library is waiting to see more.

Other people out there have experienced the same error: testing-library/react-testing-library#1027 (comment)
Not sure why it seems to be timing out. And different results when only that single test is run vs running the whole test file.
@@ -61,7 +61,7 @@
"jsonwebtoken": "^9.0.0",
"jwt-decode": "^3.1.2",
"lodash": "^4.17.21",
"msw": "^0.28.1",
"msw": "^1.2.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For context: v0.x was up to 0.49, whereas we were on 0.28. So we were behind by a lot.

// Let's not close the server and see how things goes.
// We can revisit this.
// global.server.close()
global.server.close()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From what I see, bringing it back does not seem to affect anything. But it was the recommended way by msw documentation.

// Otherwise, the test would end prematurely before our component has finished its business
// (for example: commerce-sdk-react Provider needs to finish its useEffect for `auth.ready()`)
await waitFor(() => {
expect(window.localStorage.setItem).toHaveBeenCalled()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Finally this AppConfig test is fixed 😌 Previously it failed every time on my machine, and it was failing intermittently on CI.

expect(within(summary).getAllByText(/61.43/i).length).toEqual(2)

expect(screen.getByTestId('sf-cart-skeleton')).toBeInTheDocument()
expect(screen.queryByTestId('sf-cart-container')).not.toBeInTheDocument()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified this test by checking for the skeletons only. Checking for the other things has already been done by the other tests in this file.

@@ -293,15 +286,33 @@ describe('Remove item from cart', function () {
)
})
test('Can remove item from the cart', async () => {
if (process.env.CI) {
console.log('--- skipping this test only in CI because it has proven to be flaky there')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This cart test is annoyingly flaky. I couldn't figure out why a simple findBy query can lead to a timeout. Have tried various things.

Anyways, I decided to conditionally skip this test if it runs on CI.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I'd rather we just test.skip it than execute it conditionally on CI

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll skip the test.

beforeEach(() => {
global.server.use(
rest.get('*/orders/:orderId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockOrder))
}),
rest.get('*/products', (req, res, ctx) => {
return res(ctx.delay(0), ctx.json(mockProducts))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We forgot to mock this endpoint. Now it is there.

const [selectedAddressId, setSelectedAddressId] = useState(false)
const hasSavedAddresses = customer.addresses?.length > 0
const [isEditingAddress, setIsEditingAddress] = useState(false)
const [selectedAddressId, setSelectedAddressId] = useState(undefined)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The changes in this file are to address the scenario when the current customer data is not fully available yet. This scenario leads to an intermittent bug that I've logged in GUS ticket W-12779702.

Previously it was hard to reproduce the bug locally. But now the checkout tests can reproduce it consistently.

@vmarta vmarta marked this pull request as ready for review April 6, 2023 21:15
@vmarta vmarta requested a review from a team as a code owner April 6, 2023 21:15
@vmarta vmarta added the ready for review PR is ready to be reviewed label Apr 6, 2023
bfeister
bfeister previously approved these changes Apr 6, 2023
Copy link
Collaborator

@bfeister bfeister left a comment

Choose a reason for hiding this comment

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

one minor comment, not blocking but I think differing test runs between CI and local test runs is a "slippery slope"

@@ -105,11 +105,12 @@ const ShippingAddressSelection = ({
onSubmit = async () => null
}) => {
const {formatMessage} = useIntl()
const {data: customer} = useCurrentCustomer()
const {data: customer, isLoading, isFetching} = useCurrentCustomer()
const isLoadingRegisteredCustomer = isLoading && isFetching
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this look like we are looking for isInitialLoading? https://tanstack.com/query/v4/docs/react/guides/disabling-queries#isinitialloading

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, it's the same as isInitialLoading. Forgot about that.

In the normal cases, I would switch to isInitialLoading. But with useCurrentCustomer, I feel that it does not really represent what really happens. The query is enabled only if the customer is registered. So it's clearer to me if I name it isLoadingRegisteredCustomer.

alexvuong
alexvuong previously approved these changes Apr 10, 2023
@vmarta vmarta dismissed stale reviews from alexvuong and bfeister via 1589d87 April 10, 2023 22:22
@@ -292,16 +285,30 @@ describe('Remove item from cart', function () {
})
)
})
test('Can remove item from the cart', async () => {
test.skip('Can remove item from the cart', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As per Brian's comment, I decided to skip this test: #1100 (comment)

@bfeister bfeister requested review from bfeister and alexvuong April 11, 2023 17:07
Copy link
Collaborator

@bfeister bfeister left a comment

Choose a reason for hiding this comment

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

Thanks so much for helping improve our testing @vmarta 💪 🏅

LGTM

@vmarta vmarta enabled auto-merge (squash) April 11, 2023 19:13
@vmarta vmarta merged commit bd8e33c into v3 Apr 11, 2023
@vmarta vmarta deleted the upgrade-msw branch April 11, 2023 19:41
bfeister added a commit that referenced this pull request Apr 25, 2023
* v3:
  [Spike]Replace watch with nodemon (#1146)
  Fix Page Designer ImageWithText Link component (#1092) (#1148)
  Upgrade deprecated dependencies (#1124)
  Restart login flow if refresh_token is invalid (#1135)
  Move some util to site-utils to avoid circular imports (#1133)
  Restore old file name. (#1140)
  Update eslint configuration (#1129)
  CI: clarify the environment variables (#1127)
  Remove react-query-devtools from production build (#1121)
  Update lerna.json (#1118)
  Parallelize lighthouse ci (#1126)
  Increase test timeouts only on CI env (#1123)
  Remove unused `request` deprecated dependency. (#1125)
  Upgrade msw to latest (#1100)
  Add mergeBasket hook (#1114)
  [V3][Hooks Integration 🪝] Manually update cache for ShopperCustomer (#1113)
  dont use callback on mutateAsync (#1119)
  2-spaces not 4-spaces (#1117)

# Conflicts:
#	packages/internal-lib-build/package-lock.json
#	packages/pwa-kit-create-app/package-lock.json
#	packages/pwa-kit-dev/package-lock.json
#	packages/pwa-kit-dev/package.json
#	packages/pwa-kit-dev/src/configs/webpack/config.js
#	packages/pwa-kit-dev/src/ssr/server/build-dev-server.js
#	packages/pwa-kit-react-sdk/package-lock.json
#	packages/pwa-kit-runtime/package-lock.json
#	packages/template-express-minimal/package-lock.json
#	packages/template-mrt-reference-app/package-lock.json
#	packages/template-retail-react-app/package-lock.json
#	packages/template-typescript-minimal/package-lock.json
#	packages/test-commerce-sdk-react/package-lock.json
bfeister added a commit that referenced this pull request Apr 25, 2023
…-rehaul

* feature/template-extensibility:
  [Spike]Replace watch with nodemon (#1146)
  Fix Page Designer ImageWithText Link component (#1092) (#1148)
  Upgrade deprecated dependencies (#1124)
  Restart login flow if refresh_token is invalid (#1135)
  Move some util to site-utils to avoid circular imports (#1133)
  Restore old file name. (#1140)
  Update eslint configuration (#1129)
  CI: clarify the environment variables (#1127)
  Remove react-query-devtools from production build (#1121)
  Update lerna.json (#1118)
  Parallelize lighthouse ci (#1126)
  Increase test timeouts only on CI env (#1123)
  Remove unused `request` deprecated dependency. (#1125)
  Upgrade msw to latest (#1100)
  Add mergeBasket hook (#1114)
  [V3][Hooks Integration 🪝] Manually update cache for ShopperCustomer (#1113)
  dont use callback on mutateAsync (#1119)
  2-spaces not 4-spaces (#1117)

# Conflicts:
#	packages/internal-lib-build/package-lock.json
#	packages/pwa-kit-dev/package-lock.json
#	packages/pwa-kit-react-sdk/package-lock.json
#	packages/pwa-kit-runtime/package-lock.json
#	packages/pwa-kit-runtime/package.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for review PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants