Use helpers version of cleanOutput in tests.

Simplify expect semantics in tests.

Change-type: patch
Signed-off-by: Scott Lowe <scott@balena.io>
This commit is contained in:
Scott Lowe 2019-11-15 10:45:14 +01:00
parent cf42dca777
commit 1325fb8c9a
6 changed files with 50 additions and 70 deletions

@ -1,5 +1,5 @@
import * as Promise from 'bluebird';
import * as chai from 'chai';
import { expect } from 'chai';
import rewire = require('rewire');
import * as sinon from 'sinon';
import * as url from 'url';
@ -14,7 +14,7 @@ describe('Utils:', function() {
utils
.getDashboardLoginURL('https://127.0.0.1:3000/callback')
.then((loginUrl: string) =>
chai.expect(() => url.parse(loginUrl)).to.not.throw(Error),
expect(() => url.parse(loginUrl)).to.not.throw(Error),
));
it('should eventually contain an https protocol', () =>
@ -23,7 +23,7 @@ describe('Utils:', function() {
loginUrl: utils.getDashboardLoginURL('https://127.0.0.1:3000/callback'),
}).then(function({ dashboardUrl, loginUrl }) {
const { protocol } = url.parse(loginUrl);
return chai.expect(protocol).to.equal(url.parse(dashboardUrl).protocol);
return expect(protocol).to.equal(url.parse(dashboardUrl).protocol);
}));
it('should correctly escape a callback url without a path', () =>
@ -32,7 +32,7 @@ describe('Utils:', function() {
loginUrl: utils.getDashboardLoginURL('http://127.0.0.1:3000'),
}).then(function({ dashboardUrl, loginUrl }) {
const expectedUrl = `${dashboardUrl}/login/cli/http%253A%252F%252F127.0.0.1%253A3000`;
return chai.expect(loginUrl).to.equal(expectedUrl);
return expect(loginUrl).to.equal(expectedUrl);
}));
return it('should correctly escape a callback url with a path', () =>
@ -41,29 +41,29 @@ describe('Utils:', function() {
loginUrl: utils.getDashboardLoginURL('http://127.0.0.1:3000/callback'),
}).then(function({ dashboardUrl, loginUrl }) {
const expectedUrl = `${dashboardUrl}/login/cli/http%253A%252F%252F127.0.0.1%253A3000%252Fcallback`;
return chai.expect(loginUrl).to.equal(expectedUrl);
return expect(loginUrl).to.equal(expectedUrl);
}));
});
return describe('.loginIfTokenValid()', function() {
it('should eventually be false if token is undefined', function() {
const promise = utils.loginIfTokenValid(undefined);
return chai.expect(promise).to.eventually.be.false;
return expect(promise).to.eventually.be.false;
});
it('should eventually be false if token is null', function() {
const promise = utils.loginIfTokenValid(null);
return chai.expect(promise).to.eventually.be.false;
return expect(promise).to.eventually.be.false;
});
it('should eventually be false if token is an empty string', function() {
const promise = utils.loginIfTokenValid('');
return chai.expect(promise).to.eventually.be.false;
return expect(promise).to.eventually.be.false;
});
it('should eventually be false if token is a string containing only spaces', function() {
const promise = utils.loginIfTokenValid(' ');
return chai.expect(promise).to.eventually.be.false;
return expect(promise).to.eventually.be.false;
});
describe('given the token does not authenticate with the server', function() {
@ -78,7 +78,7 @@ describe('Utils:', function() {
it('should eventually be false', function() {
const promise = utils.loginIfTokenValid(tokens.johndoe.token);
return chai.expect(promise).to.eventually.be.false;
return expect(promise).to.eventually.be.false;
});
describe('given there was a token already', function() {
@ -88,12 +88,12 @@ describe('Utils:', function() {
balena.auth
.getToken()
.then(function(originalToken: string) {
chai.expect(originalToken).to.equal(tokens.janedoe.token);
expect(originalToken).to.equal(tokens.janedoe.token);
return utils.loginIfTokenValid(tokens.johndoe.token);
})
.then(balena.auth.getToken)
.then((currentToken: string) =>
chai.expect(currentToken).to.equal(tokens.janedoe.token),
expect(currentToken).to.equal(tokens.janedoe.token),
));
});
@ -104,9 +104,7 @@ describe('Utils:', function() {
utils
.loginIfTokenValid(tokens.johndoe.token)
.then(() => balena.auth.isLoggedIn())
.then((isLoggedIn: boolean) =>
chai.expect(isLoggedIn).to.equal(false),
));
.then((isLoggedIn: boolean) => expect(isLoggedIn).to.equal(false)));
});
});
@ -122,7 +120,7 @@ describe('Utils:', function() {
return it('should eventually be true', function() {
const promise = utils.loginIfTokenValid(tokens.johndoe.token);
return chai.expect(promise).to.eventually.be.true;
return expect(promise).to.eventually.be.true;
});
});
});

@ -1,4 +1,4 @@
import * as chai from 'chai';
import { expect } from 'chai';
import { balenaAPIMock, runCommand } from '../../helpers';
describe('balena env add', function() {
@ -20,8 +20,8 @@ describe('balena env add', function() {
const { out, err } = await runCommand(`env add TEST 1 -d ${deviceId}`);
chai.expect(out.join('')).to.equal('');
chai.expect(err.join('')).to.equal('');
expect(out.join('')).to.equal('');
expect(err.join('')).to.equal('');
// @ts-ignore
mock.remove();

@ -15,7 +15,7 @@
* limitations under the License.
*/
import * as chai from 'chai';
import { expect } from 'chai';
import { balenaAPIMock, runCommand } from '../../helpers';
describe('balena env rename', function() {
@ -25,8 +25,8 @@ describe('balena env rename', function() {
const { out, err } = await runCommand('env rename 376 emacs --device');
chai.expect(out.join('')).to.equal('');
chai.expect(err.join('')).to.equal('');
expect(out.join('')).to.equal('');
expect(err.join('')).to.equal('');
// @ts-ignore
mock.remove();

@ -1,4 +1,4 @@
import * as chai from 'chai';
import { expect } from 'chai';
import { balenaAPIMock, runCommand } from '../../helpers';
describe('balena env rm', function() {
@ -8,8 +8,8 @@ describe('balena env rm', function() {
const { out, err } = await runCommand('env rm 144690 -d -y');
chai.expect(out.join('')).to.equal('');
chai.expect(err.join('')).to.equal('');
expect(out.join('')).to.equal('');
expect(err.join('')).to.equal('');
// @ts-ignore
mock.remove();

@ -1,6 +1,6 @@
import * as chai from 'chai';
import { expect } from 'chai';
import * as _ from 'lodash';
import { runCommand } from '../helpers';
import { cleanOutput, runCommand } from '../helpers';
const SIMPLE_HELP = `
Usage: balena [COMMAND] [OPTIONS]
@ -92,62 +92,44 @@ const GLOBAL_OPTIONS = `
--version, -v
`;
const cleanOutput = (output: string[] | string) => {
return _(_.castArray(output))
.map(log => {
return log.split('\n').map(line => {
return line.trim();
});
})
.flatten()
.compact()
.value();
};
describe('balena help', function() {
it('should print simple help text', async () => {
const { out, err } = await runCommand('help');
chai
.expect(cleanOutput(out))
.to.deep.equal(
cleanOutput([
SIMPLE_HELP,
'Run `balena help --verbose` to list additional commands',
GLOBAL_OPTIONS,
]),
);
expect(cleanOutput(out)).to.deep.equal(
cleanOutput([
SIMPLE_HELP,
'Run `balena help --verbose` to list additional commands',
GLOBAL_OPTIONS,
]),
);
chai.expect(err.join('')).to.equal('');
expect(err.join('')).to.equal('');
});
it('should print additional commands with the -v flag', async () => {
const { out, err } = await runCommand('help -v');
chai
.expect(cleanOutput(out))
.to.deep.equal(
cleanOutput([SIMPLE_HELP, ADDITIONAL_HELP, GLOBAL_OPTIONS]),
);
expect(cleanOutput(out)).to.deep.equal(
cleanOutput([SIMPLE_HELP, ADDITIONAL_HELP, GLOBAL_OPTIONS]),
);
chai.expect(err.join('')).to.equal('');
expect(err.join('')).to.equal('');
chai.expect(err.join('')).to.equal('');
expect(err.join('')).to.equal('');
});
it('should print simple help text when no arguments present', async () => {
const { out, err } = await runCommand('');
chai
.expect(cleanOutput(out))
.to.deep.equal(
cleanOutput([
SIMPLE_HELP,
'Run `balena help --verbose` to list additional commands',
GLOBAL_OPTIONS,
]),
);
expect(cleanOutput(out)).to.deep.equal(
cleanOutput([
SIMPLE_HELP,
'Run `balena help --verbose` to list additional commands',
GLOBAL_OPTIONS,
]),
);
chai.expect(err.join('')).to.equal('');
expect(err.join('')).to.equal('');
});
});

@ -1,4 +1,4 @@
import * as chai from 'chai';
import { expect } from 'chai';
import * as fs from 'fs';
import { runCommand } from '../helpers';
@ -11,13 +11,13 @@ describe('balena version', function() {
it('should print the installed version of the CLI', async () => {
const { out } = await runCommand('version');
chai.expect(out.join('')).to.equal(`${packageJSON.version}\n`);
expect(out.join('')).to.equal(`${packageJSON.version}\n`);
});
it('should print additional version information with the -a flag', async () => {
const { out } = await runCommand('version -a');
chai.expect(out.join('')).to.equal(
expect(out.join('')).to.equal(
`balena-cli version "${packageJSON.version}"
Node.js version "${nodeVersion}"
`,
@ -29,7 +29,7 @@ Node.js version "${nodeVersion}"
const json = JSON.parse(out.join(''));
chai.expect(json).to.deep.equal({
expect(json).to.deep.equal({
'balena-cli': packageJSON.version,
'Node.js': nodeVersion,
});