Skip to content

A demo of stubs and spies with CommonJS and ES modules. CommonJS doesn't allow mock, stubs and spies for dependencies called within the same module and ES modules don't allow them for named imports. This repo contains demos of the problem as well as solutions.

Notifications You must be signed in to change notification settings

rjbultitude/mocks-stubs-and-spies-CJS-ESM-Sinon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A demo of two problems with mocks, stubs and spies in modular JavaScript

Problem number one

When we use ES6's import/export syntax to share individual functions, but compile to CommonJS, it's not possible to stub or spy on a function that's called by another in the same module.

Problem number two

When using native ES modules, you cannot stub or spy on named imports from any module.

Set up

Install dependencies

Run npm install

Run in ES Modules mode

Add the property type to package.json and set the value to module then change the npm script test from npm run test:commonjs to npm run test:esm. Then run npm test and examine the results.

Run in CommonJS mode

Remove the type property from package.json and change the npm script test from npm run test:esm to npm run test:commonjs Then run npm test and examine the results.

About

A demo of stubs and spies with CommonJS and ES modules. CommonJS doesn't allow mock, stubs and spies for dependencies called within the same module and ES modules don't allow them for named imports. This repo contains demos of the problem as well as solutions.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published