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 349 forks source link

TypeScript error when trying to provide a stateHandler with setup and teardown #1164

Open ghost91- opened 10 months ago

ghost91- commented 10 months ago

Software versions

Please provide at least OS and version of pact-js

Issue Checklist

Please confirm the following:

Expected behaviour

There are no TypeScript errors when trying to provide a stateHandler with setup and teardown to a Verifier.

Actual behaviour

Trying to provide a stateHandler with setup and teardown results in the following TypeScript error:

error TS2322: Type '{ setup: () => Promise<void>; teardown: () => Promise<void>; }' is not assignable to type 'StateHandler & ((state: string, params?: { [name: string]: string; } | undefined) => Promise<unknown>)'.
  Type '{ setup: () => Promise<void>; teardown: () => Promise<void>; }' is not assignable to type 'StateFuncWithSetup & ((state: string, params?: { [name: string]: string; } | undefined) => Promise<unknown>)'.
    Type '{ setup: () => Promise<void>; teardown: () => Promise<void>; }' is not assignable to type '(state: string, params?: { [name: string]: string; } | undefined) => Promise<unknown>'.
      Type '{ setup: () => Promise<void>; teardown: () => Promise<void>; }' provides no match for the signature '(state: string, params?: { [name: string]: string; } | undefined): Promise<unknown>'.

Steps to reproduce

import { Verifier } from '@pact-foundation/pact';

const verifier = new Verifier({
  stateHandlers: {
    'name of the stateHandler': { setup: async () => {}, teardown: async () => {} } // this line produces a TypeScript error
  }
});
valkolovos commented 2 months ago

I've seen this error as well. It appears to be because VerifierOptions as defined in src/dsl/verifier/types.ts includes both ProxyOptions and MessageProviderOptions which each define stateHandlers differently. I am not very good with Typescript, but the error message thrown indicates that the transpiler is preferring the type definition of stateHandlers defined by MessageProviderOptions over the one provided by ProxyOptions.

muratkeremozcan commented 2 months ago

I have an easy repro.

Pull the repo, install and remove the ts-ignore to reproduce.

https://github.com/muratkeremozcan/pact-js-example-provider/blob/main/src/test-helpers/state-handlers.ts#L66