Question

Perform authentication in Jest integration tests

  • 13 July 2023
  • 1 reply
  • 775 views

Badge +1

I’ve created a number of integration tests (using Jest) for my integration/app that require valid credentials for the tests to succeed.

What’s the recommend approach to perform the authentication?  Maybe invoke the perfrom method of the authentication.js script in a before filter?  

beforeAll(() => {
// TODO: get a new access token
// invoke authenticate.js?
});

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Userlevel 3
Badge +6

Hey @benjamink9 ,

Great question!

Yes, while I haven’t tried that personally, using appTester to invoke the authentication before the tests certainly sounds like a valid approach.

Alternatively, a less dynamic option might be to grab credentials, export them as environment variables, and import them into a mocked data bundle. You could also hardcode them into the mocked bundle, but we recommend utilizing environment variables. Here’s an example of this for reference:

https://github.com/zapier/zapier-platform/blob/1217d2797881462d732da1d764ef51655b05a08b/example-apps/oauth2/test/authentication.test.js#L85

One other option that comes to mind is to use Nock to mock requests. If you’re not testing the authentication directly and are looking to test how other parts handle a given response, Nock can help simulate a request, and you could likely exclude auth from the mocked request:

https://github.com/zapier/zapier-platform/blob/main/packages/cli/README.md#mocking-requests