Nicola Peduzzi
8 years ago
5 changed files with 237 additions and 5 deletions
@ -1,2 +1,3 @@ |
|||||
node_modules |
node_modules |
||||
example |
examples |
||||
|
tests |
||||
|
@ -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(), |
||||
|
}); |
@ -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…
Reference in new issue