From 66bf8ff71009ca03accfe3d4b1a31c9087c56438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6nnemann?= Date: Sun, 14 Jun 2015 21:07:39 -0700 Subject: [PATCH] refactor(mocks): extract from specs --- test/mocks/child-process.js | 16 +++++++++++++++ test/mocks/registry.js | 20 +++++++++++++++++++ test/specs/commits.js | 18 ++--------------- test/specs/last-release.js | 40 ++++++++++--------------------------- test/specs/type.js | 2 +- 5 files changed, 49 insertions(+), 47 deletions(-) create mode 100644 test/mocks/child-process.js create mode 100644 test/mocks/registry.js diff --git a/test/mocks/child-process.js b/test/mocks/child-process.js new file mode 100644 index 00000000..d539c49a --- /dev/null +++ b/test/mocks/child-process.js @@ -0,0 +1,16 @@ +const rawCommits = [ + 'hash-one==SPLIT==commit-one==END==\n', + 'hash-two==SPLIT==commit-two==END==\n' +] + +module.exports = { + exec: (command, cb) => { + cb( + null, + /\.\.HEAD/.test(command) ? + rawCommits[0] : + rawCommits.join() + ) + }, + '@noCallThru': true +} diff --git a/test/mocks/registry.js b/test/mocks/registry.js new file mode 100644 index 00000000..9a326fff --- /dev/null +++ b/test/mocks/registry.js @@ -0,0 +1,20 @@ +const nock = require('nock') + +const availableModule = { + 'dist-tags': { + latest: '1.33.7' + }, + versions: { + '1.33.7': { + gitHead: 'HEAD' + } + } +} + +module.exports = nock('http://registry.npmjs.org') + .get('/available') + .reply(200, availableModule) + .get('/@scoped/available') + .reply(200, availableModule) + .get('/unavailable') + .reply(404, {}) diff --git a/test/specs/commits.js b/test/specs/commits.js index 1b6a1100..6a04fa33 100644 --- a/test/specs/commits.js +++ b/test/specs/commits.js @@ -1,22 +1,8 @@ const test = require('tap').test const proxyquire = require('proxyquire') -const rawCommits = [ - 'hash-one==SPLIT==commit-one==END==\n', - 'hash-two==SPLIT==commit-two==END==\n' -] -const commits = proxyquire('../../dist/lib/commits.js', { - 'child_process': { - exec: (command, cb) => { - cb( - null, - /\.\.HEAD/.test(command) ? - rawCommits[0] : - rawCommits.join() - ) - }, - '@noCallThru': true - } +const commits = proxyquire('../../dist/lib/commits', { + 'child_process': require('../mocks/child-process') }) test('commits since last release', (t) => { diff --git a/test/specs/last-release.js b/test/specs/last-release.js index 62b72b2d..7dfa86df 100644 --- a/test/specs/last-release.js +++ b/test/specs/last-release.js @@ -1,35 +1,18 @@ const test = require('tap').test -const nock = require('nock') -const lastRelease = require('../../dist/lib/last-release.js') +require('../mocks/registry') +const lastRelease = require('../../dist/lib/last-release') -const availableModule = { - 'dist-tags': { - latest: '1.33.7' - }, - versions: { - '1.33.7': { - gitHead: 'HEAD' - } - } +const npmConfig = { + registry: 'http://registry.npmjs.org' } -const registry = 'http://registry.npmjs.org' -const regMock = nock(registry) - .get('/available') - .reply(200, availableModule) - .get('/@scoped/available') - .reply(200, availableModule) - .get('/unavailable') - .reply(404, {}) - test('last release from registry', (t) => { t.test('get release from package name', (t) => { lastRelease({ name: 'available' - }, { - registry - }, (err, release) => { + }, npmConfig, + (err, release) => { t.error(err) t.is(release.version, '1.33.7', 'version') t.is(release.gitHead, 'HEAD', 'gitHead') @@ -39,9 +22,8 @@ test('last release from registry', (t) => { t.test('get release from scoped package name', (t) => { lastRelease({ name: '@scoped/available' - }, { - registry - }, (err, release) => { + }, npmConfig, + (err, release) => { t.error(err) t.is(release.version, '1.33.7', 'version') t.is(release.gitHead, 'HEAD', 'gitHead') @@ -51,12 +33,10 @@ test('last release from registry', (t) => { t.test('get nothing from not yet published package name', (t) => { lastRelease({ name: 'unavailable' - }, { - registry - }, (err, release) => { + }, npmConfig, + (err, release) => { t.error(err) t.is(release.version, undefined, 'no version') - regMock.done() }) }) }) diff --git a/test/specs/type.js b/test/specs/type.js index c3eb8e13..c32d8251 100644 --- a/test/specs/type.js +++ b/test/specs/type.js @@ -1,6 +1,6 @@ const test = require('tap').test -const type = require('../../dist/lib/type.js') +const type = require('../../dist/lib/type') test('get type from commits', (t) => { t.test('get type from plugin', (t) => {