Skip to content

Commit

Permalink
chore: migrate to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
trs committed Jul 13, 2018
1 parent 2c0e4c3 commit a0ec145
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 79 deletions.
4 changes: 1 addition & 3 deletions .config/eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
"extends": "eslint:recommended",
"env": {
"node": true,
"mocha": true,
"jest": true,
"es6": true
},
"plugins": [
"mocha",
"node"
],
"rules": {
"mocha/no-exclusive-tests": 2,
"no-warning-comments": [
1,
{
Expand Down
2 changes: 0 additions & 2 deletions .config/mocha.opts

This file was deleted.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
],
"bin": "bin/npmum.js",
"scripts": {
"test": "NODE_ENV=test mocha --opts ./.config/mocha.opts",
"test:watch": "npm run test -- -w",
"test": "NODE_ENV=test jest test/**/*.spec.js",
"test:watch": "npm run test -- --watch",
"lint": "eslint -c ./.config/eslintrc.json \"src/**/*.js\" \"test/**/*.js\" \"bin/**/*.js\"",
"lint:fix": "eslint --fix -c ./.config/eslintrc.json \"src/**/*.js\" \"test/**/*.js\" \"bin/**/*.js\"",
"commitmsg": "validate-commit-msg"
Expand All @@ -33,13 +33,11 @@
"npm": ">3"
},
"devDependencies": {
"chai": "^4.1.2",
"condition-circle": "^1.5.0",
"eslint": "^4.11.0",
"eslint-plugin-mocha": "^4.11.0",
"eslint-plugin-node": "^5.2.1",
"husky": "^0.14.3",
"mocha": "^4.0.1",
"jest": "^23.4.0",
"mock-stdin": "^0.3.1",
"semantic-release": "^15.4.1",
"sinon": "^4.1.2",
Expand Down
5 changes: 3 additions & 2 deletions src/commands/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const use = {
handle,

_readNpmrc,
_writeNpmrc
_writeNpmrc,
_resolveNpmrcPath
};

function _readNpmrc(path) {
Expand Down Expand Up @@ -38,7 +39,7 @@ function _resolveNpmrcPath(options) {

if (nodePath.basename(path) === NPMRC) return path;

return nodePath.join(path, nodePath.delimiter, NPMRC);
return nodePath.join(path, nodePath.sep, NPMRC);
}

function handle(name, options = {}) {
Expand Down
33 changes: 16 additions & 17 deletions test/commands/add.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const {expect} = require('chai');
const sinon = require('sinon');
const mockStdin = require('mock-stdin');

const add = require('../../src/commands/add');
const storage = require('../../src/storage');

describe('add', function () {
describe('add', () => {
let sandbox;
let stdin;

Expand All @@ -18,7 +17,7 @@ describe('add', function () {
sandbox.restore();
});

it('prompts for a token', () => {
test('prompts for a token', () => {
sandbox.stub(storage, 'getUser').returns(undefined);
sandbox.stub(storage, 'addUser').returns(true);

Expand All @@ -28,44 +27,44 @@ describe('add', function () {

return add.handle('test')
.then(success => {
expect(success).to.equal(true);
expect(storage.addUser.firstCall.args[0]).to.equal('test');
expect(storage.addUser.firstCall.args[1]).to.eql({token: '123'});
expect(success).toBe(true);
expect(storage.addUser.firstCall.args[0]).toBe('test');
expect(storage.addUser.firstCall.args[1]).toEqual({token: '123'});
});
});

it('allows token param', () => {
test('allows token param', () => {
sandbox.stub(storage, 'getUser').returns(undefined);
sandbox.stub(storage, 'addUser').returns(true);

return add.handle('test', {token: '123'})
.then(success => {
expect(success).to.equal(true);
expect(storage.addUser.firstCall.args[0]).to.equal('test');
expect(storage.addUser.firstCall.args[1]).to.eql({token: '123'});
expect(success).toBe(true);
expect(storage.addUser.firstCall.args[0]).toBe('test');
expect(storage.addUser.firstCall.args[1]).toEqual({token: '123'});
});
});

it('trims token', () => {
test('trims token', () => {
sandbox.stub(storage, 'getUser').returns(undefined);
sandbox.stub(storage, 'addUser').returns(true);

return add.handle('test', {token: ' 123 '})
.then(success => {
expect(success).to.equal(true);
expect(storage.addUser.firstCall.args[0]).to.equal('test');
expect(storage.addUser.firstCall.args[1]).to.eql({token: '123'});
expect(success).toBe(true);
expect(storage.addUser.firstCall.args[0]).toBe('test');
expect(storage.addUser.firstCall.args[1]).toEqual({token: '123'});
});
});

it('fails if user exists', () => {
test('fails if user exists', () => {
sandbox.stub(storage, 'getUser').returns({});
sandbox.spy(storage, 'addUser');

return add.handle('test')
.then(success => {
expect(success).to.equal(false);
expect(storage.addUser.called).to.equal(false);
expect(success).toBe(false);
expect(storage.addUser.called).toBe(false);
});
});
});
17 changes: 8 additions & 9 deletions test/commands/ls.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const {expect} = require('chai');
const sinon = require('sinon');

const ls = require('../../src/commands/ls');
const storage = require('../../src/storage');

describe('ls', function () {
describe('ls', () => {
let sandbox;

beforeEach(() => {
Expand All @@ -16,14 +15,14 @@ describe('ls', function () {
sandbox.restore();
});

describe('_mapUserTokens', function () {
it('truncates user tokens', () => {
describe('_mapUserTokens', () => {
test('truncates user tokens', () => {
const users = {
test: {token: '123'},
other: {token: '1234567890'}
};
const mappedUsers = ls._mapUserTokens(users);
expect(mappedUsers).to.eql([{
expect(mappedUsers).toEqual([{
name: 'test',
token: '123',
current: ''
Expand All @@ -35,20 +34,20 @@ describe('ls', function () {
});
});

it('does not loop through empty array', () => {
test('does not loop through empty array', () => {
sandbox.stub(storage, 'getUsers').returns({});

const success = ls.handle();
expect(success).to.equal(false);
expect(success).toBe(false);
});

it('writes column list of tokens', () => {
test('writes column list of tokens', () => {
sandbox.stub(storage, 'getUsers').returns({
test: {token: '123'},
other: {token: '1234567890'}
});

const success = ls.handle();
expect(success).to.equal(true);
expect(success).toBe(true);
});
});
13 changes: 6 additions & 7 deletions test/commands/rm.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const {expect} = require('chai');
const sinon = require('sinon');

const rm = require('../../src/commands/rm');
const storage = require('../../src/storage');

describe('rm', function () {
describe('rm', () => {
let sandbox;

beforeEach(() => {
Expand All @@ -14,22 +13,22 @@ describe('rm', function () {
sandbox.restore();
});

it('removes existing user', () => {
test('removes existing user', () => {
sandbox.stub(storage, 'getUsers').returns({
test: {}
});
sandbox.stub(storage, 'setUsers').returns();

const success = rm.handle('test');
expect(success).to.equal(true);
expect(success).toBe(true);
});

it('does not remove non-existant user', () => {
test('does not remove non-existant user', () => {
sandbox.stub(storage, 'getUsers').returns({});
sandbox.spy(storage, 'setUsers');

const success = rm.handle('test');
expect(success).to.equal(false);
expect(storage.setUsers.called).to.equal(false);
expect(success).toBe(false);
expect(storage.setUsers.called).toBe(false);
});
});
35 changes: 20 additions & 15 deletions test/commands/use.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const {expect} = require('chai');
const sinon = require('sinon');

const use = require('../../src/commands/use');
const storage = require('../../src/storage');

describe('use', function () {
describe('use', () => {
let sandbox;

beforeEach(() => {
Expand All @@ -14,51 +13,57 @@ describe('use', function () {
sandbox.restore();
});

it('fails if user does not exist', () => {
test('fails if user does not exist', () => {
sandbox.stub(storage, 'getUser').returns();

return use.handle('test')
.then(success => {
expect(success).to.equal(false);
expect(success).toBe(false);
});
});

it('fails if user does not have a token', () => {
test('fails if user does not have a token', () => {
sandbox.stub(storage, 'getUser').returns({});

return use.handle('test')
.then(success => {
expect(success).to.equal(false);
expect(success).toBe(false);
});
});

it('updates .npmrc', () => {
test('updates .npmrc', () => {
sandbox.stub(storage, 'getUser').returns({token: 'new-cool-token'});

sandbox.stub(use, '_readNpmrc').resolves('//registry.npmjs.org/:_authToken=super-awesome-fake-token');
sandbox.stub(use, '_writeNpmrc').resolves();

return use.handle('test')
.then(success => {
expect(success).to.equal(true);
expect(use._writeNpmrc.firstCall.args[1]).to.equal('//registry.npmjs.org/:_authToken=new-cool-token');
expect(success).toBe(true);
expect(use._writeNpmrc.firstCall.args[1]).toBe('//registry.npmjs.org/:_authToken=new-cool-token');
});
});

describe('._resolveNpmrcPath', () => {
it('test', () => {
test('path with .npmrc', () => {
const path = use._resolveNpmrcPath({path: '/test/.npmrc'});
expect(path).to.equal('/test/.npmrc');
expect(path).toBe('/test/.npmrc');
});

it('test', () => {
test('path without .npmrc', () => {
const path = use._resolveNpmrcPath({path: '/test'});
expect(path).to.equal('/test/.npmrc');
expect(path).toBe('/test/.npmrc');
});

it('test', () => {
test('path without .npmrc', () => {
const path = use._resolveNpmrcPath({path: '/test/'});
expect(path).to.equal('/test/.npmrc');
expect(path).toBe('/test/.npmrc');
});

test('path without .npmrc', () => {
const path = use._resolveNpmrcPath({local: true});
const cwd = process.cwd();
expect(path).toBe(`${cwd}/.npmrc`);
});
});
});
36 changes: 17 additions & 19 deletions test/storage.spec.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
const {expect} = require('chai');

const storage = require('../src/storage');

describe('storage', function () {
after(done => {
describe('storage', () => {
afterAll(done => {
require('fs').unlink(storage._path, done);
});

describe('addUser', function () {
it('adds new user', () => {
describe('addUser', () => {
test('adds new user', () => {
const success = storage.addUser('test', {data: 1});
expect(success).to.equal(true);
expect(success).toBe(true);
});

it('overrides existing user', () => {
test('overrides existing user', () => {
const success = storage.addUser('test', {data: 2});
expect(success).to.equal(true);
expect(success).toBe(true);
});
});

describe('getUser', function () {
it('returns existing user', () => {
describe('getUser', () => {
test('returns existing user', () => {
const user = storage.getUser('test');
expect(user).to.eql({data: 2});
expect(user).toEqual({data: 2});
});

it('returns undefined if user does not exist', () => {
test('returns undefined if user does not exist', () => {
const user = storage.getUser('bad');
expect(user).to.equal(undefined);
expect(user).toBe(undefined);
});
});

describe('removeUser', function () {
it('removes an existing user', () => {
describe('removeUser', () => {
test('removes an existing user', () => {
const success = storage.removeUser('test');
expect(success).to.equal(true);
expect(success).toBe(true);
});

it('checks for non existant user', () => {
test('checks for non existant user', () => {
const success = storage.removeUser('bad');
expect(success).to.equal(false);
expect(success).toBe(false);
});
});
});

0 comments on commit a0ec145

Please sign in to comment.