2.15.0 • Published 4 days ago

@s-ui/test-contract v2.15.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 days ago

sui-test-contract

Useful tooling for defining contract tests (based on Pact) that will generate contract documents. It will also allow us to publish such documents into a defined Pact Broker.

Dependencies

This package depends on the API mocking tool MSW. That means all the mocking requests should be handled by such tool, and the recommendation is to share the mocks along your whole app, so they should be placed in a unique folder named mocks in the root of your project.

Environment

These kind of contract test are intended to be executed in a server side environment with Mocha and Chai. It's recommended to use the package @s-ui/test for executing them.

Using the setup function

In order to start using the contract tests in your app, you'll need to execute the setupContractTests function passing the needed parameters. It will allow you to create the interactions, test them and finally it will generate the contract documents (in the path: contract/documents by default).

It's important to know that YOU'LL NEED TO EXECUTE THIS FUNCTION IN A TEST FILE (e.g.: consumerSpec.js)

Here you have a detailed example:

// `mocks/apples/responses.js`
export const applesResponse = [
  {color: 'red', type: 'Fuji'},
  {color: 'green', type: 'Granny Smith'}
]
// `mocks/apples/handlers.js`
import {rest} from 'msw'
import {applesResponse} from './responses.js'

export const applesRequestHandler = rest.get(
  'http://localhost:8181/apples',
  (req, res, ctx) => res(ctx.status(200), ctx.json(applesResponse))
)
// `contract/test/apples/consumerSpec.js`
import {expect} from 'chai'
import {FetcherFactory} from '@s-ui/domain'
import {setupContractTests} from '@s-ui/test-contract'
import {applesResponse} from '../../../../mocks/apples/responses.js'
import {applesRequestHandler} from '../../../../mocks/apples/handlers.js'

const fetcher = FetcherFactory.httpFetcher({config: {}})
const consumer = 'test-consumer'

setupContractTests({
  apiUrl: 'http://localhost:8181',
  consumer,
  fetcher,
  providers: {
    'test-provider': [
      {
        apiUrl: 'https://mydomain.com',
        endpoint: '/apples',
        description: 'A request for getting some apples',
        state: 'I have some apples',
        handler: applesRequestHandler,
        response: applesResponse
      }
    ]
  }
})

Setup options

ParameterRequiredTypeDefaultDescription
apiUrlYesstringComplete URL where ALL the requests should be done
consumerYesstringName of the API consumer
providersYesobjectObject of providers containing an array of interactions (see Provider interaction options)
fetcherYesFetcherInstance of a fetcher class (e.g. Axios)
defaultOptionsNoobjectDefault options for the requests
excludeHeadersNoarray['x-powered-by', 'accept', 'user-agent', 'cookie']Headers to be excluded in the generated contracts
contractsDirNostring./contract/documentsPath to the directory that will contain the generated contracts

Provider interaction options

ParameterRequiredTypeDefaultDescription
descriptionYesstringDescription for the interaction
stateYesstringState to be matched by the provider
apiUrlNostringApi url for current endpoint request
endpointYesstringEndpoint to be added in the contract document
queryNoobjectQuery params if needed in the request
bodyNoSee possibilities for Fetch API BodyBody to be sent in the request
methodNostringgetRequest method
handlerYesRequest handlerRequest handler used in the contract test
optionsNoobjectdefaultOptions (from the setup configuration)Request options
responseYesanyResponse to be validated in the contract test
addMatchingRulesNobooleanFlag to add Pact matching rules to check contracts by value type

Publishing the contracts

When you have your contract documents generated (e.g.: contract/documents/test-consumer-test-provider-123456789.json), you'll need to publish them to the Pact Broker, the place where providers (API Backend) will validate their own tests against the contracts.

You just need to run the following command:

sui-test-contract publish --broker-url "https://my-contract-tests-broker.com"
2.15.0

4 days ago

2.14.0

4 months ago

2.13.0-beta.4

4 months ago

2.13.0-beta.5

4 months ago

2.13.0-beta.6

4 months ago

2.13.0-beta.7

4 months ago

2.13.0-beta.8

4 months ago

2.13.0-beta.2

7 months ago

2.13.0-beta.3

7 months ago

2.13.0

5 months ago

2.13.0-beta.1

11 months ago

2.13.0-beta.0

12 months ago

2.11.0

1 year ago

2.12.0

1 year ago

2.11.0-beta.0

1 year ago

2.10.0

1 year ago

2.8.0-beta.2

2 years ago

2.8.0-beta.1

2 years ago

2.9.0

2 years ago

2.8.0

2 years ago

2.9.0-beta.0

2 years ago

2.8.0-beta.0

2 years ago

2.7.0

2 years ago

2.6.0

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

2.3.0

2 years ago

2.2.0

2 years ago

2.5.0

2 years ago

2.4.0

2 years ago

2.1.0

2 years ago

2.0.0-beta.4

2 years ago

2.0.0

2 years ago

2.0.0-beta.3

2 years ago

2.1.0-beta.0

2 years ago

2.0.0-beta.2

2 years ago

2.0.0-beta.1

2 years ago

2.0.0-beta.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0-beta.3

2 years ago

1.0.0-beta.2

2 years ago

1.0.0-beta.1

2 years ago

1.0.0-beta.0

2 years ago