nock-hook

1.0.1 • Public • Published

nock-hook

Build Status

Convinient wrapper around Nock for HTTP testing

Install

$ npm install nock-hook --save-dev

Examples

mocha

'use strict';
 
const assert = require('assert');
const nockHook = require('nock-hook');
 
const got = require('got');
 
describe('nock-hook', function() {
 
  it('fetch ya.ru', function() {
    const closeNock = nockHook('fetch-ya.ru', { dirname: __dirname + '/fixtures/mocha' });
    const startTime = Date.now();
 
    return got('https://ya.ru/')
      .then(res => {
        assert(res.body.toLowerCase().indexOf('<!doctype html>') === 0, 'body starts by doctype');
        assert((Date.now() - startTime) < 20, 'less than 50 ms');
 
        closeNock();
      })
      .catch(err => {
        closeNock();
        return Promise.reject(err);
      });
  });
 
});

ava

  • All tests should be serial
import test from 'ava';
import nockHook from 'nock-hook';
 
import got from 'got';
 
test.serial('test', async t => {
  const startTime = Date.now();
  const closeHook = nockHook('fetch-ya-ru', { dirname: __dirname + '/fixtures/ava.default' });
 
  const res = await got('https://ya.ru/');
 
  t.truthy(res.body.toLowerCase().indexOf('<!doctype html>') === 0);
  t.truthy((Date.now() - startTime) < 50);
 
  closeHook();
});

ava with helper

  • All tests should be serial
  • Automatically setup function name
import test from 'ava';
import nockHook from 'nock-hook/ava';
 
import got from 'got';
 
test.beforeEach(t => {
  t.context.closeNock = nockHook(t, __filename, { dirname: __dirname + '/fixtures/ava.helper' });
});
 
test.afterEach.always(t => {
  t.context.closeNock();
});
 
test.serial('fetch ya.ru', async t => {
  const startTime = Date.now();
  const res = await got('https://ya.ru/');
 
  t.truthy(res.body.toLowerCase().indexOf('<!doctype html>') === 0);
  t.truthy((Date.now() - startTime) < 50);
});

License

MIT © ewnd9

Readme

Keywords

Package Sidebar

Install

npm i nock-hook

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • ewnd9