department-of-veterans-affairs / va.gov-team

Public resources for building on and in support of VA.gov. Visit complete Knowledge Hub:
https://depo-platform-documentation.scrollhelp.site/index.html
282 stars 203 forks source link

Integrate Pact with Cypress #10658

Open U-DON opened 4 years ago

U-DON commented 4 years ago

Description

Implement a method for creating stubs in Cypress tests based on contracts generated from Pact.

Acceptance Criteria

U-DON commented 3 years ago

Previous proof of concept: department-of-veterans-affairs/vets-website#12505

holdenhinkle commented 3 years ago

Holden Hinkle 11:12 AM Hey Eugene. Happy Friday! When you have a chance, can you please add more details to this ticket so we better understand what you want/envision? https://app.zenhub.com/workspaces/vsp-5cedc9cce6e3335dc5a49fc4/issues/department-of-veterans-affairs/va.gov-team/10658

eugene_doan 11:14 AM Sure, I can fill out some more details when I have time. For now, I’ve posted a link to an old draft PR that I wrote up to explore this idea.

Holden Hinkle 11:17 AM Great, thanks Eugene.

Holden Hinkle 11:51 AM What about removing /pacts from .gitignore and then importing a contract into a cypress spec like you did here in src/applications/search/tests/search.cypress.spec.js: import contract from '../../../../pacts/va.gov_search-va.gov_api.json'; Is that all we need to do? The contract should only be committed if it passes…

eugene_doan 11:52 AM I was pondering the idea of having the Pact tests be a dependency for the E2E tests in the CI graph.

Holden Hinkle 11:53 AM What do you mean by CI graph?

eugene_doan 11:53 AM The Pact tests still need to be run, so I didn’t see the value of having to commit the pacts to version control.

eugene_doan 11:53 AM As in E2E tests in CI wouldn’t run until both the build and Pact tests are completed.

Holden Hinkle 8 minutes ago That’s when the contract would be saved. Then the Cypress tests would run and reference the contract in CI.

eugene_doan 7 minutes ago Yeah, we don’t need to remove the pacts from .gitgnore if we use them in this way. They’re always generated from running the tests anyway.

Holden Hinkle 7 minutes ago Right, I see that now.


eugene_doan 11:54 AM If we were to explore this idea of using the mocks created from Pact. 11:55 E2E tests already have a dependency on the build.

Holden Hinkle 11:59 AM When are Pact tests run now? Before or after Cypress tests? 11:59 I like this idea btw - makes perfect sense. :+1: 1

eugene_doan 11:59 AM Before, but the Cypress tests don’t have an explicit dependency on the Pact tests.

Holden Hinkle 12:00 PM Okay, so we need to setup that dependency and then test this? 12:01 I haven’t configured anything in Circle yet, but it looks easy enough (I think). I remember Demain kinda of showing me how that’s done. 12:01 We should probably move this to GitHub actions first, right? Since we’re done with Circle?

eugene_doan 12:02 PM Yeah, I’d explore the idea in a branch. Set up the dependency in CI, modify a test to use a generated pact, then see what happens when it runs in the PR. :+1: 1

12:02 Yup, we’re moving to GitHub Actions, so that’s where it should be implemented.

Holden Hinkle 12:02 PM Awesome, thanks Eugene!

U-DON commented 3 years ago

Here's a blog post from the developers of Pact that presents an example of integrating Pact and Cypress. I haven't looked much into this yet, so it might differ from the route we want to explore.

holdenhinkle commented 3 years ago

Cool article. It's basically the same thing we want to explore. 99% of the article just articulates it in a very clear way.

However, the author says this in a callout: I want to run Cypress tests against mocked data and I want to have contracts between the frontend and backend. I think it would be great if those contracts could be used to supply mocks or equivalently, if the test mocks could be used to generate contracts.

That last part ("if the test mocks could be used to generate contracts") is the only time he mentions the idea of somehow creating contracts from test mocks. I'm not sure how that would work. I like your idea of first writing the consumer tests, then importing the contract into the Cypress test, and making that workflow a sort of convention.

holdenhinkle commented 3 years ago

This can be implemented after we move to GitHub Actions.

pjhill commented 2 years ago

Now that we have moved to GHA, let's explore this ticket again.