GithubHelp home page GithubHelp logo

codeandcats / jest-helpers Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 1.15 MB

TypeScript helper functions for Jest to help make your tests resilient to refactoring.

License: MIT License

JavaScript 24.63% TypeScript 75.37%

jest-helpers's Introduction

jest-helpers


Tests should be living documentation for your code, but often test descriptions get out of sync with your code. This library helps keep them in sync using TypeScript.


npm version Build Status Coverage Status

Benefits

  • When you rename a class, the name of your test for it will automatically update
  • When you rename any of the following, your test will get a TypeScript error until you update your test:
    • A method on a class
    • A field on a class
    • An exported function inside a module
  • Has useful functions for creating mocks:
    • partialOf<T>(partial: Partial<T>): T
    • deepPartialOf<T>(partial: DeepPartial<T>): T

Install

npm install jest-helpers --save-dev

Usage

./greeter.ts

export class Greeter {
  getGreeting(name: string) {
    return `Hello ${name}`;
  }
}

export function showGreeting(greeter: Greeter, name: string) {
  const greeting = greeter.getGreeting(name)
  console.log(greeting)
}

./greeter.test.ts

import { describeClass, describeFunction, describeMethod, partialOf } from 'jest-helpers';
import greeterModule = require('greeter');
import { Greeter, showGreeting } from './greeter';

describeClass(Greeter, () => {
  describeMethod(Greeter, 'getGreeting', () => {
    it('should return a personalised greeting', () => {
      const greeter = new Greeter();
      expect(greeter.getGreeting('Joe')).toEqual('Hello Joe');
    });
  });
});

describeFunction(greeterModule, 'showGreeting', () => {
  it('should log greeting to the console', () => {
    const greeterMock = partialOf<Greeter>({
      getGreeting: jest.fn().mockReturnValue('yo!')
    });

    jest.spyOn(console, 'log');

    showGreeting(greeterMock, 'Joe');

    expect(greeterMock.getGreeting).toHaveBeenCalledWith('Joe');
    expect(console.log).toHaveBeenCalledWith('yo!');
  });
});

Running jest --verbose will output something like

  example/greeter.ts
    Greeter
      getGreeting
        ✓ should return a personalised greeting (4ms)
    showGreeting
      ✓ should log greeting to the console (2ms)

If you rename your Greeter class, it will automatically update the test description.

If you rename your Greeter.getGreeting method, you will get a TypeScript error in your test until you update your test to match the new name.

If you rename your showGreeting function, you will get a TypeScript error in your test until you update your test to match the new name.

Contributing

Got an issue or a feature request? Log it.

Pull-requests are also welcome. 😸

jest-helpers's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

jest-helpers's Issues

An in-range update of ts-jest is breaking the build 🚨

The devDependency ts-jest was updated from 24.0.0 to 24.0.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

ts-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 23 commits.

  • b43b3c1 chore(release): 24.0.1
  • 2d91a37 chore: update package-lock
  • 485d3f7 build(deps): bump semver from 5.6.0 to 5.7.0 (#1043)
  • 2bd2534 build(deps-dev): bump @types/node from 10.14.3 to 10.14.4 (#1041)
  • bdba560 build(deps-dev): bump @types/node from 10.14.2 to 10.14.3 (#1038)
  • 08766bf build(deps-dev): bump @types/node from 10.14.1 to 10.14.2 (#1036)
  • 5f92fd2 build(deps-dev): bump js-yaml from 3.12.2 to 3.13.0 (#1034)
  • a9c79e9 build(deps-dev): bump @types/yargs from 12.0.9 to 12.0.10 (#1032)
  • 245ab29 build(deps-dev): bump eslint from 5.15.2 to 5.15.3 (#1031)
  • 4e72e59 build(deps-dev): bump eslint from 5.15.1 to 5.15.2 (#1030)
  • fb7dd55 feat(config): specify package.json location (#823) (#1013)
  • 279edcd build(deps-dev): bump tslint from 5.13.1 to 5.14.0 (#1028)
  • 8b93228 build(deps-dev): bump @types/node from 10.12.30 to 10.14.1 (#1027)
  • b825c7f build(deps-dev): bump @types/lodash.memoize from 4.1.4 to 4.1.6 (#1014)
  • 6f0ab80 build(deps-dev): bump @types/lodash.merge from 4.6.5 to 4.6.6 (#1015)

There are 23 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 11.12.1 to 11.12.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

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.