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

Mappersmith partial match is not very easy to parse and identify why something is not matching #269

Open
klippx opened this issue Jan 19, 2022 · 1 comment

Comments

@klippx
Copy link
Collaborator

klippx commented Jan 19, 2022

"[Mappersmith Test] No exact match found for \"POST http://example.org/blogs\" (body: \"{\"title\":\"title\",\"data\":{\"nestedItemTwo\":2,\"nestedItemOne\":1,\"nestingLevelTwo\":{\"deepNestedItemTwo\":2,\"deepNestedItemOne\":1}}}\"; headers: \"\"), partial match with \"POST http://example.org/blogs\" (body: \"{\"title\":\"title\",\"data\":{\"nestedItemOne\":1,\"nestedItemTwo\":2,\"nestingLevelTwo\":{\"deepNestedItemOne\":1,\"deepNestedItemTwo\":2}}}\"; headers: \"\"), check your mock definition"

What I do is usually to line break on POST http and then scroll to the right slowly until I can see why it's a diff... 😢

POST http://example.org/blogs\" (body: \"{\"title\":\"title\",\"data\":{\"nestedItemTwo\":2,\"nestedItemOne\":1,\"nestingLevelTwo\":{\"deepNestedItemTwo\":2,\"deepNestedItemOne\":1}}}\"; headers: \"\")
POST http://example.org/blogs\" (body: \"{\"title\":\"title\",\"data\":{\"nestedItemOne\":1,\"nestedItemTwo\":2,\"nestingLevelTwo\":{\"deepNestedItemOne\":1,\"deepNestedItemTwo\":2}}}\"; headers: \"\"), check your mock definition"

What would be nice is to have a diff where it was obvious what went wrong, such as:

"[Mappersmith Test] No exact match found for \"POST http://example.org/blogs\" - partial match with:"
{
   "body": {
     "title": "title",
     "data": {
-      "nestedItemTwo": 2,
+      "nestedItemOne": 1,
-      "nestedItemOne": 1,
+      "nestedItemTwo": 2,
       "nestingLevelTwo": {
-        "deepNestedItemTwo": 2,
+        "deepNestedItemOne": 1,
-        "deepNestedItemOne": 1
+        "deepNestedItemTwo": 2
       }
     }
   },
 }
@klippx
Copy link
Collaborator Author

klippx commented Jan 25, 2022

Discussed ideas:

One idea could be a middleware exposed in mappersmith/test that you opt-in

export const createDebugMockMiddleware: Middleware = () => ({
  response: async (next) => {
    try {
      return await next()
    } catch (e) {
      if ('message' in e && typeof e.message === 'string' && e.message.length > 0) {
        const { message } = e
        if (message.match(/\[Mappersmith Test\]/)) {
          console.log(awesomeFormat(message))
        }
      }
      throw e
    }
  },
})

Another idea is to use jest object matchers and the built in diff from there.

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

No branches or pull requests

1 participant