newman-contract
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

npm version Build Status

Contract Testing using Newman

With newman-contract you dont need to relly on postman to read or write your contract tests, just write your contract definition like any other file and then use the powerful newman cli to run it!

If you are not familiar with Consumer-Driven contract tests using Postman, I recommend you to read this article.

Features:

  • Creates a boilerplate collection for contract testing
  • Human readable error messages
  • Easy source control over your contracts
  • Able to use process.env to build collections
  • Support for esModules
  • Typescipt definitions

Getting Started

Install:

npm install --save newman
npm install --save newman-contract

Then, create a file exporting your contract definition:

// contract/search.js
const { ContractDefinition } = require("newman-contract")
const baseUrl = "https://my-api.com"
 
module.exports = ContractDefinition({
  method: "GET",
  endpoint: `${baseUrl}/search`,
  query: { query: 'my term' }
  schema: {
    type: "object",
    properties: {
        // ... Your JSON schema to match response
    }
  }
})

Or, with ES Modules:

import { ContractDefinition } from 'newman-contract'
export default ContractDefinition({ ... })

Now, you need to run newman with a ContractCollection

const newman = require('newman')
const { ContractCollection } = require('newman-contract')
 
newman.run({
    collection: ContractCollection({ fromPattern: 'contract/*.js' })
    reporters: ['cli'] // You can use any newman reporter
})

API Reference

ContractDefinition(Object contract) -> Object

Parse the given contract object to a contract definition, building a postman test to match the response to the given schema.

contract
  • method: HTTP method
  • endpoint: endpoint to wich the request is made (accepts encoded query)
  • schema: response schema to match
  • name (optional): Name for the especific test
  • query (optinal): JSON Object containing all request query (priority over encoded query)
  • headers (optional): JSON Object containing all request headers
  • body (optional): JSON Object containing request body data

ContractCollection(Object options) -> JSON Object

Finds all Contract Definitions and builds a postman collection out of it.

options
  • fromPattern: Glob pattern to find the contract definitions
  • name (optional): Custom name for collection (Default: 'Contract Collection')
  • exportToPath (optional): File path to which the final collection will be exported to

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.1
    0
    • latest
  • 1.1.0
    0
    • next

Version History

Package Sidebar

Install

npm i newman-contract

Weekly Downloads

0

Version

1.1.1

License

ISC

Unpacked Size

37.1 kB

Total Files

48

Last publish

Collaborators

  • arupertti