newman-contract
v1.1.1
Published
A helper for contract testing using newman
Downloads
3
Maintainers
Readme
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 methodendpoint
: endpoint to wich the request is made (accepts encoded query)schema
: response schema to matchname (optional)
: Name for the especific testquery (optinal)
: JSON Object containing all request query (priority over encoded query)headers (optional)
: JSON Object containing all request headersbody (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 definitionsname (optional)
: Custom name for collection (Default: 'Contract Collection')exportToPath (optional)
: File path to which the final collection will be exported to