JavaScript Mocking Fundamentals on TestingJavaScript.com
Learn how mocking in JavaScript tests works by implementing them from scratch!
In this material, we have a set of
no-frameworktests 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:
monkey-patching.js(no jest version)
mock-fn.js
spy.js
inline-module-mock.js
external-mock-module.js
The files are intended to test the
thumb-war.jsmodule and mock the
utilsmodule.
To run the tests, run
npx jest. To start watch mode run
npx jest --watch
You can definitely run the
no-frameworkfiles 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-runnerand should probably be published eventually.