Skip to content

Commit

Permalink
test: Simplify npm mock
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Nov 29, 2017
1 parent b0635c3 commit ddc2f97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
25 changes: 15 additions & 10 deletions test/get-last-release.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import nock from 'nock';
import {stub} from 'sinon';
import tempy from 'tempy';
import lastRelease from '../lib/get-last-release';
import {registry, mock, available, unpublished} from './helpers/mock-registry';
import {registry, mock, availableModule, unpublishedModule} from './helpers/mock-registry';

let processStdout;
let processStderr;
Expand Down Expand Up @@ -51,7 +51,7 @@ test.after.always(() => {

test.serial('Get release from package name', async t => {
const name = 'available';
const registryMock = available(name);
const registryMock = mock(name).reply(200, availableModule);
const release = await lastRelease({name, publishConfig: {registry}}, t.context.logger);

t.is(release.version, '1.33.7');
Expand All @@ -61,7 +61,7 @@ test.serial('Get release from package name', async t => {

test.serial("Get release from a tagged package's name", async t => {
const name = 'tagged';
const registryMock = available(name);
const registryMock = mock(name).reply(200, availableModule);
const release = await lastRelease({name, publishConfig: {registry, tag: 'foo'}}, t.context.logger);

t.is(release.version, '0.8.15');
Expand All @@ -71,7 +71,7 @@ test.serial("Get release from a tagged package's name", async t => {

test.serial('Get release from the latest fallback tag', async t => {
const name = 'tagged';
const registryMock = available(name);
const registryMock = mock(name).reply(200, availableModule);
const release = await lastRelease({name, publishConfig: {registry, tag: 'bar'}}, t.context.logger);

t.is(release.version, '1.33.7');
Expand All @@ -81,7 +81,7 @@ test.serial('Get release from the latest fallback tag', async t => {

test.serial('Get release from scoped package name', async t => {
const name = '@scoped/available';
const registryMock = available(name);
const registryMock = mock(name).reply(200, availableModule);

const release = await lastRelease({name, publishConfig: {registry}}, t.context.logger);
t.is(release.version, '1.33.7');
Expand All @@ -91,7 +91,7 @@ test.serial('Get release from scoped package name', async t => {

test.serial('Get nothing from completely unpublished package name', async t => {
const name = 'completely-unpublished';
const registryMock = unpublished(name);
const registryMock = mock(name).reply(200, unpublishedModule);
const release = await lastRelease({name, publishConfig: {registry}}, t.context.logger);

t.is(release.version, undefined);
Expand All @@ -118,7 +118,7 @@ test.serial('Get nothing from not yet published package name (unavailable w/o re

test.serial('Get registry from ".npmrc"', async t => {
const name = 'available';
const registryMock = available(name);
const registryMock = mock(name).reply(200, availableModule);
await appendFile('./.npmrc', `registry = ${registry}`);
const release = await lastRelease({name}, t.context.logger);

Expand All @@ -141,7 +141,10 @@ test.serial('Get nothing from not yet published package name (unavailable w/o st

test.serial('Send bearer authorization using NPM_TOKEN', async t => {
const name = 'available';
const registryMock = available(name, ['authorization', 'Bearer npm_token']);
const registryMock = mock(name, {reqheaders: {authorization: `Bearer ${process.env.NPM_TOKEN}`}}).reply(
200,
availableModule
);
await appendFile('./.npmrc', `registry = ${registry}\nalways-auth = true`);
const release = await lastRelease({name}, t.context.logger);

Expand All @@ -155,7 +158,9 @@ test.serial('Uses basic auth when always-auth=true in ".npmrc"', async t => {
delete process.env.NPM_TOKEN;
process.env.NPM_USERNAME = 'username';
process.env.NPM_PASSWORD = 'password';
const registryMock = available(name, null, {user: 'username', pass: 'password'});
const registryMock = mock(name)
.basicAuth({user: 'username', pass: 'password'})
.reply(200, availableModule);
await appendFile('./.npmrc', `registry = ${registry}\nalways-auth = true`);
const release = await lastRelease({name}, t.context.logger);

Expand All @@ -177,7 +182,7 @@ test.serial('Throws error on server error', async t => {

test.serial('Handle missing trailing slash on registry URL', async t => {
const name = 'available';
const registryMock = available(name);
const registryMock = mock(name).reply(200, availableModule);
const release = await lastRelease({name, publishConfig: {registry: 'http://registry.npmjs.org'}}, t.context.logger);

t.is(release.version, '1.33.7');
Expand Down
24 changes: 4 additions & 20 deletions test/helpers/mock-registry.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
import nock from 'nock';

const REGISTRY_URL = 'http://registry.npmjs.org/';
const availableModule = {
export const availableModule = {
'dist-tags': {latest: '1.33.7', foo: '0.8.15'},
versions: {'0.8.15': {gitHead: 'bar'}, '1.33.7': {gitHead: 'HEAD'}},
};
const unpublishedModule = {
export const unpublishedModule = {
name: 'i-am-completely-unpublished',
time: {'2.0.0': '2016-12-01T17:50:30.699Z', unpublished: {time: '2016-12-01T17:53:45.940Z'}},
};

export const registry = REGISTRY_URL;

export function mock(packageName, matchHeader, basicAuth) {
let req = nock(REGISTRY_URL);
if (matchHeader) {
req = req.matchHeader.apply(req, matchHeader);
}
req = req.get(`/${packageName.replace('/', '%2F')}`);
if (basicAuth) {
req = req.basicAuth(basicAuth);
}
return req;
}

export function available(packageName, matchHeader, basicAuth) {
return mock(packageName, matchHeader, basicAuth).reply(200, availableModule);
}

export function unpublished(packageName, matchHeader, basicAuth) {
return mock(packageName, matchHeader, basicAuth).reply(200, unpublishedModule);
export function mock(packageName, headers) {
return nock(REGISTRY_URL, headers).get(`/${packageName.replace('/', '%2F')}`);
}

0 comments on commit ddc2f97

Please sign in to comment.