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

[V3] Pact.log is gone? #909

Closed
2 tasks done
Maxim-Filimonov opened this issue Aug 4, 2022 · 5 comments
Closed
2 tasks done

[V3] Pact.log is gone? #909

Maxim-Filimonov opened this issue Aug 4, 2022 · 5 comments

Comments

@Maxim-Filimonov
Copy link

Maxim-Filimonov commented Aug 4, 2022

Thank you for making a feature request! We appreciate it very much. GitHub Issues are a big input into the priorities for Pact-JS development

All italic text in this template is safe to remove before submitting

Thanks again!

Checklist

This checklist is optional, but studies show that people who have followed it checklist are really excellent people and we like them

Before making a feature request, I have:

Feature description

To be able to see logs in pact.log again. It seems that pact.log is completely gone with V3.

Use case

So that it's possible to diagnose a mismatch issue without putting lots of console logs in my codebase and staring at errors like that:

data: {
        error: 'Request-Mismatch : HttpRequest { method: "POST", path: "/account/exist", query: None, headers: Some({"connection": ["close"], "accept": ["application/json", "text/plain", "*/*"], "x-app-version": ["1.0.0"], "x-app-platform": ["ios"], "user-agent": ["axios/0.27.2"], "content-length": ["24"], "host": ["127.0.0.1:56111"], "x-app-timezone": ["Europe/Paris"], "content-type": ["application/json; charset=utf-8"]}), body: Present(b"{\\"username\\":\\"username1\\"}", Some(ContentType { main_type: "application", sub_type: "json", attributes: {"charset": "utf-8"}, suffix: None }), None), matching_rules: MatchingRules { rules: {} }, generators: Generators { categories: {} } }'
      }
    }

I still have no clue what is it trying to tell me.

To add a bit more contex. Apparently, this particular issue happens if my own code throws an error on 500. In that scenario I cannot see pact logs which tell me what was the mismatch.

I was able to circumvent it with empty try catch:

try {
        await validateUsername({
          username,
          api: new Api(mockServer.url),
        });
      } catch (error) {}

And then I get proper errors:

Mock server failed with the following mismatches:

        0) The following request was incorrect: 

                POST /account/exist
              
                         1.0 Expected a Map with keys email, username but received one with keys username

Wondering if it's expected and should pact catch all exceptions and return them back with mismatches?

@YOU54F
Copy link
Member

YOU54F commented Aug 4, 2022

if you expect your client code to throw an error, you need to handle that in your testing framework

https://jestjs.io/docs/tutorial-async#rejects

jest will check that a function returns a rejected promise with your error message

an example with mocha

https://github.com/pact-foundation/pact-js/blob/master/examples/e2e/test/consumer.spec.js#L130

Any chance you could provide more of your test code?

@Maxim-Filimonov
Copy link
Author

Maxim-Filimonov commented Aug 4, 2022

Sure:
Here is the methor that is being called:

export async function validateUsername({
  username,
  email,
  api = new Api(),
}: {
  username?: string;
  email?: string;
  api?: Api;
}) {
  return httpRequest({
    method: 'post',
    endpoint: api.getUrl(RAW_ENDPOINTS.validateUsername),  // /account/exists
    body: { username, email },
  })
    .then((response) => {
      return response;
    })
    .catch((error) => {
      throw new Error(
        error.message || 'There was a problem processing the request',
      );
    });
}

httpRequest is just a thin wrapper around axios.
We get 500 from pact server due to mismatched request which causes this promise to be rejected. I do not expect rejection in this scenario as I'm testing positive case.
The problem is that when the pact server throws 500 promise rejection finishes tests before we get mismatches: log.
With pact v2 we had pact.log to check and figure out the problem.

@mefellows
Copy link
Member

Thanks for raising.

The issue is because we throw immediately if we get an error, but we should still report the failure in those cases (see https://github.com/pact-foundation/pact-js/blob/master/src/v3/pact.ts#L214).

This should be easy enough to do.

We currently don't expose a way to log to file, but if we properly logged to the terminal would that solve your issue @Maxim-Filimonov ?

@Maxim-Filimonov
Copy link
Author

I think logging to terminal would be at least some solution. Old pact did a very easy to understand pact.log which was pleasure to work with as I didn't need to look for it in the middle of all other application logs.

@Maxim-Filimonov
Copy link
Author

Clossing as this has been implemented.

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 a pull request may close this issue.

3 participants