npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@s-ui/test-contract

v2.16.0

Published

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

Downloads

6,200

Readme

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

| Parameter | Required | Type | Default | Description | | ---------------- | -------- | --------- | ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | | apiUrl | Yes | string | | Complete URL where ALL the requests should be done | | consumer | Yes | string | | Name of the API consumer | | providers | Yes | object | | Object of providers containing an array of interactions (see Provider interaction options) | | fetcher | Yes | Fetcher | | Instance of a fetcher class (e.g. Axios) | | defaultOptions | No | object | | Default options for the requests | | excludeHeaders | No | array | ['x-powered-by', 'accept', 'user-agent', 'cookie'] | Headers to be excluded in the generated contracts | | contractsDir | No | string | ./contract/documents | Path to the directory that will contain the generated contracts |

Provider interaction options

| Parameter | Required | Type | Default | Description | | ------------- | -------- | ------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------- | --------------------------------------------- | | description | Yes | string | | Description for the interaction | | state | Yes | string | | State to be matched by the provider | | apiUrl | No | string | | Api url for current endpoint request | | endpoint | Yes | string | | Endpoint to be added in the contract document | | query | No | object | | Query params if needed in the request | | body | No | See possibilities for Fetch API Body | | Body to be sent in the request | | method | No | string | get | Request method | | handler | Yes | Request handler | | Request handler used in the contract test | | options | No | object | defaultOptions (from the setup configuration) | Request options | | response | Yes | any | | Response to be validated in the contract test | | addMatchingRules | No | boolean | | Flag 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"