<#1243 fix: allow hooks to run only once per inter...
# pact-js-development
g
#1243 fix: allow hooks to run only once per interaction Pull request opened by lhokktyn Current behaviour will run the
beforeEach
and
afterEach
hooks multiple times if there are several provider states defined in an interaction. This is due to the way the hook are run on every call to
/_pactSetup
via the proxy. This change will ensure each of those hooks is run only once per interaction, regardless of how many provider states are defined in that interaction. --- Our primary use case in applying this fix was to be able to support multiple
given()
statements in our consumer tests without having them each trigger the
beforeEach
hook (which we want to use for resetting state), eg await provider() .addInteraction() .given("has a pet with status", "sold") .given("has a pet with status", "pending") .uponReceiving("a request for pets with multiple statuses") ... Which produces the following in the Pact JSON: { "description": "a request for pets with multiple statuses", "pending": false, "providerStates": [ { "name": "has a pet with status", "params": { "value": "sold" } }, { "name": "has a pet with status", "params": { "value": "pending" } } ], ... } Which drives multiple calls to the
/_pactSetup
endpoint during Provider verification, one for each of the
providerStates
, and thus multiple calls to
beforeEach
. This fix ensures the hooks are only run on the first
setup
call and on the last
teardown
call. pact-foundation/pact-js GitHub Actions: Test linux-musl-arm64-node-18 GitHub Actions: Test linux-musl-amd64-node-18 GitHub Actions: Test linux-musl-amd64-node-20 GitHub Actions: Test linux-musl-amd64-node-22 GitHub Actions: Test windows-latest--node-22 GitHub Actions: Test ubuntu-latest--node-22 GitHub Actions: Test macos-12--node-22 GitHub Actions: Test macos-14--node-22 GitHub Actions: Test windows-latest--node-20 GitHub Actions: Test ubuntu-latest--node-20 GitHub Actions: Test macos-12--node-20 GitHub Actions: Test macos-14--node-20 GitHub Actions: Test windows-latest--node-18 GitHub Actions: Test ubuntu-latest--node-18 GitHub Actions: Test macos-12--node-18 GitHub Actions: Test macos-14--node-18 GitHub Actions: Test windows-latest--node-16 GitHub Actions: Test ubuntu-latest--node-16 GitHub Actions: Test macos-12--node-16 GitHub Actions: Test macos-14--node-16 ✅ 4 other checks have passed 4/24 successful checks