Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added unit tests for file references
  • Loading branch information
Frank Schmid committed Nov 17, 2017
commit 9a509414570ff6b52dba22dea093824f8ceee6c1
33 changes: 33 additions & 0 deletions tests/mocks/packageLocalRef.mock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"dependencies": {
"archiver": "^2.0.0",
"bluebird": "^3.4.0",
"fs-extra": "^0.26.7",
"glob": "^7.1.2",
"localmodule": "file:../../mymodule",
"lodash": "^4.17.4",
"npm-programmatic": "0.0.5",
"uuid": "^5.4.1",
"ts-node": "^3.2.0",
"@scoped/vendor": "1.0.0",
"pg": "^4.3.5"
},
"devDependencies": {
"babel-eslint": "^7.2.3",
"chai": "^4.1.0",
"chai-as-promised": "^7.1.1",
"eslint": "^4.3.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-lodash": "^2.4.4",
"eslint-plugin-promise": "^3.5.0",
"istanbul": "^0.4.5",
"mocha": "^3.4.2",
"mockery": "^2.1.0",
"serverless": "^1.17.0",
"sinon": "^2.3.8",
"sinon-chai": "^2.12.0"
},
"peerDependencies": {
"webpack": "*"
}
}
101 changes: 101 additions & 0 deletions tests/packExternalModules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Serverless = require('serverless');
const childProcessMockFactory = require('./mocks/child_process.mock');
const fsExtraMockFactory = require('./mocks/fs-extra.mock');
const packageMock = require('./mocks/package.mock.json');
const packageLocalRefMock = require('./mocks/packageLocalRef.mock.json');

chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));
Expand Down Expand Up @@ -151,6 +152,50 @@ describe('packExternalModules', () => {
}
]
};
const statsWithFileRef = {
stats: [
{
compilation: {
chunks: [
{
modules: [
{
identifier: _.constant('"crypto"')
},
{
identifier: _.constant('"uuid/v4"')
},
{
identifier: _.constant('external "eslint"')
},
{
identifier: _.constant('"mockery"')
},
{
identifier: _.constant('"@scoped/vendor/module1"')
},
{
identifier: _.constant('external "@scoped/vendor/module2"')
},
{
identifier: _.constant('external "uuid/v4"')
},
{
identifier: _.constant('external "localmodule"')
},
{
identifier: _.constant('external "bluebird"')
},
]
}
],
compiler: {
outputPath: '/my/Service/Path/.webpack/service'
}
}
}
]
};

it('should do nothing if webpackIncludeModules is not set', () => {
_.unset(serverless, 'service.custom.webpackIncludeModules');
Expand Down Expand Up @@ -212,6 +257,62 @@ describe('packExternalModules', () => {
]));
});

it('should rebase file references', () => {
const expectedCompositePackageJSON = {
name: 'test-service',
version: '1.0.0',
description: 'Packaged externals for test-service',
private: true,
dependencies: {
'@scoped/vendor': '1.0.0',
uuid: '^5.4.1',
localmodule: 'file:../../locals/../../mymodule',
bluebird: '^3.4.0'
}
};
const expectedPackageJSON = {
dependencies: {
'@scoped/vendor': '1.0.0',
uuid: '^5.4.1',
localmodule: 'file:../../locals/../../mymodule',
bluebird: '^3.4.0'
}
};

_.set(serverless, 'service.custom.webpackIncludeModules.packagePath', path.join('locals', 'package.json'));
module.webpackOutputPath = 'outputPath';
fsExtraMock.pathExists.yields(null, false);
fsExtraMock.copy.yields();
childProcessMock.exec.onFirstCall().yields(null, '{}', '');
childProcessMock.exec.onSecondCall().yields(null, '', '');
childProcessMock.exec.onThirdCall().yields();
module.compileStats = statsWithFileRef;

sandbox.stub(process, 'cwd').returns(path.join('/my/Service/Path'));
mockery.registerMock(path.join(process.cwd(), 'locals', 'package.json'), packageLocalRefMock);

return expect(module.packExternalModules()).to.be.fulfilled
.then(() => BbPromise.all([
// The module package JSON and the composite one should have been stored
expect(writeFileSyncStub).to.have.been.calledTwice,
expect(writeFileSyncStub.firstCall.args[1]).to.equal(JSON.stringify(expectedCompositePackageJSON, null, 2)),
expect(writeFileSyncStub.secondCall.args[1]).to.equal(JSON.stringify(expectedPackageJSON, null, 2)),
// The modules should have been copied
expect(fsExtraMock.copy).to.have.been.calledOnce,
// npm ls and npm prune should have been called
expect(childProcessMock.exec).to.have.been.calledThrice,
expect(childProcessMock.exec.firstCall).to.have.been.calledWith(
'npm ls -prod -json -depth=1'
),
expect(childProcessMock.exec.secondCall).to.have.been.calledWith(
'npm install'
),
expect(childProcessMock.exec.thirdCall).to.have.been.calledWith(
'npm prune'
)
]));
});

it('should skip module copy for Google provider', () => {
const expectedCompositePackageJSON = {
name: 'test-service',
Expand Down