Search code examples
pact

Contract matching rules are being ignored when running verification


The matching rules in my contract are being ignored. I have a contract where I need to make sure that certain value is a string. When running verify command I am getting an error because the test is looking for an exact match. I am using in the consumer side (javascript) pact Matchers in order to check only for the type of data and not the value itself. The provider is a rails application using pact version 1.20.0.

I also tried to use regex but the problem is still happening.

This is how the contract looks. I have removed some sensitive data.

{
  "consumer": {
    "name": "consumer"
  },
  "provider": {
    "name": "provider"
  },
  "interactions": [
    {
      "description": "a request",
      "providerState": "provider state",
      "request": {
        "method": "POST",
        "path": "/path/api/stuff",
        "headers": {
          "Authorization": "Bearer token",
          "Content-Type": "application/json"
        }
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json; charset=utf-8"
        },
        "body": {
          "source_code": "abcdef"
        },
        "matchingRules": {
          "$.body": {
            "match": "type"
          }
        }
      }
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "3.0.0"
    }
  }
}

This is the error that I am getting.

   Diff
   --------------------------------------
   Key: - is expected 
        + is actual 
   Matching keys and values are not shown

    {
   -  "source_code": "abcdef"
   +  "source_code": "xde5r8"
    }

   Description of differences
   --------------------------------------
   * Expected "abcdef" but got "xde5e3" at $.source_code

I expect this test to be successful because I am expecting a match in the type, and in this case both responses are strings. Maybe I am missing something here.


Solution

  • That looks to be a v3 body matcher, which not all language implementations share (see [1]). It's possible that you're using a consumer language (e.g. Pact JVM) that is generating a v3 contract, but is being verified by a v2 provider (e.g. Pact JS).