This project was written from scratch using jus npm init
command to showcase how to start with jest testing framework in combination with Kentico Cloud Delivery SDK.
-
Initialize the npm application
- Create a project folder
mkdir kc-jest cd kc-jest
- Initialize the npm package
npm init -y
This will generate
package.json
file (more info). -
Add
.gitignore
file to avoid spamming repository bynode_modules
content -
Install all required dependencies
npm install --save-dev rxjs kentico-cloud-delivery jest
rxjs
is a peer dependency forkentico-cloud-delivery
jest
is a testing framework
Development dependencies are listed in [
package.json
] an installed innode_,modules
folder.{ ... "devDependencies": { "jest": "^24.7.1", "kentico-cloud-delivery": "^5.7.2", "rxjs": "^6.4.0" } ... }
-
Write the (end-to-end) test (delivery.test.js)
const { DeliveryClient } = require('kentico-cloud-delivery'); describe('Delivery sdk', () => { it("load articles", async () => { const client = new DeliveryClient({ projectId: "975bf280-fd91-488c-994c-2f04416e5ee3" }); const result = await client.items() .type("article") .getPromise(); expect(result).toHaveProperty('items'); expect(result.items).toHaveLength(6); result.items.forEach(item => console.log(item.elements.title.value)); // Just to see the titles in log // expect(result).toMatchSnapshot(); // Just a snapshot sample (https://jestjs.io/docs/en/snapshot-testing) }) })
-
Add test script to the
package.json
,{ ... "scripts": { "test": "jest" } }
-
Run the tests
npm run test
Hurray! Kentico Cloud ❤️ Jest!