3

I am new to pact consumer testing. I am creating consumer pact using JavaScript. To verify with provider I need fromProvderState path parameter. I am using pactV3, here is the code

.given('set id', { id: 52})
        .withRequest({
          method: 'GET',
          path: fromProviderState('data/xml/${id}', 'data/xml/42'),
            headers: {
              Accept: MatchersV3.like(APP_JSON),
              'Content-Type': APP_JSON,
            },           
          })

I tried many example but couldn't make it work. My expectation is something like this

"path": {
  "dataType": "STRING",
  "expression": "data/xml/${id}",
  "type": "ProviderState"
}

In response json file this is what I am getting

"method": "GET",
 "path": "data/xml/42",

Can somebody please help me to solve this issue? Thanks in advance,

isherwood
  • 58,414
  • 16
  • 114
  • 157

2 Answers2

0

Why is that response json file a problem? What are you expecting it to do and why do you think you need it?

The fromProviderState feature only does the magic on the provider side. You return values that will be used in that expression, from the provider state callback. e.g. https://github.com/pact-foundation/pact-js/blob/master/examples/v3/provider-state-injected/provider/account-service.test.js#L36-L37

Please provide a full minimal reproducible example for us to help.

Matthew Fellows
  • 3,669
  • 1
  • 15
  • 18
0

This is how the path can be validated with Pact.

path: fromProviderState(
        `/data/xml/\${id}`,
        Matchers.regex(
          /^\/data\/xml\/[0-9]+$/,
          `/data/xml/${id}`
        )
      ) as Matchers.Path,

Change regex to what suits best for your path segment format. It can be UUID regex, or with a specific number length, etc.

Oleg Abrazhaev
  • 2,751
  • 2
  • 28
  • 41