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

@evidentiasoftware/jest-unit-test-generator

v1.2.5

Published

Generate unit test specs with all class dependencies mocked. Can be extended with custom handlers to cover move advanced cases

Downloads

38

Readme

Jest unit test generator

Travis CI build

Automates creation of initial unit test files taking dependencies into account.

Based on FDIM's jasmine-unit-test-generator and vkotliar1's angular-unit-test-generator

A jest Mock is created for each dependency.

The TestBed is created using the mocks.

A placeholder test is created for each public method.

Uses single or double quotes based on the target class's implementation.

For dependency names ending in 'Data', 'Config', or 'Environment', an empty object is referenced as a provider.

For dependency names ending in 'Type', the 'Type' is removed from the provider name, and a defaul implmentation with that name is assumed to exist.

For example: fakeConfigSvc = createSpyObj<EvConfigServiceType>(EvConfigService, []);

Supported types:

Usage

Installation

run npm i @evidentiasoftware/jest-unit-test-generator

Options

--version
Show version number [boolean]

--require
optional package to require [string]

--handlers
directory of optional handlers [string]

--overwrite
overwrite existing spec file [boolean]

--nodom
test component as provider instead of as declared component [boolean]

--all
recursively add unit test spec for all components/services, etc... [boolean]

--help Show help [boolean]

Basic

run jest-unit-test-generator <path-to-file>

With custom dependency handlers:

run jest-unit-test-generator --handlers <path-to-handlers-dir> <path-to-file>

With dependency handlers written in typescript:

install ts-node

run jest-unit-test-generator --require ts-node/register --handlers <path-to-handlers-dir> <path-to-file>

And note that if you install generator globally, ts-node must be installed globally as well. Otherwise both need to be installed locally in the project.

Dependency handlers

You can extend formatting of resulting spec files for each dependency by making a dependency handler. See default-dependency-handler.ts and event-bus.service.handler.ts

It is possible to add extra declarations, initializers and dependencies.

Known Issues

  • if Router or HttpClient are dependencies, mocks are created instead of using Router Test or Http Test modules
  • determination of Types should relay on object type instead of naming suffix
  • currently creates failing tests for abstract classes
  • does not handle dependencies of parent classes

Development

It's probably best to:

  • add an input file in spec/fixtures folder, e.g. test.ts
  • add expected output file, e.g. test.spec.expected.ts
  • link them in integration.spec.ts

Alternavely, you can:

  • run npm link
  • run npm run build:dev
  • run npm link @evidentiasoftware/jest-unit-test-generator in your project of choice
  • run jest-unit-test-generator <option> in your project of choice

Release

  • run npm run build
  • run npm publish