Skip to content

Writing Tests

Ruy Adorno edited this page Mar 3, 2021 · 3 revisions

All tests are written using tap as our test framework.

Before running any tests make sure you install the cli dev dependencies using the cloned repo itself:

$ cd /path/to/cloned/npm/cli
$ node . install

You may then run all tests:

$ node . test

In order to run a single test, just execute the test file directly:

$ node test/lib/ls.js
# runs `npm ls` tests

If you need files on disk as part of your test, we use tap fixtures to handle that:

var path = require('path')
var t = require('tap')

t.test('test something', t => {
  const path = t.testdir({
    'package.json': JSON.stringify({
      name: 'my-example-package',
      version: '1.0.0'
    })
  })
  t.ok(path, 'should return file path to a folder containing the created fixtures')
})

If you need to mock modules (by modules I mean anything you require), you can use require-inject.

Using it looks like:

test('name-of-group-of-tests', t => {
  cost gentlyRm = requireInject('../../path/to/module/being/tested.js', {
    'mocked-module': {…mocked version…}
    '../../path/to/mocked/module.js': {…mocked version…}
  })
})
Clone this wiki locally