Coder Social home page Coder Social logo

generate-jest-mocks's Introduction

generate-jest-mocks

Automatically generate Jest mocks for your Javascript/Typescript files based on module usage.

Example

Consider the following file:

const api = require('api');
const track = require('track');
const { flush } = require('cache');

function main(user) {
  const userID = api.users.create(user);
  track('create_user', userID);
  flush(userID);
}

Running generate-jest-mocks on it will result in the following manual Jest mocks being generated:

jest.mock('api', () => ({
  users: {
    create: jest.fn(),
  }
});
jest.mock('track', () => jest.fn());
jest.mock('cache', () => ({ flush: jest.fn() }));

Or alternatively, when using --automock, Jest automocks can be generated:

jest.mock('api');
jest.mock('track');
jest.mock('cache');

It also handles ES6 and Typescript files:

import cache, {set} from 'cache';

set('foo', 'bar');
cache.flush();
}

will output:

jest.mock('cache', () => ({
  default: {
    flush: jest.fn(),
  },
  set: jest.fn(),
}));

Install

Install it using npm:

npm install -g generate-jest-mocks

Usage

generate-jest-mocks path/to/input.js

Options

Generate Automocks

By default generate-jest-mocks will output manual mocks. To generate automocks use --automock or -a.

Include modules

By default a mock will be generated for each imported module. To specify specific modules to be mocked use --include=module or -i=module. Example:

generate-jest-mocks --include=api -i=cache path/to/input.js

Exclude modules

To exclude one or more modules from being mocked use --exclude=module or -e=module. Example:

generate-jest-mocks --exclude=api -e=cache path/to/input.js

Programming API

Import and call the default function:

const generate = require('generate-jest-mocks').default;

const output = generate(fs.readFileSync('path/to/file.js'), {
  automock: false,
  exclude: ['exclude/module'],
  include: ['include/module'],
});

or with ES6 simply:

import generate from 'generate-jest-mocks';

How to Contribute

  1. Fork the repository
  2. Create a new branch for your feature or bug fix
  3. Write your code
  4. Write tests that cover your code as much as possible
  5. Run all tests and ensure they pass
  6. Submit a pull request

Please try to keep your pull request small and focused. This will make it much easier to review and accept.

generate-jest-mocks's People

Contributors

codedependant avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.