Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@s-ui/test-contract

Package Overview
Dependencies
Maintainers
0
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@s-ui/test-contract

Useful tooling for defining contract tests (based on Pact) that will generate contract documents.

  • 2.16.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
2K
decreased by-7.74%
Maintainers
0
Weekly downloads
 
Created
Source

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"

FAQs

Package last updated on 27 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc