Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Change testing suite to follow lisk standards - Closes #448 #437

Merged
merged 6 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"babel-plugin-istanbul": "=4.1.5",
"babel-preset-env": "=1.6.1",
"babel-register": "=6.26.0",
"chai": "=4.1.2",
"chai-as-promised": "=7.1.1",
"coveralls": "=3.0.0",
"eslint": "=4.11.0",
"eslint-config-airbnb-base": "=12.1.0",
Expand All @@ -78,8 +80,7 @@
"mocha-bdd": "=0.1.2",
"nyc": "=11.3.0",
"prettier": "=1.9.2",
"should": "=13.1.3",
"should-sinon": "=0.0.6",
"sinon": "=4.1.2"
"sinon": "=4.1.2",
"sinon-chai": "=2.14.0"
}
}
40 changes: 26 additions & 14 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,41 @@
*/
import os from 'os';
import 'babel-polyfill';
import should from 'should';
import chai, { Assertion } from 'chai';
import 'chai/register-should';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
import sinon from 'sinon';
import 'should-sinon';
import mochaBDD from 'mocha-bdd';

process.env.NODE_ENV = 'test';
process.env.LISKY_CONFIG_DIR =
process.env.LISKY_CONFIG_DIR || `${os.homedir()}/.lisky`;

should.use((_, Assertion) => {
// istanbul ignore next
Assertion.add('hexString', function hexString() {
this.params = {
operator: 'to be hex string',
};
Buffer.from(this.obj, 'hex')
.toString('hex')
.should.equal(this.obj);
});
/* eslint-disable no-underscore-dangle */
Assertion.addMethod('matchAny', function handleAssert(matcher) {
const obj = this._obj;

new Assertion(obj).to.be.an('array');
const result = obj.some(val => matcher(val));
this.assert(
result,
'expected #{this} to match at least once',
'expected #{this} not to match',
);
});

Assertion.addMethod('customError', function handleAssert(error) {
const obj = this._obj;
new Assertion(obj).to.be.instanceOf(Error);
new Assertion(obj.name).to.equal(error.name);
new Assertion(obj.message).to.equal(error.message);
});
/* eslint-enable no-underscore-dangle */

mochaBDD();
// See https://github.com/shouldjs/should.js/issues/41
Object.defineProperty(global, 'should', { value: should });

[sinonChai, chaiAsPromised].forEach(chai.use);

global.sinon = sinon;
global.sandbox = sinon.sandbox.create();
8 changes: 4 additions & 4 deletions test/steps/api/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getFirstBoolean } from '../utils';

export function itShouldNotSetTheLiskAPIInstanceTestnetSetting() {
const { liskAPIInstance } = this.test.ctx;
return liskAPIInstance.setTestnet.should.not.be.called();
return liskAPIInstance.setTestnet.should.not.be.called;
}

export function itShouldSetTheLiskAPIInstanceTestnetSettingTo() {
Expand All @@ -42,7 +42,7 @@ export function itShouldUseTheLiskAPIInstanceToSendARequestToTheEndpointUsingThe

export function itShouldNotBroadcastTheSignature() {
const { liskAPIInstance } = this.test.ctx;
return liskAPIInstance.broadcastSignatures.should.not.be.called();
return liskAPIInstance.broadcastSignatures.should.not.be.called;
}

export function itShouldBroadcastTheSignature() {
Expand All @@ -54,7 +54,7 @@ export function itShouldBroadcastTheSignature() {

export function itShouldNotBroadcastTheTransaction() {
const { liskAPIInstance } = this.test.ctx;
return liskAPIInstance.broadcastTransaction.should.not.be.called();
return liskAPIInstance.broadcastTransaction.should.not.be.called;
}

export function itShouldBroadcastTheTransaction() {
Expand All @@ -66,7 +66,7 @@ export function itShouldBroadcastTheTransaction() {

export function itShouldResolveToTheAPIResponse() {
const { returnValue, apiResponse } = this.test.ctx;
return returnValue.should.be.fulfilledWith(apiResponse);
return returnValue.should.eventually.eql(apiResponse);
}

export function theLiskAPIInstanceShouldBeALiskJSAPIInstance() {
Expand Down
2 changes: 1 addition & 1 deletion test/steps/childProcesses/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function itShouldExecuteAScriptExecutingThirdInASeparateChildProcess() {
}

export function itShouldNotExecuteAThirdScriptInASeparateChildProcess() {
return childProcess.exec.should.not.be.calledThrice();
return childProcess.exec.should.not.be.calledThrice;
}

export function theLiskyInstanceShouldLogTheFirstChildProcessOutputFirst() {
Expand Down
2 changes: 1 addition & 1 deletion test/steps/config/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function itShouldUpdateTheConfigVariableToBoolean() {

export function itShouldResolveToTheConfig() {
const { returnValue, config } = this.test.ctx;
return returnValue.should.be.fulfilledWith(config);
return returnValue.should.eventually.eql(config);
}

export function theDefaultConfigShouldBeExported() {
Expand Down
26 changes: 13 additions & 13 deletions test/steps/crypto/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export function itShouldSignTheMessageWithThePassphrase() {

export function itShouldResolveToTheResultOfSigningTheMessage() {
const { returnValue, cryptoResult } = this.test.ctx;
return returnValue.should.be.fulfilledWith(cryptoResult);
return returnValue.should.eventually.eql(cryptoResult);
}

export function itShouldResolveToTheResultOfDecryptingThePassphrase() {
const { returnValue, cryptoResult } = this.test.ctx;
return returnValue.should.be.fulfilledWith(cryptoResult);
return returnValue.should.eventually.eql(cryptoResult);
}

export function itShouldDecryptThePassphraseUsingTheIVAndThePassword() {
Expand All @@ -46,7 +46,7 @@ export function itShouldDecryptThePassphraseUsingTheIVAndThePassword() {

export function itShouldResolveToTheResultOfEncryptingThePassphraseCombinedWithThePublicKey() {
const { returnValue, cryptoResult, publicKey } = this.test.ctx;
return returnValue.should.be.fulfilledWith(
return returnValue.should.eventually.eql(
Object.assign({}, cryptoResult, { publicKey }),
);
}
Expand All @@ -61,7 +61,7 @@ export function itShouldEncryptThePassphraseUsingThePassword() {

export function itShouldResolveToTheResultOfEncryptingThePassphrase() {
const { returnValue, cryptoResult } = this.test.ctx;
return returnValue.should.be.fulfilledWith(cryptoResult);
return returnValue.should.eventually.eql(cryptoResult);
}

export function itShouldDecryptTheMessageUsingTheNonceThePassphraseAndTheSenderPublicKey() {
Expand All @@ -76,7 +76,7 @@ export function itShouldDecryptTheMessageUsingTheNonceThePassphraseAndTheSenderP

export function itShouldResolveToTheResultOfDecryptingTheMessage() {
const { returnValue, cryptoResult } = this.test.ctx;
return returnValue.should.be.fulfilledWith(cryptoResult);
return returnValue.should.eventually.eql(cryptoResult);
}

export function itShouldEncryptTheMessageWithThePassphraseForTheRecipient() {
Expand All @@ -90,7 +90,7 @@ export function itShouldEncryptTheMessageWithThePassphraseForTheRecipient() {

export function itShouldResolveToTheResultOfEncryptingTheMessage() {
const { returnValue, cryptoResult } = this.test.ctx;
return returnValue.should.be.fulfilledWith(cryptoResult);
return returnValue.should.eventually.eql(cryptoResult);
}

export function itShouldResolveToAnObjectWithThePassphraseAndThePublicKeyAndTheAddress() {
Expand All @@ -105,7 +105,7 @@ export function itShouldResolveToAnObjectWithThePassphraseAndThePublicKeyAndTheA
publicKey,
address,
};
return returnValue.should.be.fulfilledWith(expectedObject);
return returnValue.should.eventually.eql(expectedObject);
}

export function theSignatureShouldBeReturned() {
Expand Down Expand Up @@ -215,12 +215,12 @@ export function theDecryptedMessageShouldBeReturned() {

export function itShouldResolveToThePassphrase() {
const { returnValue, passphrase } = this.test.ctx;
return returnValue.should.be.fulfilledWith(passphrase);
return returnValue.should.eventually.eql(passphrase);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer .equal where possible.

}

export function itShouldReturnAnObjectWithThePassphrase() {
const { returnValue, passphrase } = this.test.ctx;
return returnValue.should.be.fulfilledWith({
return returnValue.should.eventually.eql({
passphrase,
secondPassphrase: null,
password: null,
Expand All @@ -230,7 +230,7 @@ export function itShouldReturnAnObjectWithThePassphrase() {

export function itShouldReturnAnObjectWithTheSecondPassphrase() {
const { returnValue, secondPassphrase } = this.test.ctx;
return returnValue.should.be.fulfilledWith({
return returnValue.should.eventually.eql({
passphrase: null,
secondPassphrase,
password: null,
Expand All @@ -240,7 +240,7 @@ export function itShouldReturnAnObjectWithTheSecondPassphrase() {

export function itShouldReturnAnObjectWithThePassword() {
const { returnValue, password } = this.test.ctx;
return returnValue.should.be.fulfilledWith({
return returnValue.should.eventually.eql({
passphrase: null,
secondPassphrase: null,
password,
Expand All @@ -250,7 +250,7 @@ export function itShouldReturnAnObjectWithThePassword() {

export function itShouldReturnAnObjectWithTheData() {
const { returnValue, data } = this.test.ctx;
return returnValue.should.be.fulfilledWith({
return returnValue.should.eventually.eql({
passphrase: null,
secondPassphrase: null,
password: null,
Expand All @@ -266,7 +266,7 @@ export function itShouldReturnAnObjectWithThePassphraseTheSecondPassphraseThePas
password,
data,
} = this.test.ctx;
return returnValue.should.be.fulfilledWith({
return returnValue.should.eventually.eql({
passphrase,
secondPassphrase,
password,
Expand Down
4 changes: 2 additions & 2 deletions test/steps/files/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ export function theDefaultConfigShouldBeWrittenToTheConfigFile() {
}

export function theConfigFileShouldNotBeWritten() {
return fsUtils.writeJSONSync.should.not.be.called();
return fsUtils.writeJSONSync.should.not.be.called;
}

export function itShouldResolveToTheFirstLineOfTheFile() {
const { returnValue, passphrase } = this.test.ctx;
return returnValue.should.be.fulfilledWith(passphrase);
return returnValue.should.eventually.eql(passphrase);
}
36 changes: 22 additions & 14 deletions test/steps/general/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
*/
import { getFirstQuotedString, getFirstNumber } from '../utils';
import { FileSystemError, ValidationError } from '../../../src/utils/error';
import { ValidationError, FileSystemError } from '../../../src/utils/error';

export function theErrorShouldBeInstanceOfNodesBuiltInError() {
const { testError } = this.test.ctx;
Expand All @@ -35,13 +35,17 @@ export function itShouldReturnTheResult() {
export function itShouldThrowValidationError() {
const { testFunction } = this.test.ctx;
const message = getFirstQuotedString(this.test.title);
return testFunction.should.throw(new ValidationError(message));
return testFunction.should
.throw()
.and.be.customError(new ValidationError(message));
}

export function itShouldThrowFileSystemError() {
const { testFunction } = this.test.ctx;
const message = getFirstQuotedString(this.test.title);
return testFunction.should.throw(new FileSystemError(message));
return testFunction.should
.throw()
.and.be.customError(new FileSystemError(message));
}

export function itShouldExitWithCode() {
Expand All @@ -52,7 +56,7 @@ export function itShouldExitWithCode() {

export function itShouldResolveToTheErrorObject() {
const { returnValue, errorObject } = this.test.ctx;
return returnValue.should.be.fulfilledWith(errorObject);
return returnValue.should.eventually.eql(errorObject);
}

export async function itShouldResolveToAnObjectWithMessage() {
Expand Down Expand Up @@ -82,13 +86,17 @@ export function itShouldRejectWithTheErrorMessage() {
export function itShouldRejectWithFileSystemErrorAndMessage() {
const { returnValue } = this.test.ctx;
const message = getFirstQuotedString(this.test.title);
return returnValue.should.be.rejectedWith(new FileSystemError(message));
return returnValue.should.be.rejected.then(err => {
return err.should.be.customError(new FileSystemError(message));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer direct return without function body.

});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar suggestion about adding a method to chai.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great idea! it's much cleaner.
I will add customError check function

}

export function itShouldRejectWithValidationErrorAndMessage() {
const { returnValue } = this.test.ctx;
const message = getFirstQuotedString(this.test.title);
return returnValue.should.be.rejectedWith(new ValidationError(message));
return returnValue.should.be.rejected.then(err => {
return err.should.be.customError(new ValidationError(message));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

});
}

export function itShouldRejectWithMessage() {
Expand All @@ -104,22 +112,22 @@ export function itShouldRejectWithTheOriginalRejection() {

export function itShouldReturnAnEmptyObject() {
const { returnValue } = this.test.ctx;
return returnValue.should.be.fulfilledWith({});
return returnValue.should.eventually.eql({});
}

export function itShouldReturnTrue() {
const { returnValue } = this.test.ctx;
return returnValue.should.be.true();
return returnValue.should.be.true;
}

export function itShouldReturnFalse() {
const { returnValue } = this.test.ctx;
return returnValue.should.be.false();
return returnValue.should.be.false;
}

export function itShouldReturnNull() {
const { returnValue } = this.test.ctx;
return should(returnValue).be.null();
return should.equal(returnValue, null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another downside to switching to chai.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I guess it'll be solved when we switch to expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it will.
I almost tried to port except to global, too... lol

}

export function itShouldReturnString() {
Expand All @@ -131,12 +139,12 @@ export function itShouldReturnString() {

export function itShouldResolveToTheOptions() {
const { options, returnValue } = this.test.ctx;
return returnValue.should.be.fulfilledWith(options);
return returnValue.should.eventually.eql(options);
}

export function itShouldResolveToTheDataAsAString() {
const { returnValue, data } = this.test.ctx;
return returnValue.should.be.fulfilledWith(data);
return returnValue.should.eventually.eql(data);
}

export function itShouldReturnAnObjectWithError() {
Expand All @@ -149,10 +157,10 @@ export function itShouldReturnAnObjectWithError() {

export function itShouldResolveToTheWarrantyInformation() {
const { returnValue, warranty } = this.test.ctx;
return returnValue.should.be.fulfilledWith({ warranty });
return returnValue.should.eventually.eql({ warranty });
}

export function itShouldResolveToTheCopyrightInformation() {
const { returnValue, copyright } = this.test.ctx;
return returnValue.should.be.fulfilledWith({ copyright });
return returnValue.should.eventually.eql({ copyright });
}
Loading