Skip to content

Commit 9a50941

Browse files
author
Frank Schmid
committed
Added unit tests for file references
1 parent 3404001 commit 9a50941

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"dependencies": {
3+
"archiver": "^2.0.0",
4+
"bluebird": "^3.4.0",
5+
"fs-extra": "^0.26.7",
6+
"glob": "^7.1.2",
7+
"localmodule": "file:../../mymodule",
8+
"lodash": "^4.17.4",
9+
"npm-programmatic": "0.0.5",
10+
"uuid": "^5.4.1",
11+
"ts-node": "^3.2.0",
12+
"@scoped/vendor": "1.0.0",
13+
"pg": "^4.3.5"
14+
},
15+
"devDependencies": {
16+
"babel-eslint": "^7.2.3",
17+
"chai": "^4.1.0",
18+
"chai-as-promised": "^7.1.1",
19+
"eslint": "^4.3.0",
20+
"eslint-plugin-import": "^2.7.0",
21+
"eslint-plugin-lodash": "^2.4.4",
22+
"eslint-plugin-promise": "^3.5.0",
23+
"istanbul": "^0.4.5",
24+
"mocha": "^3.4.2",
25+
"mockery": "^2.1.0",
26+
"serverless": "^1.17.0",
27+
"sinon": "^2.3.8",
28+
"sinon-chai": "^2.12.0"
29+
},
30+
"peerDependencies": {
31+
"webpack": "*"
32+
}
33+
}

tests/packExternalModules.test.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Serverless = require('serverless');
1212
const childProcessMockFactory = require('./mocks/child_process.mock');
1313
const fsExtraMockFactory = require('./mocks/fs-extra.mock');
1414
const packageMock = require('./mocks/package.mock.json');
15+
const packageLocalRefMock = require('./mocks/packageLocalRef.mock.json');
1516

1617
chai.use(require('chai-as-promised'));
1718
chai.use(require('sinon-chai'));
@@ -151,6 +152,50 @@ describe('packExternalModules', () => {
151152
}
152153
]
153154
};
155+
const statsWithFileRef = {
156+
stats: [
157+
{
158+
compilation: {
159+
chunks: [
160+
{
161+
modules: [
162+
{
163+
identifier: _.constant('"crypto"')
164+
},
165+
{
166+
identifier: _.constant('"uuid/v4"')
167+
},
168+
{
169+
identifier: _.constant('external "eslint"')
170+
},
171+
{
172+
identifier: _.constant('"mockery"')
173+
},
174+
{
175+
identifier: _.constant('"@scoped/vendor/module1"')
176+
},
177+
{
178+
identifier: _.constant('external "@scoped/vendor/module2"')
179+
},
180+
{
181+
identifier: _.constant('external "uuid/v4"')
182+
},
183+
{
184+
identifier: _.constant('external "localmodule"')
185+
},
186+
{
187+
identifier: _.constant('external "bluebird"')
188+
},
189+
]
190+
}
191+
],
192+
compiler: {
193+
outputPath: '/my/Service/Path/.webpack/service'
194+
}
195+
}
196+
}
197+
]
198+
};
154199

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

260+
it('should rebase file references', () => {
261+
const expectedCompositePackageJSON = {
262+
name: 'test-service',
263+
version: '1.0.0',
264+
description: 'Packaged externals for test-service',
265+
private: true,
266+
dependencies: {
267+
'@scoped/vendor': '1.0.0',
268+
uuid: '^5.4.1',
269+
localmodule: 'file:../../locals/../../mymodule',
270+
bluebird: '^3.4.0'
271+
}
272+
};
273+
const expectedPackageJSON = {
274+
dependencies: {
275+
'@scoped/vendor': '1.0.0',
276+
uuid: '^5.4.1',
277+
localmodule: 'file:../../locals/../../mymodule',
278+
bluebird: '^3.4.0'
279+
}
280+
};
281+
282+
_.set(serverless, 'service.custom.webpackIncludeModules.packagePath', path.join('locals', 'package.json'));
283+
module.webpackOutputPath = 'outputPath';
284+
fsExtraMock.pathExists.yields(null, false);
285+
fsExtraMock.copy.yields();
286+
childProcessMock.exec.onFirstCall().yields(null, '{}', '');
287+
childProcessMock.exec.onSecondCall().yields(null, '', '');
288+
childProcessMock.exec.onThirdCall().yields();
289+
module.compileStats = statsWithFileRef;
290+
291+
sandbox.stub(process, 'cwd').returns(path.join('/my/Service/Path'));
292+
mockery.registerMock(path.join(process.cwd(), 'locals', 'package.json'), packageLocalRefMock);
293+
294+
return expect(module.packExternalModules()).to.be.fulfilled
295+
.then(() => BbPromise.all([
296+
// The module package JSON and the composite one should have been stored
297+
expect(writeFileSyncStub).to.have.been.calledTwice,
298+
expect(writeFileSyncStub.firstCall.args[1]).to.equal(JSON.stringify(expectedCompositePackageJSON, null, 2)),
299+
expect(writeFileSyncStub.secondCall.args[1]).to.equal(JSON.stringify(expectedPackageJSON, null, 2)),
300+
// The modules should have been copied
301+
expect(fsExtraMock.copy).to.have.been.calledOnce,
302+
// npm ls and npm prune should have been called
303+
expect(childProcessMock.exec).to.have.been.calledThrice,
304+
expect(childProcessMock.exec.firstCall).to.have.been.calledWith(
305+
'npm ls -prod -json -depth=1'
306+
),
307+
expect(childProcessMock.exec.secondCall).to.have.been.calledWith(
308+
'npm install'
309+
),
310+
expect(childProcessMock.exec.thirdCall).to.have.been.calledWith(
311+
'npm prune'
312+
)
313+
]));
314+
});
315+
215316
it('should skip module copy for Google provider', () => {
216317
const expectedCompositePackageJSON = {
217318
name: 'test-service',

0 commit comments

Comments
 (0)