REST API Testing

with JavaScript

Preconfigure

Install Node and NPM

  1. Go to https://nodejs.org
  2. Download LTS version
  3. Install
  4. Verify that node is installed
➜  node -v
v10.15.3

➜  npm -v
6.9.0

Install Dependencies

➜  npm init

➜  npm install jest --save-dev

➜  jest --init

Jest

➜  npm install axios --save-dev

Why Jest?

  • zero config
  • fast and safe
  • code coverage
  • easy mocking
  • powerfull api, expect, mock, spy etc.
  • async out of the box

Basic Test

Write "Sum" function test

describe('test section name', () => {
  test('what should happen in test',() => {
    // your code()

    expect(... your code resule ...).toBe(... expected result ...)
  })
});

Async Callback Test

Async Callback Test

describe('testing async code with callbacks', () => {
  test('should return valid shop id', done => {
    function callback(data) {
      expect(data.statusCode).toBe(200);
      done();
    }

    getShopDetailsNodeHttp(VALID_SHOP, callback);
  })
});

Async Promise Tests

Promise base testing

describe('fetching total products in shop with Promises', () => {
  test('should return 54', () => {
    return getProductsCount(VALID_SHOP).then(count => expect(count).toBe(54))
  });

  test('should return Error', () => {
    expect.assertions(1);
    return getProductsCount(INVALID_SHOP).catch(e => expect(e).toBe('Error: No shop found'))
  });
});

Promise base testing

describe('fetching total products in shop with Promises and resolves, rejects expects', () => {
  test('should return 54', () => {
    expect(getProductsCount(VALID_SHOP)).resolves.toBe(54)
  });

  test('should return Error', () => {
    expect.assertions(1);
    return expect(getProductsCount(INVALID_SHOP)).rejects.toBe('Error: No shop found');
  });
});

with .resolves, .rejects expect api

Async/Await testing

describe('fetching total products in shop with async/await', () => {
  test('should return 54', async () => {
    const number = await getProductsCount(VALID_SHOP);
    expect(number).toBe(54);
  });

  test('should return "Error: No shop found"', async () => {
    expect.assertions(1);

    try {
      const number = await getProductsCount(INVALID_SHOP);
    } 
      catch (e) {
      expect(e).toBe('Error: No shop found');
    }
  });
});

Async/Await testing

describe.only('fetching total products in shop with async/await combined with .resolves/.rejects', () => {
  test('should return 54', async () => {
    await expect(getProductsCount(VALID_SHOP)).resolves.toBe(54);
  });

  test('should return "Error: No shop found"', async () => {
    expect.assertions(1);
    await expect(getProductsCount(INVALID_SHOP)).rejects.toBe('Error: No shop found');
  });
});

Combined with .resolves, .rejects

Which one to use?

Testing Serve Response

Analysing response status 

describe('check server response', () => {
  test('should return 200', async () => {
    const response = await getShopDetails(VALID_SHOP);
    expect(response.status).toBe(200);
  });

  test('should return 404', async () => {
    expect.assertions(1);

    try {
      const response = await getShopDetails(INVALID_SHOP);
    }
    catch (e) {
      expect(e.response.status).toBe(404);
    }
  });
});

Analysing response body 

describe.only('check server response data', () => {
  test('should return valid shop id', async () => {
    const response = await getShopDetails(VALID_SHOP);
    expect(response.data.id).toBe(12895814);
  });
});

Snapshot Testing

Snapshoting response body

describe.only('check server response data via shapshot', () => {
  test('should return valid shop id', async () => {
    const response = await getShopDetails(VALID_SHOP);
    expect(response.data.toJSON()).toMatchSnapshot();
  });
});
 › 1 snapshot written.
Snapshot Summary
 › 1 snapshot written from 1 test suite.

Snapshoting response body

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[
  `check server response data via shapshot should return valid shop id 1`] = `
    "{
      \\"id\\":12895814,
      \\"name\\":\\"CalacaCeramicArt\\",
      \\"title\\":\\"Unique Handmade Ceramic Teapots, Mugs, Sculptures, etc.\\",
      \\"productsCount\\":54
     }"
\`;

Snapshoting response body

 › 1 snapshot failed.
Snapshot Summary
 › 1 snapshot failed from 1 test suite. Inspect your code changes or run `npm test -- -u` to update them.

Modify response body

➜  npm test -- -u
 › 1 snapshot updated.
Snapshot Summary
 › 1 snapshot updated from 1 test suite.

Update shapshots

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[
  `check server response data via shapshot should return valid shop id 1`] = `
    "{
      \\"id\\":12895814,
      \\"name\\":\\"CalacaCeramicArt\\",
      \\"title\\":\\"Unique Handmade Ceramic Teapots, Mugs, Sculptures, etc.\\",
      \\"productsCount\\":55
     }"
\`;

Thank You

Questions?

REST API testing with JavaScript

By Viktor Shevchenko

REST API testing with JavaScript

  • 782