Giter Site home page Giter Site logo

jest-nock's Introduction

jest-nock

Automate request traffic recording and replay for tests.

GitHub npm version

Instead of mocking requests or setting up nock for every test manually, this setup allows to mark tests that make requests to other systems and automatically record and replay requests made during a test run. After recording the requests for your test suite, it will never make real network requests again, so you don't need these external systems available during a test run and your test suite will run much faster, while still ensuring your project integrates well will the used APIs.

Example

it.nock('uses some method that makes requests to external services', () => {
    const result = await makeSomeRequestsAndCombineThem();
    expect(result).toBe(derivate);
});

Install

yarn add jest-nock

To use jest-nock with your jest setup, add it to your testFramework file setup as follows:

const { upgradeJasmine } = require('jest-nock');

upgradeJasmine(jasmine, global);

Usage

  • Mark a test to be recorded/replayed with it.nock(...)
  • Set JEST_NOCK_RECORD=true in your environment and run the tests you want to record against some real/mock/test API.

Recorded requests will be stored in a folder called __nocks__ next to the file containing the test/suite.

Acknowledgements

  • @eplawless for djb2 hash gist

Copyright 2018 Sebastian Herrlinger (SPRING Axel Springer Digital News Media GmbH)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

jest-nock's People

Contributors

kommander avatar tme-dynatrace avatar

Stargazers

Wildan Zulfikar avatar Hanif Dwy Putra S avatar Yuichi Tateno (secon) avatar M.K. Safi avatar Rob Gonnella avatar Reto Kaiser avatar Cristiano Rastelli avatar Roman avatar Loïc Mahieu avatar

Watchers

 avatar James Cloos avatar Andreas Prang avatar Viktor Mucha avatar  avatar

jest-nock's Issues

TypeScript types support

It'd be great to get types as part of the module. Otherwise VS Code always highlights it.nock as invalid.

http://take.ms/A4kwV

Thanks!

Option to de-activate `jest-nock`

Add an option to disable jest-nock and do real network request, but without update the fixtures. This would allow to have the same tests both for aceptation and integration.

Provide more control over the filename generation

First of all, great module, thanks for your work!

Would be great to be able to generate a custom filename.

For example, I'd like to be able to use group names and test name combo. And instead of djb2 to use a SHA hash.

Thank you.

Why the `.nock` suffix?

jest-nock/index.js

Lines 146 to 149 in bfe7d53

glb.it.nock = bindNock(env.it, testPath);
glb.fit.nock = bindNock(env.fit, testPath);
glb.beforeAll.nock = bindNock(env.beforeAll, testPath, 'beforeAll');
glb.afterAll.nock = bindNock(env.afterAll, testPath, 'afterAll');

Why are nocked functions set as new .nock ones instead of replace the original ones? This way capture would be done for all the tests... I think it makes sense to at least give the option to replace all tests functions with that. Also, maybe it would be possible to do the monkey patching automatically instead of by adding a setup file?

Cannot find module 'nock' from 'index.js'

Hey,

I'm getting the following error, when trying to use this module:

❯ npx jest
 FAIL  test/file.test.ts
  ● Test suite failed to run

    Cannot find module 'nock' from 'index.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:203:17)
      at Object.<anonymous> (node_modules/jest-nock/index.js:7:14)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.567s
Ran all test suites.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.