Skip to content

JeremyAw/api_testing_with_nock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Testing with Nock

This repository is used to try out API testing using nock, mocha, chai and chai-http.

The test server will be set up using express.

Setting Up

  • Just run npm install if you're cloning this repository along with the package.json file
  • The instructions below are for starting from scratch

Initialize the repository

  • Install nodejs and npm
    sudo apt install nodejs

  • If npm wasn't installed along with nodejs, install it manually
    sudo apt install npm

  • Create package.json file
    npm init

  • Create .gitignore file
    touch .gitignore

  • Install express
    npm install express

Install testing dependencies

  • Install nock, mocha, chai and chai-http
    npm install --save-dev nock mocha chai chai-http

  • Modify test script in package.json

"scripts": {
    "test": "mocha"
  },

Mocking HTTP requests using nock

  • For each describe block, create a nock interceptor for the http route that is being tested in the before hook
describe('GET /route/to/be/tested', () => {
    before(() => {
      nock(host)
      .get('/route/to/be/tested')
      .reply(200, 'This is some fake data sent by the nock interceptor.')
    })

    it('should get do something', (done) => {
      // test something
      // call done() to signal callback end (only when using `chai-http`)
    })
  })
  • Run npm test to execute the tests without a live server (duh, that's the whole point)

Recording fixtures using Nock Back

  • Nocks can become cumbersome to write, especially when they return different results with each call

  • Fortunately, nock supports fixture recording and playback

  • Set up the fixture directory and mode to use

const nockBack = require('nock').back

nockBack.fixtures = __dirname + '/fixtures'
nockBack.setMode('record')
  • For each http request to record/test, pass in the desired file name for the recorded fixture, call the http request before finally using the recorded fixture to run the tests

  • Take note to call nockDone() after recording the fixture and after completion of the tests

  • You can use other packages such as http or request to make the http request as well

  • Ensure that you pass in the same fixture file name for both nockBack() calls

// Pass in desired file name for recorded fixture
nockBack('myFixture.json', async (nockDone) => {
  // Record the fixture
  await axios.get('https://some-random-website.com')
  nockDone()

  // Use the recorded fixture for tests
  nockBack('myFixture.json', (nockDone) => {
    describe('GET /route/to/be/tested', () => {
        // test something
        // call done() to signal callback end (only when using `chai-http`)
      })
    })
    nockDone()
  })
})

Recording fixtures using Nock Recorder

  • to be done soon

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published