Skip to main content
Version: 2.0.0

Keploy Integration with Jest

Pre-requisites

  1. Node.js
  2. nyc: npm i nyc

Installation

Get Keploy jest sdk

Install the latest release of the Keploy Jest SDK

npm i @keploy/sdk

Update package file

Update the package.json file that runs the application:

  "scripts": {
//other scripts
"test": "jest --coverage --collectCoverageFrom='src/**/*.{js,jsx}'",
"coverage": "nyc npm test && npm run coverage:merge && npm run coverage:report",
"coverage:merge": "mkdir -p ./coverage && nyc merge ./coverage .nyc_output/out.json",
"coverage:report": "nyc report --reporter=lcov --reporter=text"
//other scripts
}

Usage

For the code coverage for the keploy API tests using the jest integration, you need to add the following test to your Jest test file. It can be called as Keploy.test.js.

const {expect} = require("@jest/globals");
const keploy = require("@keploy-sdk"); //shortend this
const timeOut = 300000;

describe(
"Keploy Server Tests",
() => {
test(
"TestKeploy",
(done) => {
const cmd = "npm start";
const options = {};
keploy.Test(cmd, options, (err, res) => {
if (err) {
done(err);
} else {
expect(res).toBeTruthy(); // Assert the test result
done();
}
});
},
timeOut
);
},
timeOut
);

Now let's run jest tests along keploy using command:-

keploy test -c "npm test" --delay 15

To get Combined coverage

keploy test -c "npm run coverage" --delay 10 --coverage