npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-typed-mock

v1.4.3

Published

Get your __mocks__ typed!

Downloads

3

Readme

jest-typed-mock

Make unit tests great again!

NPM

jest-typed-mock brings type checking to the wild __mocks__ of Jest. Helping you maintain the correct mocks, matching the real files, they are going to mock. This is like eslint, but for mocks.

Manual mocks are defined by writing a module in a __mocks__/ subdirectory immediately adjacent to the module. For example, to mock a module called user in the models directory, create a file called user.js and put it in the models/__mocks__ directory. https://facebook.github.io/jest/docs/en/manual-mocks.html#content

Usage

Add to your package.json

"scripts": {
  ...
  "jest-typed-mock": "jest-typed-mock flow",       // for Flow 
  "jest-typed-mock": "jest-typed-mock typescript", // for TS
  "jest-typed-mock": "jest-typed-mock javascript", // for JS
  "jest-typed-mock": "jest-typed-mock exports",    // only check names
  ...
}

In some cases (always?) you have to specify babel env.

Type safety

Rewiremock can provide a type safety to dependency mocking, but Jest do had it's own mocking (and sandboxing) system. As long rewiremock still able to work, even under the Jest management - it is now a quite good idea.

jest-typed-mock is not a library. It is just a small tool, which can check mocked file against the real one.

Lets imagine - we have a.js

//a.js
export const function1 = (a:number):number => a**2;
export const function2 = (a:number):number => a*2;
export default (a:string) => a.substr(1)

and __mocks__/a.js

export const function1 = (a:number):number => 1;
export const function2 = (a:number):number => 2;
export default (a:string) => "3"

It is ok. But next you change the real a.js

//a.js
// function 1 is changed
export const function1 = (a:string):number => parseInt(a);
// function 2 is removed
export default (a:string) => a.substr(1)

But your tests are still using the old __mocks__, and they are still green.

jest-typed-mock just matches real files and mocks.

TS:

Property 'function2' is missing in type 'typeof "....a"'.

Flow:

property `function2` of exports of "/__mocks__/a.js". Property not found in const real = () => import('/a.js');

JS

As long there is no types, could only test exported names as their types(object, number, function), and function arguments count. Even this is quite helpful.

jest-typed-mock: mocked export "function2" does not exists in a.js jest-typed-mock will also try to check function agains function, at least argument count, which can break a lot of things. To bypass this checking use jest-typed-mock exports, or specify noFunctionCompare in API options

require('jest-typed-mock/runjs')(__dirname , {noFunctionCompare: true})

Result

As result - you reduce a smell of mocking. Mocks becomes less fake, and tests becomes less flake.

Licence

MIT