Coder Social home page Coder Social logo

jest-how-do-i-mock-x's Introduction

Jest: How do I mock x?

This repo contains runnable minimal reproducable examples of where I've been stuck in the past trying to work out how to mock something, and (maybe) the solution.

The aim of this repo is to:

  • provide a testing ground for me to try mocking things in a simplified, isolated environment
  • help future me come back to a particular solution I came up with in the past
  • maybe help others in similar situations :)

Feel free to PR with any other jest testing puzzles you've faced!

Scenarios

Running the tests

$ yarn # install node_modules
$ yarn test # runs Jest

Further reading / Prior art

Testing approach

There are many ways to unit test, and I'm not suggesting what's "good" or "bad". This section just provides context as to why the solutions here may or may not look idiosyncratic.

Unit testing setup

For the most part, test files usually:

  • import something once at the top level
  • have a bunch of unit tests over that imported thing
  • want to change a mocked value per test

I personally don't usually define manual mock files for modules, and prefer to inline the mocking where possible. This is because:

  • I commonly want to change the mocked value per test (as described above), so I'd have to build in something like __setMockFiles (as seen in the the Jest docs example) to provide this "dynamic mock" behaviour.
  • I like to be explicit in tests and see all the "mock logic" inline with the rest of the test
  • Keeping a parallel structure of every module I want to mock gets tedious for larger codebases

jest-how-do-i-mock-x's People

Contributors

magicmark avatar

Watchers

 avatar

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.