Skip to content

Commit

Permalink
Merge pull request #6208 from serverless/upgrade-chai
Browse files Browse the repository at this point in the history
Tests: Upgrade dependencies, improve isolation and experience on Windows
  • Loading branch information
medikoo committed Jun 5, 2019
2 parents a8ffc21 + 93a632f commit 3b77632
Show file tree
Hide file tree
Showing 27 changed files with 308 additions and 157 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
- name: "Unit Tests - Windows - Node.js v12"
os: windows
node_js: 12
env:
- SLS_IGNORE_WARNING=*
- FORCE_COLOR=1 # For some reason on Windows colors support is not detected
before_script:
# Ensure Python 2 in Windows enviroment (Ruby is already preinstalled)
- |
Expand Down
12 changes: 9 additions & 3 deletions lib/classes/CLI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,10 @@ describe('CLI', () => {
});

it('should print general --help to stdout', (done) => {
exec(`${this.serverlessExec} --help`, (err, stdout) => {
exec(`${this.serverlessExec} --help`, (err, stdout, stderr) => {
if (err) {
process.stdout.write(stdout);
process.stderr.write(stderr);
done(err);
return;
}
Expand All @@ -647,8 +649,10 @@ describe('CLI', () => {
});

it('should print command --help to stdout', (done) => {
exec(`${this.serverlessExec} deploy --help`, (err, stdout) => {
exec(`${this.serverlessExec} deploy --help`, (err, stdout, stderr) => {
if (err) {
process.stdout.write(stdout);
process.stderr.write(stderr);
done(err);
return;
}
Expand All @@ -660,8 +664,10 @@ describe('CLI', () => {
});

it('should print help --verbose to stdout', (done) => {
exec(`${this.serverlessExec} help --verbose`, (err, stdout) => {
exec(`${this.serverlessExec} help --verbose`, (err, stdout, stderr) => {
if (err) {
process.stdout.write(stdout);
process.stderr.write(stderr);
done(err);
return;
}
Expand Down
4 changes: 1 addition & 3 deletions lib/classes/Error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,14 @@ describe('Error', () => {
});

it('should re-throw error when handling raises an exception itself', () => {
const handlingError = new Error('an unexpected error');

let thrownError = null;
try {
logError('INVALID INPUT');
} catch (e) {
thrownError = e;
}

expect(thrownError).to.deep.equal(handlingError);
expect(thrownError.message).to.equal('INVALID INPUT');
});
});

Expand Down
44 changes: 19 additions & 25 deletions lib/classes/PluginManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,12 @@ describe('PluginManager', () => {
it('should log a warning when trying to load unknown plugin with help flag', () => {
const consoleLogStub = sinon.stub(pluginManager.serverless.cli, 'log').returns();
const servicePlugins = ['ServicePluginMock3', 'ServicePluginMock1'];
const servicePluginMock1 = new ServicePluginMock1();

pluginManager.setCliOptions({ help: true });
pluginManager.loadPlugins(servicePlugins);

expect(pluginManager.plugins).to.contain(servicePluginMock1);
expect(pluginManager.plugins
.some(plugin => plugin instanceof ServicePluginMock1)).to.equal(true);
expect(consoleLogStub.calledOnce).to.equal(true);
});

Expand Down Expand Up @@ -722,9 +722,7 @@ describe('PluginManager', () => {
mockRequire.stop('ServicePluginMock1');
});
});
describe('#asyncPluginInit()', function () {
this.timeout(5000);

describe('#asyncPluginInit()', () => {
it('should call async init on plugins that have it', () => {
const plugin1 = new ServicePluginMock1();
plugin1.asyncInit = sinon.stub().returns(BbPromise.resolve());
Expand All @@ -735,9 +733,7 @@ describe('PluginManager', () => {
});
});

describe('#loadAllPlugins()', function () {
this.timeout(5000);

describe('#loadAllPlugins()', () => {
beforeEach(function () { // eslint-disable-line prefer-arrow-callback
mockRequire('ServicePluginMock1', ServicePluginMock1);
mockRequire('ServicePluginMock2', ServicePluginMock2);
Expand All @@ -757,13 +753,12 @@ describe('PluginManager', () => {
const servicePlugins = ['ServicePluginMock1', 'ServicePluginMock2'];
pluginManager.loadAllPlugins(servicePlugins);

const servicePluginMock1 = new ServicePluginMock1();
const servicePluginMock2 = new ServicePluginMock2();
const enterprisePluginMock = new EnterprisePluginMock();

expect(pluginManager.plugins).to.contain(servicePluginMock1);
expect(pluginManager.plugins).to.contain(servicePluginMock2);
expect(pluginManager.plugins).to.contain(enterprisePluginMock);
expect(pluginManager.plugins
.some(plugin => plugin instanceof ServicePluginMock1)).to.equal(true);
expect(pluginManager.plugins
.some(plugin => plugin instanceof ServicePluginMock2)).to.equal(true);
expect(pluginManager.plugins
.some(plugin => plugin instanceof EnterprisePluginMock)).to.equal(true);
// note: this test will be refactored as the Create plugin will be moved
// to another directory
expect(pluginManager.plugins.length).to.be.above(2);
Expand Down Expand Up @@ -815,11 +810,10 @@ describe('PluginManager', () => {
const servicePlugins = ['ServicePluginMock1', 'ServicePluginMock2'];
pluginManager.loadServicePlugins(servicePlugins);

const servicePluginMock1 = new ServicePluginMock1();
const servicePluginMock2 = new ServicePluginMock2();

expect(pluginManager.plugins).to.contain(servicePluginMock1);
expect(pluginManager.plugins).to.contain(servicePluginMock2);
expect(pluginManager.plugins
.some(plugin => plugin instanceof ServicePluginMock1)).to.equal(true);
expect(pluginManager.plugins
.some(plugin => plugin instanceof ServicePluginMock2)).to.equal(true);
});

it('should not error if plugins = null', () => {
Expand Down Expand Up @@ -1590,20 +1584,20 @@ describe('PluginManager', () => {

const commands = pluginManager.getCommands();
expect(commands).to.have.a.property('mycmd');
expect(commands).to.have.a.deep.property('mycmd.commands.mysubcmd');
expect(commands).to.have.a.deep.property('mycmd.commands.spawncmd');
expect(commands).to.have.a.nested.property('mycmd.commands.mysubcmd');
expect(commands).to.have.a.nested.property('mycmd.commands.spawncmd');
// Check for omitted entrypoints
expect(commands).to.not.have.a.property('myep');
expect(commands).to.not.have.a.deep.property('myep.commands.mysubep');
expect(commands).to.not.have.a.deep.property('mycmd.commands.spawnep');
expect(commands).to.not.have.a.nested.property('myep.commands.mysubep');
expect(commands).to.not.have.a.nested.property('mycmd.commands.spawnep');
});

it('should return aliases', () => {
pluginManager.addPlugin(AliasPluginMock);

const commands = pluginManager.getCommands();
expect(commands).to.have.a.property('on')
.that.has.a.deep.property('commands.premise');
.that.has.a.nested.property('commands.premise');
expect(commands).to.have.a.property('premise');
});
});
Expand Down
8 changes: 4 additions & 4 deletions lib/classes/Service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ describe('Service', () => {
const serverless = new Serverless();
const noService = new Service(serverless);

return expect(noService.load()).to.eventually.resolve;
return expect(noService.load()).to.be.fulfilled;
});

it('should load serverless.yml from filesystem', function () {
this.timeout(3000);
it('should load serverless.yml from filesystem', () => {
const SUtils = new Utils();
const serverlessYml = {
service: 'new-service',
Expand Down Expand Up @@ -735,7 +734,8 @@ describe('Service', () => {
}

it(`should not throw an error if http event is absent and
stage contains only alphanumeric, underscore and hyphen`, () => {
stage contains only alphanumeric, underscore and hyphen`, function () {
this.timeout(10000); // Occasionally times out with default settings
const SUtils = new Utils();
const serverlessYml = {
service: 'new-service',
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('Utils', () => {

expect(() => {
serverless.utils.readFileSync(tmpFilePath);
}).to.throw(new RegExp('YAMLException:.*invalid.yml'));
}).to.throw(/.*invalid.yml/);
});
});

Expand Down
10 changes: 8 additions & 2 deletions lib/classes/Variables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const slsError = require('./Error');
const Utils = require('../../lib/classes/Utils');
const Variables = require('../../lib/classes/Variables');
const { getTmpDirPath } = require('../../tests/utils/fs');
const { skipOnWindowsDisabledSymlinks } = require('../../tests/utils/misc');

BbPromise.longStackTraces(true);

Expand Down Expand Up @@ -1669,13 +1670,18 @@ module.exports = {
.should.become('hello world');
});

it('should populate symlinks', () => {
it('should populate symlinks', function () {
const SUtils = new Utils();
const tmpDirPath = getTmpDirPath();
const realFilePath = path.join(tmpDirPath, 'someFile');
const symlinkPath = path.join(tmpDirPath, 'refSomeFile');
SUtils.writeFileSync(realFilePath, 'hello world');
fse.ensureSymlinkSync(realFilePath, symlinkPath);
try {
fse.ensureSymlinkSync(realFilePath, symlinkPath);
} catch (error) {
skipOnWindowsDisabledSymlinks(error, this);
throw error;
}
serverless.config.update({ servicePath: tmpDirPath });
return serverless.variables.getValueFromFile('file(./refSomeFile)')
.should.become('hello world')
Expand Down
6 changes: 3 additions & 3 deletions lib/classes/YamlParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('YamlParser', () => {
serverless.utils.writeFileSync(path.join(tmpDirPath, 'test.yml'), testYml);

return expect(serverless.yamlParser.parse(path.join(tmpDirPath, 'test.yml')))
.to.eventually.have.deep.property('main.foo').to.equal('bar');
.to.eventually.have.nested.property('main.foo').to.equal('bar');
});

it('should parse a .yml file with JSON-REF to JSON', () => {
Expand All @@ -67,7 +67,7 @@ describe('YamlParser', () => {
serverless.utils.writeFileSync(path.join(tmpDirPath, 'test.yml'), testYml);

return expect(serverless.yamlParser.parse(path.join(tmpDirPath, 'test.yml')))
.to.eventually.have.deep.property('main.foo').to.equal('bar');
.to.eventually.have.nested.property('main.foo').to.equal('bar');
});

it('should parse a .yml file with recursive JSON-REF', () => {
Expand All @@ -92,7 +92,7 @@ describe('YamlParser', () => {
serverless.utils.writeFileSync(path.join(tmpDirPath, 'one.yml'), oneYml);

return expect(serverless.yamlParser.parse(path.join(tmpDirPath, 'one.yml')))
.to.eventually.have.deep.property('one.two.foo').to.equal('bar');
.to.eventually.have.nested.property('one.two.foo').to.equal('bar');
});
});
});
10 changes: 5 additions & 5 deletions lib/plugins/aws/common/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,34 @@ describe('AwsCommon', () => {

describe('commands', () => {
it('should be only entrypoints', () => {
expect(awsCommon.commands).to.have.deep.property('aws.type', 'entrypoint');
expect(awsCommon.commands).to.have.nested.property('aws.type', 'entrypoint');
});

describe('aws:common:validate', () => {
it('should exist', () => {
expect(awsCommon.commands)
.to.have.deep.property('aws.commands.common.commands.validate');
.to.have.nested.property('aws.commands.common.commands.validate');
});
});

describe('aws:common:cleanupTempDir', () => {
it('should exist', () => {
expect(awsCommon.commands)
.to.have.deep.property('aws.commands.common.commands.cleanupTempDir');
.to.have.nested.property('aws.commands.common.commands.cleanupTempDir');
});
});

describe('aws:common:moveArtifactsToPackage', () => {
it('should exist', () => {
expect(awsCommon.commands)
.to.have.deep.property('aws.commands.common.commands.moveArtifactsToPackage');
.to.have.nested.property('aws.commands.common.commands.moveArtifactsToPackage');
});
});

describe('aws:common:moveArtifactsToTemp', () => {
it('should exist', () => {
expect(awsCommon.commands)
.to.have.deep.property('aws.commands.common.commands.moveArtifactsToTemp');
.to.have.nested.property('aws.commands.common.commands.moveArtifactsToTemp');
});
});
});
Expand Down
3 changes: 1 addition & 2 deletions lib/plugins/aws/deployFunction/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ describe('AwsDeployFunction', () => {
let awsDeployFunction;
let cryptoStub;

beforeEach(function () {
this.timeout(3000);
beforeEach(() => {
serverless = new Serverless();
serverless.servicePath = true;
serverless.service.environment = {
Expand Down

0 comments on commit 3b77632

Please sign in to comment.