Skip to content

Commit

Permalink
Added unit tests for package section copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Schmid committed Apr 30, 2018
1 parent f9896e6 commit 5793736
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/packagers/npm.test.js
Expand Up @@ -38,6 +38,10 @@ describe('npm', () => {
expect(npmModule.lockfileName).to.equal('package-lock.json');
});

it('should return no packager sections', () => {
expect(npmModule.copyPackageSectionNames).to.be.an('array').that.is.empty;
});

it('requires to copy modules', () => {
expect(npmModule.mustCopyModules).to.be.true;
});
Expand Down
4 changes: 4 additions & 0 deletions lib/packagers/yarn.test.js
Expand Up @@ -37,6 +37,10 @@ describe('yarn', () => {
expect(yarnModule.lockfileName).to.equal('yarn.lock');
});

it('should return packager sections', () => {
expect(yarnModule.copyPackageSectionNames).to.deep.equal(['resolutions']);
});

it('does not require to copy modules', () => {
expect(yarnModule.mustCopyModules).to.be.false;
});
Expand Down
70 changes: 70 additions & 0 deletions tests/packExternalModules.test.js
Expand Up @@ -33,6 +33,7 @@ const packagerMockFactory = {
create(sandbox) {
const packagerMock = {
lockfileName: 'mocked-lock.json',
copyPackageSectionNames: [ 'section1', 'section2' ],
mustCopyModules: true,
rebaseLockfile: sandbox.stub(),
getProdDependencies: sandbox.stub(),
Expand Down Expand Up @@ -243,6 +244,75 @@ describe('packExternalModules', () => {
]));
});

it('should copy needed package sections if available', () => {
const originalPackageJSON = {
name: 'test-service',
version: '1.0.0',
description: 'Packaged externals for test-service',
private: true,
scripts: {},
section1: {
value: 'myValue'
},
dependencies: {
'@scoped/vendor': '1.0.0',
uuid: '^5.4.1',
bluebird: '^3.4.0'
}
};
const expectedCompositePackageJSON = {
name: 'test-service',
version: '1.0.0',
description: 'Packaged externals for test-service',
private: true,
scripts: {},
section1: originalPackageJSON.section1,
dependencies: {
'@scoped/vendor': '1.0.0',
uuid: '^5.4.1',
bluebird: '^3.4.0'
}
};
const expectedPackageJSON = {
name: 'test-service',
version: '1.0.0',
description: 'Packaged externals for test-service',
private: true,
scripts: {},
dependencies: {
'@scoped/vendor': '1.0.0',
uuid: '^5.4.1',
bluebird: '^3.4.0'
},
section1: originalPackageJSON.section1
};

module.webpackOutputPath = 'outputPath';
readFileSyncStub.onFirstCall().returns(originalPackageJSON);
readFileSyncStub.throws(new Error('Unexpected call to readFileSync'));
fsExtraMock.pathExists.yields(null, false);
fsExtraMock.copy.yields();
packagerMock.getProdDependencies.returns(BbPromise.resolve({}));
packagerMock.install.returns(BbPromise.resolve());
packagerMock.prune.returns(BbPromise.resolve());
packagerMock.runScripts.returns(BbPromise.resolve());
module.compileStats = stats;
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(packagerMock.getProdDependencies).to.have.been.calledOnce,
expect(packagerMock.install).to.have.been.calledOnce,
expect(packagerMock.prune).to.have.been.calledOnce,
expect(packagerMock.runScripts).to.have.been.calledOnce,
]));
});

it('should install external modules', () => {
const expectedCompositePackageJSON = {
name: 'test-service',
Expand Down

0 comments on commit 5793736

Please sign in to comment.