Browse Source

Covered validate module

master
Nicola Peduzzi 8 years ago
parent
commit
8e0a5edc82
  1. 3
      .npmignore
  2. 2
      lib/utils.js
  3. 5
      package.json
  4. 14
      tests/fs-extra.mock.js
  5. 218
      tests/validate.test.js

3
.npmignore

@ -1,2 +1,3 @@
node_modules node_modules
example examples
tests

2
lib/utils.js

@ -1,3 +1,5 @@
'use strict';
function guid() { function guid() {
function s4() { function s4() {
return Math.floor((1 + Math.random()) * 0x10000) return Math.floor((1 + Math.random()) * 0x10000)

5
package.json

@ -35,6 +35,9 @@
"chai": "^3.5.0", "chai": "^3.5.0",
"istanbul": "^0.4.4", "istanbul": "^0.4.4",
"mocha": "^3.0.2", "mocha": "^3.0.2",
"sinon": "^1.17.5" "mockery": "^1.7.0",
"serverless": "^1.0.0-beta.2",
"sinon": "^1.17.5",
"sinon-chai": "^2.8.0"
} }
} }

14
tests/fs-extra.mock.js

@ -0,0 +1,14 @@
'use strict';
const sinon = require('sinon');
module.exports = () => ({
_resetSpies() {
for (let p in this) {
if (this.hasOwnProperty(p) && p !== '_resetSpies') {
this[p].reset();
}
}
},
removeSync: sinon.spy(),
});

218
tests/validate.test.js

@ -1,10 +1,222 @@
'use strict'; 'use strict';
const expect = require('chai').expect; const chai = require('chai');
const validate = require('../lib/validate'); const sinon = require('sinon');
const mockery = require('mockery');
const Serverless = require('serverless');
const path = require('path');
const makeFsExtraMock = require('./fs-extra.mock');
chai.use(require('sinon-chai'));
const expect = chai.expect;
describe('validate', () => { describe('validate', () => {
let fsExtraMock;
let baseModule;
let module;
let serverless;
before(() => {
mockery.enable({ warnOnUnregistered: false });
fsExtraMock = makeFsExtraMock();
mockery.registerMock('fs-extra', fsExtraMock);
baseModule = require('../lib/validate');
});
after(() => {
mockery.disable();
mockery.deregisterAll();
});
beforeEach(() => {
serverless = new Serverless();
fsExtraMock._resetSpies();
module = Object.assign({
serverless,
options: {},
}, baseModule);
});
it('should expose a `validate` method', () => { it('should expose a `validate` method', () => {
expect(validate.validate).to.be.a('function'); expect(module.validate).to.be.a('function');
});
it('should set `webpackConfig` in the context to `custom.webpack` option', () => {
const testConfig = {
entry: 'test',
context: 'testcontext',
output: {},
};
module.serverless.service.custom.webpack = testConfig;
return module
.validate()
.then(() => {
expect(module.webpackConfig).to.eql(testConfig);
});
});
it('should delete the output path', () => {
const testOutPath = 'test';
const testConfig = {
entry: 'test',
context: 'testcontext',
output: {
path: testOutPath,
},
};
module.serverless.service.custom.webpack = testConfig;
return module
.validate()
.then(() => {
expect(fsExtraMock.removeSync).to.have.been.calledWith(testOutPath);
});
});
it('should override the output path if `out` option is specified', () => {
const testConfig = {
entry: 'test',
context: 'testcontext',
output: {
path: 'originalpath',
filename: 'filename',
},
};
const testServicePath = 'testpath';
const testOptionsOut = 'testdir';
module.options.out = testOptionsOut;
module.serverless.config.servicePath = testServicePath;
module.serverless.service.custom.webpack = testConfig;
return module
.validate()
.then(() => {
expect(module.webpackConfig.output).to.eql({
path: `${testServicePath}/${testOptionsOut}`,
filename: 'filename',
});
});
});
it('should set a default `webpackConfig.context` if not present', () => {
const testConfig = {
entry: 'test',
output: {},
};
const testServicePath = 'testpath';
module.serverless.config.servicePath = testServicePath;
module.serverless.service.custom.webpack = testConfig;
return module
.validate()
.then(() => {
expect(module.webpackConfig.context).to.equal(testServicePath);
});
});
describe('default output', () => {
it('should set a default `webpackConfig.output` if not present', () => {
const testEntry = 'testentry';
const testConfig = {
entry: testEntry,
};
const testServicePath = 'testpath';
module.serverless.config.servicePath = testServicePath;
module.serverless.service.custom.webpack = testConfig;
return module
.validate()
.then(() => {
expect(module.webpackConfig.output).to.eql({
libraryTarget: 'commonjs',
path: `${testServicePath}/.webpack`,
filename: testEntry,
});
});
});
it('should set a default `webpackConfig.output.filename` if `entry` is an array', () => {
const testEntry = ['first', 'second', 'last'];
const testConfig = {
entry: testEntry,
};
const testServicePath = 'testpath';
module.serverless.config.servicePath = testServicePath;
module.serverless.service.custom.webpack = testConfig;
return module
.validate()
.then(() => {
expect(module.webpackConfig.output).to.eql({
libraryTarget: 'commonjs',
path: `${testServicePath}/.webpack`,
filename: 'last',
});
});
});
it('should set a default `webpackConfig.output.filename` if `entry` is not defined', () => {
const testConfig = {};
const testServicePath = 'testpath';
module.serverless.config.servicePath = testServicePath;
module.serverless.service.custom.webpack = testConfig;
return module
.validate()
.then(() => {
expect(module.webpackConfig.output).to.eql({
libraryTarget: 'commonjs',
path: `${testServicePath}/.webpack`,
filename: 'handler.js',
});
});
});
});
describe('config file load', () => {
it('should load a webpack config from file if `custom.webpack` is a string', () => {
const testConfig = 'testconfig'
const testServicePath = 'testpath';
const requiredPath = `${testServicePath}/${testConfig}`;
module.serverless.config.servicePath = testServicePath;
module.serverless.service.custom.webpack = testConfig;
serverless.utils.fileExistsSync = sinon.stub().returns(true);
const loadedConfig = {
entry: 'testentry',
};
mockery.registerMock(requiredPath, loadedConfig);
return module
.validate()
.then(() => {
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
expect(module.webpackConfig).to.eql(loadedConfig);
mockery.deregisterMock(requiredPath);
});
});
it('should throw if providing an invalid file', () => {
const testConfig = 'testconfig'
const testServicePath = 'testpath';
const requiredPath = `${testServicePath}/${testConfig}`;
module.serverless.config.servicePath = testServicePath;
module.serverless.service.custom.webpack = testConfig;
serverless.utils.fileExistsSync = sinon.stub().returns(false);
const loadedConfig = {
entry: 'testentry',
};
expect(module.validate.bind(module)).to.throw(/could not find/);
});
it('should load a default file if no custom config is provided', () => {
const testConfig = 'webpack.config.js';
const testServicePath = 'testpath';
const requiredPath = `${testServicePath}/${testConfig}`;
module.serverless.config.servicePath = testServicePath;
serverless.utils.fileExistsSync = sinon.stub().returns(true);
const loadedConfig = {
entry: 'testentry',
};
mockery.registerMock(requiredPath, loadedConfig);
return module
.validate()
.then(() => {
expect(serverless.utils.fileExistsSync).to.have.been.calledWith(requiredPath);
expect(module.webpackConfig).to.eql(loadedConfig);
mockery.deregisterMock(requiredPath);
});
});
}); });
}); });

Loading…
Cancel
Save