Gathering detailed insights and metrics for spec-mate
Gathering detailed insights and metrics for spec-mate
Gathering detailed insights and metrics for spec-mate
Gathering detailed insights and metrics for spec-mate
spectacle-material-cn
Generate beautiful static API documentation from OpenAPI/Swagger 2.0 specifications (zh-cn)
mock-mate
A powerful tool to mock any API based on OpenAPI specs
@zampou-spec/material-ui-phone-number-2
A material-ui react component to format phone numbers. Based on Material UI Phone Number, which ceased to be maintained.
@spectrumrjsf1/material-ui
Material UI theme, fields and widgets for react-jsonschema-form
npm install spec-mate
Typescript
Module System
Node Version
NPM Version
70.3
Supply Chain
98.1
Quality
74.9
Maintenance
100
Vulnerability
99.6
License
Total Downloads
151
Last Day
1
Last Week
7
Last Month
12
Last Year
151
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
spec-mate@1.0.0
Unpacked Size
302.31 kB
Size
73.41 kB
File Count
8
NPM Version
10.8.3
Node Version
20.16.0
Published on
Sep 14, 2024
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
7
Compared to previous week
Last Month
20%
12
Compared to previous month
Last Year
0%
151
Compared to previous year
spec-mate is a powerful API testing framework developed by S25Digital, designed to help developers streamline the process of testing and validating APIs. With support for advanced assertions, customizable hooks, and seamless integration with tools like Nock, spec-mate makes it easy to test even the most complex APIs.
chai-json-schema
.To install spec-mate, use npm or yarn:
1npm install spec-mate
Additionally, you’ll need some supporting libraries for testing:
1npm install chai axios nock --save-dev
1import { SpecMate } from 'spec-mate'; 2 3const api = new SpecMate('https://api.example.com'); 4 5async function runTest() { 6 await api.request('/users') 7 .withMethod('GET') 8 .expectStatus(200) 9 .expectHeader('Content-Type', 'application/json') 10 .expectJsonPath('results[0].name', 'John Doe') 11 .run(); 12} 13 14runTest();
This example demonstrates a simple GET
request with validations for the response status code, content type, and a specific JSON path.
SpecMate offers a range of built-in assertions to validate various aspects of an API response. Below is a detailed list of available assertions with usage examples.
Ensure that the response has the expected status code.
1.expectStatus(200)
Check if a specific header exists and has the expected value.
1.expectHeader('Content-Type', 'application/json')
Ensure a specific header is present in the response.
1.expectHeaderExists('Content-Type')
Ensure a specific header is absent in the response.
1.expectHeaderNotExists('X-Powered-By')
Validate that a value exists at a specific path within the JSON response body.
1.expectJsonPath('results[0].name', 'John Doe')
Check the length of an array or object at a specific JSON path.
1.expectJsonLength('results', 3)
Ensure the response body contains a specific text.
1.expectBodyContains('user')
Ensure that the response body matches a regular expression.
1.expectBodyMatches(/"name": "John Doe"/)
Verify that the Content-Type
header matches a specific type.
1.expectContentType('application/json')
Ensure that the status code falls within a specific range.
1.expectStatusInRange(200, 299)
Check if the response status is a redirection (3xx).
1.expectRedirect()
Check for the existence and value of a cookie.
1.expectCookie('session', 'abc123')
Ensure that a cookie contains a specific partial value.
1.expectCookieContains('session', 'abc')
Create your own assertions with custom logic. This is helpful for more advanced testing scenarios.
1.customAssertion((response) => { 2 expect(response.data.results).to.have.lengthOf.above(1); 3});
In addition to the built-in assertions, SpecMate allows you to define your own custom assertions to extend the testing capabilities.
1api.request('/users') 2 .withMethod('GET') 3 .customAssertion((response) => { 4 expect(response.data).to.have.property('results').with.length.greaterThan(1); 5 }) 6 .run();
Check for the existence or value of cookies in the response.
1.expectCookie('session', 'abc123') 2.expectCookieContains('session', 'abc')
Here’s an example of using spec-mate with Nock to mock API responses for testing purposes:
1import { SpecMate } from 'spec-mate'; 2import nock from 'nock'; 3 4describe('API Test Example with Nock', () => { 5 const api = new SpecMate('https://api.example.com'); 6 7 beforeEach(() => { 8 nock.cleanAll(); 9 nock.disableNetConnect(); 10 }); 11 12 it('should mock and validate a GET request', async () => { 13 // Mock GET /users API request 14 nock('https://api.example.com') 15 .get('/users') 16 .reply(200, { 17 results: [{ name: 'John Doe' }, { name: 'Jane Doe' }] 18 }); 19 20 await api.request('/users') 21 .withMethod('GET') 22 .expectStatus(200) 23 .expectJsonPath('results[0].name', 'John Doe') 24 .run(); 25 }); 26});
SpecMate provides hooks that allow you to run custom logic before or after a request is made.
Use the beforeRequest
hook to modify request headers, such as adding authentication tokens.
1api.on('beforeRequest', (context) => { 2 context.headers['Authorization'] = `Bearer some-token`; 3});
Run logic after the request is completed, such as logging the response.
1api.on('afterRequest', (response) => { 2 console.log('Response received:', response.data); 3});
This project is licensed under the MIT License. See the LICENSE file for more details.
spec-mate is developed and maintained by S25Digital
For seamless API testing with mocked responses, check out mock-mate, another tool developed by S25Digital. mock-mate is designed to make API mocking simpler and more flexible, providing an easy way to create mock APIs that integrate perfectly with your spec-mate tests.
With mock-mate, you can:
Use mock-mate in combination with spec-mate to create a complete testing suite for validating API functionality in controlled environments.
No vulnerabilities found.
No security vulnerabilities found.