Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Kentico Cloud ❤️ Jest

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.

How to achieve this state

  1. Initialize the npm application

    1. Create a project folder
    mkdir kc-jest
    cd kc-jest
    1. Initialize the npm package
    npm init -y

    This will generate package.json file (more info).

  2. Add .gitignore file to avoid spamming repository by node_modules content

  3. Install all required dependencies

    npm install --save-dev rxjs kentico-cloud-delivery jest
    • rxjs is a peer dependency for kentico-cloud-delivery
    • jest is a testing framework

    Development dependencies are listed in [package.json] an installed in node_,modules folder.

    {
      ...
      "devDependencies": {
        "jest": "^24.7.1",
        "kentico-cloud-delivery": "^5.7.2",
        "rxjs": "^6.4.0"
      }
      ...
    }
    
  4. 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)
      })
    })
  5. Add test script to the package.json,

    {
      ...
      "scripts": {
          "test": "jest"
      }
    }
    
  6. Run the tests

npm run test

Hurray! Kentico Cloud ❤️ Jest!

Jest showcase