codeceptjs-httpmock is CodeceptJS helper which wraps mockttp library to manage http mock in tests.
NPM package: https://www.npmjs.com/package/codeceptjs-httpmock
This helper should be configured in codecept.json/codecept.conf.js
port: mock port. Default9037debug: - (optional) enable debug logs. Defaultfalse
Example:
{
"helpers": {
"HTTPMock" : {
"require": "codeceptjs-httpmock",
"debug": false,
"port": 9037
}
}
}Set up mock for method и urlPath. Response calculates in callback function.
I.respondWith('POST','/url',(req)=>{if(req.body.status==='good'){return{status: 200,body: '{"Status":"OK"}',headers: {'Content-Type': 'application/json'}};}return{status: 400,body: {error: 'badman'}}});Parameters
method- request methodurlPath- relative path for mockcallback- (optional) callback function. Should return response objectctx- (optional) mockttp context
Validates that request comes for method and urlpath that satisfies predicate function
I.expectRequestUntil('POST','/myPath',req=>req.body.json.param==='myValue');Parameters
method- request methodurlPath- relative path for mockpredicate- (optional) specify predicate function. Predicate function should returntruevalue.timeout- (optional) timeout in ms. After the end of the timeout method will throw Error
Validates that no request comes for method and urlpath that satisfies predicate function
I.dontExpectRequestUntil('POST','/myPath',req=>req.body.json.param==='myValue');Parameters
method- request methodurlPath- relative path for mockpredicate- (optional) specify predicate function. Predicate function should returnfalsevalue.timeout- (optional) timeout in ms. After this timeout helper will check that there were no requests
Get requests that comes to mock server by method and urlPath
letrequests=awaitI.grabServerRequests('POST','/myPath');Parameters
method- request methodurlPath- relative path for mock