question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[2.35] - mockClient running middlewares which fails matching to the mock store

See original GitHub issue

In PR #168 we added headers to mock strategy. Now I have an issue in 2.35 release that my mocks are no longer matching because of DurationMiddleware running and setting x-started-at to a value that is later unable to be matched in isExactMatch (specifically in the headerMatch):

    {
      A: 'correlation-id=f497e1b8-0fc8-42c5-87e3-5ef58f39bd8e&service-to-service-request=yes&x-started-at=1637663489675',
      B: 'correlation-id=f497e1b8-0fc8-42c5-87e3-5ef58f39bd8e&service-to-service-request=yes&x-started-at=1637663489668'
    }

A workaround is to do a MockDate.set(date) in a beforeEach - which felt a bit random - but without it the Date.now() values will be mismatching and failing to match the correct mock. Mocks not matching have always been a bit hard to debug, and now it is even harder.

This was unexpected though: We do not provide headers, so the test should just work, right?

  • Yes, but however, the problem is the fact that mockClient (as per specification!) runs the middleware stack on the mock gateway! This is different to how mockRequest works. So actually, all tests work if switched to mockRequest API instead (without the need for calling MockDate.set(date)).

In summary:

  • mockClient runs middlewares which potentially adds headers
  • This can cause existing tests to fail (if they are using mockClient) even if they are not providing headers to the mock definition
  • #168 was meant to be backwards compatible if your mock definition did not add headers and break if there was mismatch only
  • #168 got stabbed in the back by mockClient running middlewares which we were unaware of

Any opinions what we should do, a few options:

  • We can proceed, add warnings about the differences mockClient and mockRequest to the README
  • We can add best practices about using MockDate.set when running tests (and using DurationMiddleware)
  • We can try to make this new behaviour more optional
  • We can revert the whole thing

WDYT? Discuss.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tulioscommented, Nov 24, 2021

I think we can diff the input/output. We could do it for all properties. A bit harder with things like body, but we can still know if a middleware changed it. This change has the potential to improve error messages, etc.

1reaction
tulioscommented, Nov 23, 2021

@Yurickh a good idea is to add a canonical way for middleware to add headers, so we can track and differentiate between input headers from userland. I think it’s a good idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock redux saga middleware with Jest - Stack Overflow
This error indicates that the mock function does not work as I intend: it doesn't seem to be overwritten and is called from...
Read more >
https://raw.githubusercontent.com/tulios/mappersmi...
... Fix bug in mockRequest/mockClient where body params did not match independent ... a previous middleware's `error` #230 Added: - `mappersmith`: `Request.
Read more >
AWS SDK v3 Client mock - npm
Features: fluent interface - declaring behavior is short and readable; matching options - defining mock behavior by Command type and/or ...
Read more >
Open Source Used In HULK 13 - Cisco
have designed this version of the GPL to prohibit the practice for those ... produce the work, or an object code interpreter used...
Read more >
Unit Testing Redux Thunks using Redux Mock Store
There may be cases where you'll have a middleware on your production redux store, but forget to add it to your mock store,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found