pact-foundation / pact-js

JS version of Pact. Pact is a contract testing framework for HTTP APIs and non-HTTP asynchronous messaging systems.
https://pact.io
Other
1.63k stars 347 forks source link

[v3] on change of state handlers creates adds extra contract instead of ovewriting existing one #938

Closed Maxim-Filimonov closed 2 years ago

Maxim-Filimonov commented 2 years ago

Software versions

Issue Checklist

Please confirm the following:

Expected behaviour

I have the following contract:

return {
    states: [
      {
        description: 'provider has valid account to login with responsive app',
        parameters: { username, email, deviceId },
      },
    ],
    uponReceiving: 'a POST request to validate the username',
....

when I changed description of state:

        description: 'has senior account',

it should update description of state in JSON contract.

Actual behaviour

Instead, I got two different contracts in my contract.json:

"description": "a POST request to login with valid username and password",
      "providerStates": [
        {
          "name": "has senior account",
}...
 "description": "a POST request to login with valid username and password",
      "providerStates": [
        {
          "name": "provider has valid account to login with responsive app",
        }
      ],

Steps to reproduce

  1. Using v3 contract create a json file with any contract which includes state handler with description.
  2. Change description of state and notice that now there are two contractsin your json file.
mefellows commented 2 years ago

Have you cleaned out the pact directory before running the tests? By default, it will append to the pact file.

See also https://github.com/pact-foundation/pact-js/blob/master/docs/migrations/9-10.md#consumer

Maxim-Filimonov commented 2 years ago

As per the change to pactfileWriteMode this also means pact files should be cleared out prior to each test, to avoid extraneous interactions in a pact file.

This part? Right so it seems I would need to rm -rf pacts BEFORE running any pact tests ?

mefellows commented 2 years ago

Bingo!

Maxim-Filimonov commented 2 years ago

Right, so for anybody who stumbles on this here is solution for jest: First configure globalSetup script

// jest.config.js
module.exports { 
   globalSetup: './src/jest.setup.global.js',
}

Then create a script which removes pacts folder before starting tests

// jest.setup.global.js
// const fs = require('fs') // for classic node
import fs from 'fs';

module.exports = () => {
  console.debug('Clearing pact contracts');
  fs.rmdirSync('./pacts', { recursive: true });
};
mefellows commented 2 years ago

@YOU54F this might be a good thing to bring into jest-pact?

Because Pact JS doesn't know about the test runner and the process is stateless (because we enable parallel running), we don't actually know when we can clobber the file and start again. But jest pact (and mocha pact) might?