All Projects → magicmark → jest-how-do-i-mock-x

magicmark / jest-how-do-i-mock-x

Licence: other
Runnable examples for common testing situations that often prove challenging

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to jest-how-do-i-mock-x

Vuex Mock Store
✅Simple and straightforward Vuex Store mock for vue-test-utils
Stars: ✭ 246 (+290.48%)
Mutual labels:  mock, jest
automock
A library for testing classes with auto mocking capabilities using jest-mock-extended
Stars: ✭ 26 (-58.73%)
Mutual labels:  mock, jest
jest-launchdarkly-mock
Easily unit test LaunchDarkly feature flagged components with jest
Stars: ✭ 14 (-77.78%)
Mutual labels:  mock, jest
React Nativeish
React Native / React Native Web Boilerplate
Stars: ✭ 106 (+68.25%)
Mutual labels:  mock, jest
Sinon Jest Cheatsheet
Some examples on how to achieve the same goal with either of both libraries: sinon and jest. Also some of those goals achievable only by one of these tools.
Stars: ✭ 226 (+258.73%)
Mutual labels:  mock, jest
Jest Canvas Mock
🌗 A module used to mock canvas in Jest.
Stars: ✭ 189 (+200%)
Mutual labels:  mock, jest
Graphql Query Test Mock
Easily mock GraphQL queries in your Relay Modern / Apollo / any-other-GraphQL-client tests.
Stars: ✭ 49 (-22.22%)
Mutual labels:  mock, jest
Jest Mock Extended
Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
Stars: ✭ 224 (+255.56%)
Mutual labels:  mock, jest
Jest Localstorage Mock
A module to mock window.localStorage and window.sessionStorage in Jest
Stars: ✭ 247 (+292.06%)
Mutual labels:  mock, jest
React-CleanArchitecture-Example
Sample frontend project with Clean Architecture using React.js and jest.
Stars: ✭ 73 (+15.87%)
Mutual labels:  mock
minesweeper
💣Minesweeper game written with React
Stars: ✭ 18 (-71.43%)
Mutual labels:  jest
snappify
A generator of Jest snapshot-based tests for React components written with TypeScript 📸
Stars: ✭ 64 (+1.59%)
Mutual labels:  jest
ying-template
这是一个基于 `webpack@^5.27.2` + `typescript@^4.2.3` + `@babel/core@^7.2.2` + `jest@^26.6.3` + `eslint@^7.22.0` 的多页面脚手架。
Stars: ✭ 125 (+98.41%)
Mutual labels:  jest
split-tests
Split test files in Jest and Cypress into parallel CI jobs
Stars: ✭ 22 (-65.08%)
Mutual labels:  jest
mock-proxy-middleware
前后端分析项目中的本地mock和远程代理及接口文档
Stars: ✭ 26 (-58.73%)
Mutual labels:  mock
react-component-library-lerna
Build your own React component library managed with lerna, presented with storybook and published in private npm registry.
Stars: ✭ 55 (-12.7%)
Mutual labels:  jest
nextjs-typescript-graphql-starter
A Next.js starter with Typescript, Jest, GraphQL Apollo Server & Apollo Client v3 with graphql-codegen.
Stars: ✭ 15 (-76.19%)
Mutual labels:  jest
nextjs-starter-kit
NextJS Starter Kit with Testing Frameworks and CI/CD
Stars: ✭ 30 (-52.38%)
Mutual labels:  jest
jscodeshift-typescript-example
jscodeshift typescript codemod example
Stars: ✭ 27 (-57.14%)
Mutual labels:  jest
expo-detox-typescript-example
Sample Expo app with e2e tests using detox, jest and typescript
Stars: ✭ 81 (+28.57%)
Mutual labels:  jest

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].