Skip to content

Instantly share code, notes, and snippets.

@bethesque
Created March 31, 2022 06:02
Show Gist options
  • Save bethesque/a4b117ebb09488c101f88320cac678a4 to your computer and use it in GitHub Desktop.
Save bethesque/a4b117ebb09488c101f88320cac678a4 to your computer and use it in GitHub Desktop.
Example of how to use Cypress and Pact together using a shared fixture
// in cypress support
const Matchers = require('@pact-foundation/pact-web').Matchers
// turn a pact definition into a cypress mock
// TODO headers, and allow safe overriding of generated fields
const toCypressInteraction = (interaction) => {
return {
method: interaction.withRequest.method,
url: Matchers.extractPayload(interaction.withRequest.path),
status: interaction.willRespondWith.status,
response: Matchers.extractPayload(interaction.willRespondWith.body)
}
}
module.exports = {
toCypressInteraction
}
// shared fixture
import { Matchers } from '@pact-foundation/pact-web'
const { eachLike, iso8601DateTimeWithMillis, regex } = Matchers
export const TRIGGERED_WEBHOOKS_FOR_INTEGRATION_INTERACTION = {
state: 'triggered webhooks exist for consumer supply and provider product',
uponReceiving: 'load webhooks request',
withRequest: {
method: 'GET',
path: '/integrations/provider/product/consumer/supply/triggered-webhooks'
},
willRespondWith: {
status: 200,
body: {
_embedded: {
triggeredWebhooks: eachLike({
uuid: 'FYgxVp3S8dl2Ebw9SU-Leg',
webhookUuid: 'V9F_Gzf_WqpChQJMz1ka5Q',
webhookDescription: 'a webhook that does something',
triggerType: 'resource_creation',
status: regex({ matcher: 'success|failure|not_run', generate: 'success' }),
createdAt: iso8601DateTimeWithMillis('2019-12-02T12:36:12.041+11:00'),
updatedAt: iso8601DateTimeWithMillis('2019-12-02T12:36:18.394+11:00'),
eventName: 'verification_published',
consumerVersionNumber: '239aa50+1607297182',
providerVersionNumber: 'cdbbf3f+1607390821',
attempts: 7,
_embedded: {
executions: eachLike({
logs: 'LOGS'
})
}
})
}
}
}
}
// in the pact test
pactflow.addInteraction(TRIGGERED_WEBHOOKS_FOR_INTEGRATION_INTERACTION)
// in the cypress test
const { TRIGGERED_WEBHOOKS_FOR_INTEGRATION_INTERACTION } = require("../../tests/fixtures/triggeredWebhooks")
cy.route(toCypressInteraction(TRIGGERED_WEBHOOKS_FOR_INTEGRATION_INTERACTION))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment