Need help with js-mocking-fundamentals?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

kentcdodds
172 Stars 121 Forks Other 18 Commits 0 Opened issues

Description

JavaScript Mocking Fundamentals on TestingJavaScript.com

Services available

!
?

Need anything else?

Contributors list

# 1,260
Babel
CSS
package...
reactjs
16 commits
# 74,555
HTML
kcd-edu
Shell
TypeScr...
1 commit

JavaScript Mocking Fundamentals


Learn how mocking in JavaScript tests works by implementing them from scratch!


In this material, we have a set of

no-framework
tests that correspond to a set of jest tests (in the
__tests__
directory). The idea is that (with the exception of the first test), you look at the jest version first, then see how that would be implemented without a testing framework.

Order of material:

  1. monkey-patching.js
    (no jest version)
  2. mock-fn.js
  3. spy.js
  4. inline-module-mock.js
  5. external-mock-module.js

The files are intended to test the

thumb-war.js
module and mock the
utils
module.

To run the tests, run

npx jest
. To start watch mode run
npx jest --watch

Custom jest runner.

You can definitely run the

no-framework
files just using
node
(like this:
node src/no-framework/monkey-patching.js
), but in an effort to make running these easier, I created a custom jest runner that uses jest to run the files, but allow them to be run without the jest testing framework. It's really cool. It uses
create-jest-runner
and should probably be published eventually.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.