PactumJS is a REST API Testing Tool used to automate e2e, integration, contract & component (or service level) tests.
|
This readme offers an basic introduction to the library. Head over to the full documentation at https://@patrtorg/at-quaeratjs.github.io
We use Github Discussions to receive feedback, discuss ideas & answer questions.
# install @patrtorg/at-quaerat as a dev dependency
npm install --save-dev @patrtorg/at-quaerat
# install a test runner to run @patrtorg/at-quaerat tests
# mocha / jest / cucumber
npm install --save-dev mocha
or you can simply use
npx @patrtorg/at-quaerat-init
@patrtorg/at-quaerat can be used for all levels of testing in a test pyramid. It can also act as an standalone mock server to generate contracts for contract testing.
Tests in @patrtorg/at-quaerat are clear and comprehensive. It uses numerous descriptive methods to build your requests and expectations.
Running simple api test expectations.
const { spec } = require('@patrtorg/at-quaerat');
it('should be a teapot', async () => {
await spec()
.get('http://httpbin.org/status/418')
.expectStatus(418);
});
it('should save a new user', async () => {
await spec()
.post('https://jsonplaceholder.typicode.com/users')
.withHeaders('Authorization', 'Basic xxxx')
.withJson({
name: 'bolt',
email: 'bolt@swift.run'
})
.expectStatus(200);
});
# mocha is a test framework to execute test cases
mocha /path/to/test
See @patrtorg/at-quaerat-cucumber-boilerplate for more details on @patrtorg/at-quaerat & cucumber integration.
Scenario: Check Tea Pot
Given I make a GET request to "http://httpbin.org/status/418"
When I receive a response
Then response should have a status 418
// steps.js
const @patrtorg/at-quaerat = require('@patrtorg/at-quaerat');
const { Given, When, Then, Before } = require('@cucumber/cucumber');
let spec = @patrtorg/at-quaerat.spec();
Before(() => { spec = @patrtorg/at-quaerat.spec(); });
Given('I make a GET request to {string}', function (url) {
spec.get(url);
});
When('I receive a response', async function () {
await spec.toss();
});
Then('response should have a status {int}', async function (code) {
spec.response().should.have.status(code);
});
@patrtorg/at-quaerat can act as a standalone mock server that allows us to mock any server via HTTP or HTTPS, such as a REST endpoint. Simply it is a simulator for HTTP-based APIs.
Running @patrtorg/at-quaerat as a standalone mock server.
const { mock } = require('@patrtorg/at-quaerat');
mock.addInteraction({
request: {
method: 'GET',
path: '/api/projects'
},
response: {
status: 200,
body: [
{
id: 'project-id',
name: 'project-name'
}
]
}
});
mock.start(3000);
Inspired from frisby and pact.
Like this project! Star it on Github and follow on Twitter. Your support means a lot to us.
If you've ever wanted to contribute to open source, and a great cause, now is your chance! See the contributing docs for more information.
Thanks to all the people who contribute.