From 1325fb8c9aed5f6855bbb32deeefd95999f4afd8 Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Fri, 15 Nov 2019 10:45:14 +0100 Subject: [PATCH] Use helpers version of `cleanOutput` in tests. Simplify expect semantics in tests. Change-type: patch Signed-off-by: Scott Lowe --- tests/auth/utils.spec.ts | 30 +++++++-------- tests/commands/env/add.spec.ts | 6 +-- tests/commands/env/rename.spec.ts | 6 +-- tests/commands/env/rm.spec.ts | 6 +-- tests/commands/help.spec.ts | 64 +++++++++++-------------------- tests/commands/version.spec.ts | 8 ++-- 6 files changed, 50 insertions(+), 70 deletions(-) diff --git a/tests/auth/utils.spec.ts b/tests/auth/utils.spec.ts index cd9c26c4..92fa2151 100644 --- a/tests/auth/utils.spec.ts +++ b/tests/auth/utils.spec.ts @@ -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; }); }); }); diff --git a/tests/commands/env/add.spec.ts b/tests/commands/env/add.spec.ts index 0f383759..2f842401 100644 --- a/tests/commands/env/add.spec.ts +++ b/tests/commands/env/add.spec.ts @@ -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(); diff --git a/tests/commands/env/rename.spec.ts b/tests/commands/env/rename.spec.ts index fc37020b..40c929ab 100644 --- a/tests/commands/env/rename.spec.ts +++ b/tests/commands/env/rename.spec.ts @@ -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(); diff --git a/tests/commands/env/rm.spec.ts b/tests/commands/env/rm.spec.ts index 322fafc5..a8de5701 100644 --- a/tests/commands/env/rm.spec.ts +++ b/tests/commands/env/rm.spec.ts @@ -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(); diff --git a/tests/commands/help.spec.ts b/tests/commands/help.spec.ts index 7ee43082..367466cf 100644 --- a/tests/commands/help.spec.ts +++ b/tests/commands/help.spec.ts @@ -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(''); }); }); diff --git a/tests/commands/version.spec.ts b/tests/commands/version.spec.ts index 55cab1d2..08012c60 100644 --- a/tests/commands/version.spec.ts +++ b/tests/commands/version.spec.ts @@ -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, });