New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

unit-test-recorder

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unit-test-recorder

A cli tool records usage and generates unit tests

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
116
Maintainers
1
Weekly downloads
 
Created
Source

unit-test-recorder

Record unit tests as you use your application.

sample

TL;DR: Jest snapshots on steroids

Installation

npm i -g unit-test-recorder

Usage

Procedure

  • Make sure you are using git and there are no uncommited changes.
  • Run unit-test-recorder <your entrypoint>.js. Usually it is index.js or server.js. UTR will require this file as module. Make sure you are not using require.main === module.
  • It will now crawl through your project and hook itself into all your files. It will use git reset to revert it when testing is done.
  • As you use your application, it will record activity for all your exported functions.
  • Press Q or Ctrl + C key on the keyboard to safely stop the recording and your application and start generating the tests.
  • Run your favourite linter/prettier to clean up the generated test code.

Typescript (beta)

  • Make sure you have typescript installed globally. (npm i -g typescript)
  • unit-test-recorder will look for tsconfig.json in pwd. You can specifiy a differnt file using --typescript-config=my-tsconfig.json. This will make UTR automatically generate tests in typescript.

Notes

  • IMPORTANT: Serialization of JSONs in the process of recording may cause slowdown. The APIs may take considerably longer to execute. (2-10x)
  • All local modules are mocked but external modules must be whitelisted. e.g. Create a file whitelist.json with content { "axios": true } and use the flag --whitelist=./whitelist.json to record mocks for functions used by axios.
  • The ./utr_activity directory contains all the snapshots which are used to generate the code for test cases. If UTR finds an existing directory, it will load these snapshots as state. This enables the user to record in multiple sessions. It can be deleted after code has been generated.

Flags

Except entrypoint all are optional.

Flag nameDescriptionDefault
entrypoint (positional)Path to entrypoint of your applicationNone (Required)
--onlyRun only on these files (relative path, comma separated, supports javascript RegExp)undefined
--exceptDont run on these files (relative path, comma separated, supports javascript RegExp)undefined
--whitelistPath to whitelist.json./whitelist.json
--typescript-configPath to typescript config./tsconfig.json
--max-testsMaximum number of generated tests per function. Type -1 for infinity.5
--output-dirThe directory in which the tests would be written to../
--test-extExtension for test files (spec/test)test
--size-limitObjects larger than this limit will be moved to a different file500
--max-stack-depthProperties of a JSON, at a depth higher than this, will not be recorded.7

Environment variables

Environment variable nameDescription
UTR_EXPERIMENTAL_ALSSet this flag to use AsyncLocalStorage instead of cls-hooked. (Experimental, requires nodejs v13.10+)

Features

  • Pure functions
  • Dependency injections
  • Mocks
  • Typescript (beta)

Planned features

  • JSX
  • class methods
  • function methods
  • Better typescript support
  • Higher order functions

Mechanism

Detailed explanation can be found here.

Lets take this function as an example

const foo = (a, b) => a + b

UTR uses babel to explicitly instrument it to

const foo = (...p) => recorderWrapper(
    { name:'foo', fileName:'bar' },
    (a, b) => a + b,
    ...p
  )

The recorderWrapper function is provided by UTR. The function below is a simplified version of its implementation.

const recorderWrapper = (meta, fn, ...p) => {
  const result = fn(...p)
  Recorder.record({ meta, result, params: ...p })
  return result;
}

This records all calls to this function in a state. Here is a simplified representation.

{
  "fileName":{
    "functionName":{
      "captures":[
        { "params": [1, 2], "result": 3 },
        { "params": [2, 3], "result": 5 },
      ]
    }
  }
}

Now using string interpolation, we can generate test cases.

describe('fileName', () => {
  describe('functionName', () => {
    it('test 1', () => {
      expect(foo(1,2)).toEqual(3);
    })
    it('test 2', () => {
      expect(foo(2,3)).toEqual(5);
    })
  })
})

Troubleshooting

  • UTR is still early in development and may be unstable. Users are recommended to use the --only flag to run UTR on a few files at a time.

Similar software

Keywords

babel

FAQs

Package last updated on 23 Mar 2026

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