Skip to content

Commit

Permalink
Add Package unit tests and unit test script
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTaveras1231 committed Nov 21, 2016
1 parent 9dab304 commit 1c4ce24
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 2 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"bin": "./bin/npm-sass",
"scripts": {
"test": "NODE_PATH=$NODE_PATH:\"$(git rev-parse --show-toplevel)/test/fixtures/node_modules\" mocha test/index.js"
"test": "NODE_PATH=$NODE_PATH:\"$(git rev-parse --show-toplevel)/test/fixtures/node_modules\" mocha test/index.js",
"unit-test": "mocha test/units/*.js"
},
"repository": {
"type": "git",
Expand All @@ -28,6 +29,7 @@
"devDependencies": {
"chai": "^3.5.0",
"css": "^2.2.1",
"mocha": "^3.0.2"
"mocha": "^3.0.2",
"sinon": "^1.17.6"
}
}
150 changes: 150 additions & 0 deletions test/units/package-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
var Package = require('../../lib/resolver/package');
var sinon = require('sinon');
var assert = require('assert');

describe('Package', function () {
var subject;

describe('#json', function () {
it('returns the package.json file for the package', function () {
var requireStub = sinon.stub();
var result;

requireStub.withArgs('test-package/package.json')
.returns('the package.json object');

subject = new Package('test-package', requireStub);

result = subject.json();

assert(requireStub.calledWith('test-package/package.json'));
assert.equal(result, 'the package.json object');
});
});

describe('#resolve', function () {
it('resolves a path that is local to the package', function () {
var requireMock = {
resolve: sinon.stub()
};

requireMock.resolve.withArgs('test-package/given-path').returns('npm resolved path');

var subject = new Package('test-package', requireMock);

var result = subject.resolve('given-path');

assert.equal(result, 'npm resolved path');
assert(requireMock.resolve.calledWith('test-package/given-path'));
});
});

describe('#safeResolve', function () {
it('resolves a path that is local to the package (but does not error out if it cannot find it)', function () {
var requireMock = {
resolve: sinon.stub()
};

requireMock.resolve.withArgs('test-package/package.json').returns('/a/b/c/test-package/package.json');

var subject = new Package('test-package', requireMock);

var result = subject.safeResolve('given-path');

assert.equal(result, '/a/b/c/test-package/given-path');
});
});

describe('#dir', function () {
it('returns the directory of the package', function () {
var requireMock = {
resolve: sinon.stub()
};

requireMock.resolve.withArgs('test-package/package.json').returns('/a/b/c/test-package/package.json');

var subject = new Package('test-package', requireMock);

var result = subject.dir();

assert.equal(result, '/a/b/c/test-package');
});
});

describe('#entrypoint', function () {
var requireStub

beforeEach(function () {
requireStub = sinon.stub();
requireStub.withArgs('test-package/package.json');
});

describe('sass', function () {
it('returns the entrypoint file', function () {
requireStub.returns({ sass: 'package-entrypoint' });

var subject = new Package('test-package', requireStub);

var result = subject.entrypoint();

assert.equal(result, 'package-entrypoint');
});
});

describe('style', function () {
it('returns the entrypoint file', function () {
requireStub.returns({ sass: null, style: 'package-entrypoint' });

var subject = new Package('test-package', requireStub);

var result = subject.entrypoint();

assert.equal(result, 'package-entrypoint');
});
});

describe('main', function () {
it('works with scss', function () {
requireStub.returns({ sass: null, style: null, main: 'index.scss' });

var subject = new Package('test-package', requireStub);

var result = subject.entrypoint();

assert.equal(result, 'index.scss');
});

it('works with css', function () {
requireStub.returns({ sass: null, style: null, main: 'index.css' });

var subject = new Package('test-package', requireStub);

var result = subject.entrypoint();

assert.equal(result, 'index.css');
});

it('works with sass', function () {
requireStub.returns({ sass: null, style: null, main: 'index.sass' });

var subject = new Package('test-package', requireStub);

var result = subject.entrypoint();

assert.equal(result, 'index.sass');
});
});

describe('not specified', function () {
it('falls back to styles', function () {
requireStub.returns({ sass: null, style: null, main: null });

var subject = new Package('test-package', requireStub);

var result = subject.entrypoint();

assert.equal(result, 'styles');
});
});
});
});

0 comments on commit 1c4ce24

Please sign in to comment.