-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Writing Tests
Luke Karrys edited this page Feb 25, 2022
·
5 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 . installYou may then run all tests:
$ node . testIn order to run a single test, just execute the test file directly:
$ node test/lib/ls.js
# runs `npm ls` testsIf 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
tap.mock.
Using it looks like:
// use t.mock() to require a module while replacing
// its internal required modules for mocked replacements:
const myModule = t.mock('../my-module', {
'fs': {
readFileSync: () => throw new Error('oh no')
},
'../util/my-helper.js': {
foo: () => 'bar'
}
})
// run tests, e.g:
t.equal(myModule.fnThatUsesMyHelper(), 'bar')