New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jest-test-each

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-test-each

run parametrised tests easily [typesafe] without text tables or arrays of arrays.

  • 0.7.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
375
increased by237.84%
Maintainers
1
Weekly downloads
 
Created
Source

jest-test-each

This package will help you to run parametrised tests easily [typesafe] without text tables or arrays of arrays.

GitHub watchers

Table of Contents

  1. Examples
  2. Setup
  3. Features
  4. What's next

Examples

You can see demo project here.

Tests are here.

Simple

its('roundings')
  .each([
    { input: 0, expected: '0' },
    { input: 0.99, expected: '1' },
    { input: 102.99998, expected: '103' },
    { input: -6, expected: '-6' },
  ])
  .run(t => {
    expect(Math.round(t.input).toFixed(0)).toBe(t.expected);
  });

Run test in idea with jest plugin:

More complex

its('check calculator')
  .each([
    { a: 1, b: 2, exp: [3, -1, 2, 0.5] },
    { a: 1, b: 0, exp: [1, 1, 0, Infinity] },
  ])
  .each(t => [
    { sign: '+' as const, exp: t.exp[0] },
    { sign: '-' as const, exp: t.exp[1] },
    { sign: '*' as const, exp: t.exp[2] },
    { sign: '/' as const, exp: t.exp[3] },
  ])
  .each(t => [{ flatDesc: `${t.a} ${t.sign} ${t.b} should be ${t.exp}` }])
  .run(async t => {
    expect(calc(t.a, t.b, t.sign)).toBe(t.exp);
  });

and the same test with auto cases names:

Setup

Install dev dependency:

yarn add -D jest-test-each

Add the following into your config setup.js file which is referred in jest.config.js - setupFilesAfterEnv section:

// Looking a way to improve this. Can anyone help ?)
require('jest-test-each');

for .ts tests to see globals 'its' and 'Test' add the following to your tsconfig:

// tsconfig.json
 "include": [
    ...
    "node_modules/jest-test-each/dist/index.d.ts"
  ]
Additional [optional]

You can override test runner environment (by default it is jest env) by the following:

TestEachEnv({
  describe: describe,
  it: it,
  beforeAll: beforeAll,
  ...
});

Features

  • cases multiplication (.each().each()....run())
  • ability to setup test-each (global setup or each test setup):
    • number suites/cases or not
    • group each level by suite or not
    • concurrent testing
    • max length of case name - on reaching it will ask to sepcify explicit description for case
  • ability to setup test runner environment (by default it is jest)
  • ability to specify description for each case as function depending on case args
  • ability to create .each level depending on previous data
  • ability to have flat tests cases (not groupped to suites) when each case has 'flatDesc'
  • global var 'Test' (or 'its' alias) to access test each (I'm accepting suggestions on namings)
  • ability to run without single .each (its('foo').run(..))
  • To run one test from TestEach like it.only (.only(<filter>)) (example).
  • ability to run '.before' in testEach with disposable interface for automatic cleanup (example).
  • ability to skip test if it is marked with defect (example).
  • added field 'actualFailReasonParts' for case - to fail when defected test fails with other reason than expected (example).
  • '.ensure' to check cases match some condition (example).
  • '.ensureCasesLength' to check cases length - in case when .each has dynamic args and you don't want to miss some tests (example).
  • ability to skip test

.only() usage

See example.

Running the feature containing this will result in 2 tests

  its('Simple test: roundings')
    .each([
      { input: 0, expected: '0' },
      { input: 0.99, expected: '1' },
      { input: 102.99998, expected: '103' },
      { input: -6, expected: '-6' },
    ])
    .only(t => t.input === -6)
    .run(t => {
      expect(Math.round(t.input).toFixed(0)).toBe(t.expected);
    });

What's next

  • do not create suite wrapping when resulted to only one test in the group

Unavailable features

  1. to start testEach by Idea plugin (workaround: wrap with describe and do not put name into Test Each)

Keywords

FAQs

Package last updated on 09 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc