From 3513db553f64c7401d582930acd542b9110193dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Mon, 28 Oct 2019 12:15:54 +0100 Subject: [PATCH 1/8] fix: return unhandled promises from run.spec tests --- tests/spec/unit/lib/run.spec.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/spec/unit/lib/run.spec.js b/tests/spec/unit/lib/run.spec.js index 85611fed9..ac88949e5 100644 --- a/tests/spec/unit/lib/run.spec.js +++ b/tests/spec/unit/lib/run.spec.js @@ -35,14 +35,16 @@ if (process.platform === 'darwin') { spyOn(run, 'listEmulators').and.returnValue(deferred.promise); }); it('should delegate to listDevices method if `options.device` specified', function () { - run.run({ list: true, device: true }); - expect(run.listDevices).toHaveBeenCalled(); - expect(run.listEmulators).not.toHaveBeenCalled(); + return run.run({ list: true, device: true }).then(() => { + expect(run.listDevices).toHaveBeenCalled(); + expect(run.listEmulators).not.toHaveBeenCalled(); + }); }); it('should delegate to listEmulators method if `options.device` specified', function () { - run.run({ list: true, emulator: true }); - expect(run.listDevices).not.toHaveBeenCalled(); - expect(run.listEmulators).toHaveBeenCalled(); + return run.run({ list: true, emulator: true }).then(() => { + expect(run.listDevices).not.toHaveBeenCalled(); + expect(run.listEmulators).toHaveBeenCalled(); + }); }); it('should delegate to to both listEmulators and listDevices methods if neither `options.device` nor `options.emulator` are specified', function (done) { run.run({ list: true }) From 4941190f4ef39334dcb13b43936076fabf936e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Mon, 28 Oct 2019 12:04:54 +0100 Subject: [PATCH 2/8] fix: stub globals w/ spyOn instead of rewire This works around https://github.com/jhnns/rewire/issues/167 by using Jasmine's `spyOn` to stub global variables like `process` and `console`. --- tests/spec/unit/build.spec.js | 7 +++---- tests/spec/unit/versions.spec.js | 21 +++++++-------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/tests/spec/unit/build.spec.js b/tests/spec/unit/build.spec.js index 432361153..e0c51a23f 100644 --- a/tests/spec/unit/build.spec.js +++ b/tests/spec/unit/build.spec.js @@ -343,12 +343,11 @@ describe('build', function () { describe('help method', () => { it('should log a bunch of options', () => { - const logSpy = jasmine.createSpy(); - const procStub = { exit: _ => null, cwd: _ => '', argv: ['', ''] }; - build.__set__({ console: { log: logSpy }, process: procStub }); + spyOn(console, 'log'); + spyOn(process, 'exit'); build.help(); - expect(logSpy).toHaveBeenCalledWith(jasmine.stringMatching(/^Usage:/)); + expect(console.log).toHaveBeenCalledWith(jasmine.stringMatching(/^Usage:/)); }); }); diff --git a/tests/spec/unit/versions.spec.js b/tests/spec/unit/versions.spec.js index 52b30d4ff..547fec9c6 100644 --- a/tests/spec/unit/versions.spec.js +++ b/tests/spec/unit/versions.spec.js @@ -18,21 +18,19 @@ */ var semver = require('semver'); -var rewire = require('rewire'); -var versions = rewire('../../../bin/templates/scripts/cordova/lib/versions'); +var versions = require('../../../bin/templates/scripts/cordova/lib/versions'); // These tests can not run on windows. if (process.platform === 'darwin') { describe('versions', function () { + beforeEach(() => { + spyOn(console, 'log'); + }); + describe('get_apple_ios_version method', () => { it('should have found ios version.', (done) => { - let _console = versions.__get__('console'); - let logSpy = jasmine.createSpy('logSpy'); - versions.__set__('console', { log: logSpy }); - versions.get_apple_ios_version().then(() => { - expect(logSpy).not.toHaveBeenCalledWith(undefined); - versions.__set__('console', _console); + expect(console.log).not.toHaveBeenCalledWith(undefined); done(); }); }); @@ -40,13 +38,8 @@ if (process.platform === 'darwin') { describe('get_apple_osx_version method', () => { it('should have found osx version.', (done) => { - let _console = versions.__get__('console'); - let logSpy = jasmine.createSpy('logSpy'); - versions.__set__('console', { log: logSpy }); - versions.get_apple_osx_version().then(() => { - expect(logSpy).not.toHaveBeenCalledWith(undefined); - versions.__set__('console', _console); + expect(console.log).not.toHaveBeenCalledWith(undefined); done(); }); }); From bcdb7273b06b858d5ad736de60b2e85c65fe999a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Mon, 28 Oct 2019 21:00:05 +0100 Subject: [PATCH 3/8] Pass fresh EventEmitter to every new Api instance This has multiple benefits: - we don't register too many listeners on `cordova-common.events` - we silence all output from Api --- tests/spec/unit/Api.spec.js | 19 ++++++++----------- tests/spec/unit/Plugman/pluginHandler.spec.js | 3 ++- tests/spec/unit/prepare.spec.js | 7 ++----- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/tests/spec/unit/Api.spec.js b/tests/spec/unit/Api.spec.js index 4998f0382..f8ee7ba65 100644 --- a/tests/spec/unit/Api.spec.js +++ b/tests/spec/unit/Api.spec.js @@ -19,8 +19,8 @@ var path = require('path'); var fs = require('fs'); +const EventEmitter = require('events'); var PluginManager = require('cordova-common').PluginManager; -var events = require('cordova-common').events; var Api = require('../../../bin/templates/scripts/cordova/Api'); var check_reqs = require('../../../bin/templates/scripts/cordova/lib/check_reqs'); @@ -49,16 +49,14 @@ function compareListWithoutOrder (list1, list2) { describe('Platform Api', function () { describe('constructor', function () { - beforeEach(function () { - events.removeAllListeners(); - }); - it('Test 001 : should throw if provided directory does not contain an xcodeproj file', function () { - expect(function () { new Api('ios', path.join(FIXTURES, '..')); }).toThrow(); /* eslint no-new : 0 */ + expect(() => + new Api('ios', path.join(FIXTURES, '..'), new EventEmitter()) + ).toThrow(); }); it('Test 002 : should create an instance with path, pbxproj, xcodeproj, originalName and cordovaproj properties', function () { expect(function () { - var p = new Api('ios', iosProjectFixture); + var p = new Api('ios', iosProjectFixture, new EventEmitter()); expect(p.locations.root).toEqual(iosProjectFixture); expect(p.locations.pbxproj).toEqual(path.join(iosProjectFixture, 'SampleApp.xcodeproj', 'project.pbxproj')); expect(p.locations.xcodeProjDir).toEqual(path.join(iosProjectFixture, 'SampleApp.xcodeproj')); @@ -135,11 +133,11 @@ describe('Platform Api', function () { }); describe('.prototype', function () { - var api; + var api, events; var projectRoot = iosProjectFixture; beforeEach(function () { - events.removeAllListeners(); - api = new Api('ios', projectRoot); + events = new EventEmitter(); + api = new Api('ios', projectRoot, events); spyOn(fs, 'readdirSync').and.returnValue([api.locations.xcodeProjDir]); spyOn(projectFile, 'parse').and.returnValue({ getPackageName: function () { return 'ios.cordova.io'; } @@ -447,7 +445,6 @@ describe('Platform Api', function () { }).done(done); }); it('if two frameworks with the same name are added, should honour the spec of the first-installed plugin', function (done) { - spyOn(events, 'emit'); podsjson_mock.getLibrary.and.returnValue({ spec: 'something different from ' + my_pod_json.spec }); diff --git a/tests/spec/unit/Plugman/pluginHandler.spec.js b/tests/spec/unit/Plugman/pluginHandler.spec.js index a21dec56e..4f9f2dbd1 100644 --- a/tests/spec/unit/Plugman/pluginHandler.spec.js +++ b/tests/spec/unit/Plugman/pluginHandler.spec.js @@ -22,6 +22,7 @@ var fs = require('fs'); var path = require('path'); var rewire = require('rewire'); var shell = require('shelljs'); +const EventEmitter = require('events'); var PluginInfo = require('cordova-common').PluginInfo; var Api = require('../../../../bin/templates/scripts/cordova/Api'); @@ -344,7 +345,7 @@ describe('ios plugin handler', function () { }); it('Test 028 : of two plugins should apply xcode file changes from both', function (done) { - var api = new Api('ios', temp); + var api = new Api('ios', temp, new EventEmitter()); var fail = jasmine.createSpy('fail'); api.addPlugin(dummyPluginInfo) diff --git a/tests/spec/unit/prepare.spec.js b/tests/spec/unit/prepare.spec.js index 8ea411547..6f87c95c6 100644 --- a/tests/spec/unit/prepare.spec.js +++ b/tests/spec/unit/prepare.spec.js @@ -21,6 +21,7 @@ var fs = require('fs'); var fse = require('fs-extra'); +const EventEmitter = require('events'); var os = require('os'); var path = require('path'); var shell = require('shelljs'); @@ -65,17 +66,13 @@ describe('prepare', function () { beforeEach(function () { Api = rewire('../../../bin/templates/scripts/cordova/Api'); - // Prevent logging to avoid polluting the test reports - Api.__set__('events.emit', jasmine.createSpy()); - shell.mkdir('-p', iosPlatform); shell.cp('-rf', iosProjectFixture + '/*', iosPlatform); - p = new Api('ios', iosPlatform); + p = new Api('ios', iosPlatform, new EventEmitter()); }); afterEach(function () { shell.rm('-rf', path.join(__dirname, 'some')); - process.removeAllListeners(); }); describe('launch storyboard feature (CB-9762)', function () { From 235193dca2cd4e232eee135b436ebcc8c3c16164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Mon, 28 Oct 2019 13:37:39 +0100 Subject: [PATCH 4/8] Move some tests from Api.spec to check_reqs.spec --- tests/spec/unit/Api.spec.js | 66 -------------------------- tests/spec/unit/lib/check_reqs.spec.js | 44 +++++++++++++++++ 2 files changed, 44 insertions(+), 66 deletions(-) diff --git a/tests/spec/unit/Api.spec.js b/tests/spec/unit/Api.spec.js index f8ee7ba65..3c3f5c354 100644 --- a/tests/spec/unit/Api.spec.js +++ b/tests/spec/unit/Api.spec.js @@ -64,72 +64,6 @@ describe('Platform Api', function () { expect(p.locations.configXml).toEqual(path.join(iosProjectFixture, 'SampleApp', 'config.xml')); }).not.toThrow(); }); - it('Test 003 : test cocoapods check_reqs, on darwin (macOS)', function (done) { - // the purpose of this test is not to actually test whether CocoaPods is installed - // it is to test check_reqs can run and be covered (we mock the actual checking, simple check) - - check_reqs.check_os() - .then(function (message) { - // supported os - function fail () { - done.fail('check_reqs fail (' + message + ')'); - } - function success () { - done(); - } - var toolsChecker = { - success: function () { - return Q.resolve('CocoaPods found'); - }, - fail: function () { - return Q.reject('CocoaPods NOT found'); - } - }; - - // success expected - check_reqs.check_cocoapods(toolsChecker.success) - .then(success, fail) - .catch(fail); - - // fail expected - check_reqs.check_cocoapods(toolsChecker.fail) - .then(fail, success) - .catch(success); - - }, function () { - // unsupported os, do nothing - done(); - }); - }); - it('Test 004 : test cocoapods check_reqs, expected success on non-darwin (macOS)', function (done) { - // the purpose of this test is not to actually test whether CocoaPods is installed - // it is to test check_reqs can run and be covered (we mock the actual checking, simple check) - check_reqs.check_os() - .then(function () { - // supported os, do nothing - done(); - }, function (message) { - // unsupported os, check_reqs should be ignored - function fail () { - done.fail('check_reqs fail (' + message + ')'); - } - function success (toolOptions) { - expect(toolOptions.ignore).toBeDefined(); - expect(toolOptions.ignoreMessage).toBeDefined(); - done(); - } - var toolsChecker = function () { - done.fail(); // this function should not ever be called if non-darwin - return Q.reject('CocoaPods NOT found'); - }; - - // success expected - check_reqs.check_cocoapods(toolsChecker) - .then(success, fail) - .catch(fail); - }); - - }); }); describe('.prototype', function () { diff --git a/tests/spec/unit/lib/check_reqs.spec.js b/tests/spec/unit/lib/check_reqs.spec.js index b1dd507f7..b17ec8739 100644 --- a/tests/spec/unit/lib/check_reqs.spec.js +++ b/tests/spec/unit/lib/check_reqs.spec.js @@ -110,4 +110,48 @@ describe('check_reqs', function () { }); }); }); + + describe('check_cocoapods method', () => { + let toolsChecker; + beforeEach(() => { + toolsChecker = jasmine.createSpy('toolsChecker') + .and.returnValue(Promise.resolve({})); + }); + + it('should resolve when on an unsupported platform', () => { + checkReqs.__set__({ + os_platform_is_supported: () => false + }); + + return checkReqs.check_cocoapods(toolsChecker).then(toolOptions => { + expect(toolsChecker).not.toHaveBeenCalled(); + expect(toolOptions.ignore).toBeDefined(); + expect(toolOptions.ignoreMessage).toBeDefined(); + }); + }); + + it('should resolve when toolsChecker resolves', () => { + checkReqs.__set__({ + os_platform_is_supported: () => true + }); + spyOn(shell, 'exec').and.returnValue({ code: 1 }); + + return checkReqs.check_cocoapods(toolsChecker).then(() => { + expect(shell.exec).toHaveBeenCalled(); + }); + }); + + it('should reject when toolsChecker rejects', () => { + checkReqs.__set__({ + os_platform_is_supported: () => true + }); + const testError = new Error(); + toolsChecker.and.callFake(() => Promise.reject(testError)); + + return checkReqs.check_cocoapods(toolsChecker).then( + () => fail('Expected promise to be rejected'), + err => expect(err).toBe(testError) + ); + }); + }); }); From a93942c1613580ba9619f57090e74d3412a1b994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Mon, 28 Oct 2019 14:24:45 +0100 Subject: [PATCH 5/8] Read configs in beforeEach instead of restoring them --- tests/spec/unit/prepare.spec.js | 39 ++++++--------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/tests/spec/unit/prepare.spec.js b/tests/spec/unit/prepare.spec.js index 6f87c95c6..19768ed8b 100644 --- a/tests/spec/unit/prepare.spec.js +++ b/tests/spec/unit/prepare.spec.js @@ -42,11 +42,6 @@ shell.config.silent = true; var ConfigParser = require('cordova-common').ConfigParser; -// Create a real config object before mocking out everything. -var cfg = new ConfigParser(path.join(FIXTURES, 'test-config.xml')); -var cfg2 = new ConfigParser(path.join(FIXTURES, 'test-config-2.xml')); -var cfg3 = new ConfigParser(path.join(FIXTURES, 'test-config-3.xml')); - function wrapper (p, done, post) { p.then(post, function (err) { expect(err.stack).toBeUndefined(); @@ -561,10 +556,16 @@ describe('prepare', function () { /* eslint-enable no-unused-vars */ var xcOrig = xcode.project; var writeFileSyncSpy; + let cfg, cfg2, cfg3; var updateProject = prepare.__get__('updateProject'); beforeEach(function () { + // Create real config objects before mocking out everything. + cfg = new ConfigParser(path.join(FIXTURES, 'test-config.xml')); + cfg2 = new ConfigParser(path.join(FIXTURES, 'test-config-2.xml')); + cfg3 = new ConfigParser(path.join(FIXTURES, 'test-config-3.xml')); + mv = spyOn(shell, 'mv'); writeFileSyncSpy = spyOn(fs, 'writeFileSync'); @@ -584,22 +585,16 @@ describe('prepare', function () { }); it('Test#001 : should not update the app name in pbxproj', function (done) { - var cfg2OriginalName = cfg2.name; + // the original name here will be `SampleApp` (based on the xcodeproj basename) from p - // originalName here will be `SampleApp` (based on the xcodeproj basename) from p cfg2.name = function () { return 'NotSampleApp'; }; // new config has name change wrapperError(updateProject(cfg2, p.locations), done); // since the name has changed it *should* error - // originalName here will be `SampleApp` (based on the xcodeproj basename) from p cfg2.name = function () { return 'SampleApp'; }; // new config does *not* have a name change wrapper(updateProject(cfg2, p.locations), done); // since the name has not changed it *should not* error - - // restore cfg2 original name - cfg2.name = cfg2OriginalName; }); it('should write target-device preference', function (done) { - var cfg2OriginalName = cfg2.name; cfg2.name = function () { return 'SampleApp'; }; // new config does *not* have a name change writeFileSyncSpy.and.callThrough(); @@ -609,13 +604,9 @@ describe('prepare', function () { proj.parseSync(); var prop = proj.getBuildProperty('TARGETED_DEVICE_FAMILY'); expect(prop).toEqual('"1"'); // 1 is handset - - // restore cfg2 original name - cfg2.name = cfg2OriginalName; }); }); it('should write deployment-target preference', function (done) { - var cfg2OriginalName = cfg2.name; cfg2.name = function () { return 'SampleApp'; }; // new config does *not* have a name change writeFileSyncSpy.and.callThrough(); @@ -625,13 +616,9 @@ describe('prepare', function () { proj.parseSync(); var prop = proj.getBuildProperty('IPHONEOS_DEPLOYMENT_TARGET'); expect(prop).toEqual('8.0'); - - // restore cfg2 original name - cfg2.name = cfg2OriginalName; }); }); it('should write SwiftVersion preference (4.1)', function (done) { - var cfg3OriginalName = cfg3.name; cfg3.name = function () { return 'SampleApp'; }; // new config does *not* have a name change writeFileSyncSpy.and.callThrough(); wrapper(updateProject(cfg3, p.locations), done, function () { @@ -640,18 +627,13 @@ describe('prepare', function () { proj.parseSync(); var prop = proj.getBuildProperty('SWIFT_VERSION'); expect(prop).toEqual('4.1'); - - // restore cfg2 original name - cfg3.name = cfg3OriginalName; }); }); it('should write SwiftVersion preference (3.3)', function (done) { - var cfg3OriginalName = cfg3.name; cfg3.name = function () { return 'SampleApp'; }; // new config does *not* have a name change var pref = cfg3.doc.findall('platform[@name=\'ios\']/preference').filter(function (elem) { return elem.attrib.name.toLowerCase() === 'swiftversion'; })[0]; - var prefOriginalSwiftVersion = pref.attrib.value; pref.attrib.value = '3.3'; writeFileSyncSpy.and.callThrough(); wrapper(updateProject(cfg3, p.locations), done, function () { @@ -660,9 +642,6 @@ describe('prepare', function () { proj.parseSync(); var prop = proj.getBuildProperty('SWIFT_VERSION'); expect(prop).toEqual('3.3'); - // restore cfg2 original name - cfg3.name = cfg3OriginalName; - pref.attrib.value = prefOriginalSwiftVersion; }); }); @@ -1211,7 +1190,6 @@ describe('prepare', function () { }); /// /////////////////////////////////////////////// it('Test#016 : , - http and https, no clobber', function (done) { - var cfg2OriginalName = cfg2.name; // original name here is 'SampleApp' based on p // we are not testing a name change here, but testing a new config being used (name change test is above) // so we set it to the name expected @@ -1231,9 +1209,6 @@ describe('prepare', function () { expect(d.NSExceptionMinimumTLSVersion).toEqual(undefined); expect(d.NSExceptionRequiresForwardSecrecy).toEqual(undefined); expect(d.NSRequiresCertificateTransparency).toEqual(undefined); - - // restore cfg2 original name - cfg2.name = cfg2OriginalName; }); }); /// /////////////////////////////////////////////// From b863bfacf49cb8fe6e6839ab3ca969c00436fc93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Mon, 28 Oct 2019 14:31:07 +0100 Subject: [PATCH 6/8] Split test in prepare.spec into two --- tests/spec/unit/prepare.spec.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/spec/unit/prepare.spec.js b/tests/spec/unit/prepare.spec.js index 19768ed8b..745a41883 100644 --- a/tests/spec/unit/prepare.spec.js +++ b/tests/spec/unit/prepare.spec.js @@ -584,14 +584,16 @@ describe('prepare', function () { spyOn(cfg, 'getPreference'); }); - it('Test#001 : should not update the app name in pbxproj', function (done) { + it('should resolve', function (done) { // the original name here will be `SampleApp` (based on the xcodeproj basename) from p + cfg2.name = function () { return 'SampleApp'; }; // new config does *not* have a name change + wrapper(updateProject(cfg2, p.locations), done); // since the name has not changed it *should not* error + }); + it('should reject when the app name has changed', function (done) { + // the original name here will be `SampleApp` (based on the xcodeproj basename) from p cfg2.name = function () { return 'NotSampleApp'; }; // new config has name change wrapperError(updateProject(cfg2, p.locations), done); // since the name has changed it *should* error - - cfg2.name = function () { return 'SampleApp'; }; // new config does *not* have a name change - wrapper(updateProject(cfg2, p.locations), done); // since the name has not changed it *should not* error }); it('should write target-device preference', function (done) { From 3d81876eebf623d5150b2c644c404e5600cab2ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Mon, 28 Oct 2019 12:50:18 +0100 Subject: [PATCH 7/8] Fix and simplify check_reqs.checkTool tests --- tests/spec/unit/lib/check_reqs.spec.js | 104 +++++++------------------ 1 file changed, 30 insertions(+), 74 deletions(-) diff --git a/tests/spec/unit/lib/check_reqs.spec.js b/tests/spec/unit/lib/check_reqs.spec.js index b17ec8739..804b6b141 100644 --- a/tests/spec/unit/lib/check_reqs.spec.js +++ b/tests/spec/unit/lib/check_reqs.spec.js @@ -17,97 +17,53 @@ under the License. */ -var rewire = require('rewire'); -var checkReqs = rewire('../../../../bin/templates/scripts/cordova/lib/check_reqs'); +const rewire = require('rewire'); +const shell = require('shelljs'); +const versions = require('../../../../bin/templates/scripts/cordova/lib/versions'); describe('check_reqs', function () { + let checkReqs; + beforeEach(() => { + checkReqs = rewire('../../../../bin/templates/scripts/cordova/lib/check_reqs'); + }); + describe('checkTool method', () => { - const originalVersion = checkReqs.__get__('versions'); - let shellWhichSpy; - let rejectSpy; - let resolveSpy; - let getToolVersionSpy; + let checkTool; beforeEach(() => { - // Shell Spy - shellWhichSpy = jasmine.createSpy('shellWhichSpy'); - checkReqs.__set__('shell', { - which: shellWhichSpy - }); + checkTool = checkReqs.__get__('checkTool'); - // Q Spy - rejectSpy = jasmine.createSpy('rejectSpy'); - resolveSpy = jasmine.createSpy('resolveSpy'); - checkReqs.__set__('Q', { - reject: rejectSpy, - resolve: resolveSpy - }); - - // Versions Spy - getToolVersionSpy = jasmine.createSpy('rejectSpy'); + spyOn(shell, 'which').and.returnValue('/bin/node'); + spyOn(versions, 'get_tool_version').and.returnValue(Promise.resolve('1.0.0')); }); it('should not have found tool.', () => { - shellWhichSpy.and.returnValue(false); - const checkTool = checkReqs.__get__('checkTool'); - - checkTool('node', '1.0.0'); + shell.which.and.returnValue(false); - expect(rejectSpy).toHaveBeenCalledWith(jasmine.stringMatching(/^node was not found./)); + return checkTool('node', '1.0.0').then( + () => fail('Expected promise to be rejected'), + reason => expect(reason).toContain('node was not found.') + ); }); - it('should throw error because version is not following semver-notated.', (done) => { - shellWhichSpy.and.returnValue('/bin/node'); - const checkTool = checkReqs.__get__('checkTool'); - - checkReqs.__set__('versions', { - get_tool_version: getToolVersionSpy.and.returnValue(new Promise((resolve) => { - return resolve('1.0.0'); - }).catch((error) => { console.log(error); })), - compareVersions: originalVersion.compareVersions - }); - - checkTool('node', 'a.b.c').catch(err => { - expect(err).toEqual(new TypeError('Invalid Version: a.b.c')); - done(); - }); + it('should throw error because version is not following semver-notated.', () => { + return checkTool('node', 'a.b.c').then( + () => fail('Expected promise to be rejected'), + err => expect(err).toEqual(new TypeError('Invalid Version: a.b.c')) + ); }); - it('should resolve passing back tool version.', (done) => { - shellWhichSpy.and.returnValue('/bin/node'); - const checkTool = checkReqs.__get__('checkTool'); - - checkReqs.__set__('versions', { - get_tool_version: getToolVersionSpy.and.returnValue(new Promise((resolve) => { - return resolve('1.0.0'); - })), - compareVersions: originalVersion.compareVersions - }); - - checkTool('node', '1.0.0').then(() => { - let actual = resolveSpy.calls.argsFor(0)[0]; - expect(actual).toEqual({ version: '1.0.0' }); - done(); + it('should resolve passing back tool version.', () => { + return checkTool('node', '1.0.0').then(result => { + expect(result).toEqual({ version: '1.0.0' }); }); }); - it('should reject because tool does not meet minimum requirement.', (done) => { - shellWhichSpy.and.returnValue('/bin/node'); - const checkTool = checkReqs.__get__('checkTool'); - - checkReqs.__set__('versions', { - get_tool_version: getToolVersionSpy.and.returnValue(new Promise((resolve) => { - return resolve('1.0.0'); - })), - compareVersions: originalVersion.compareVersions - }); - - checkTool('node', '1.0.1').then(() => { - let actual = rejectSpy.calls.argsFor(0)[0]; - expect(actual).toContain('version 1.0.1 or greater'); - expect(actual).toContain('you have version 1.0.0'); - done(); - }); + it('should reject because tool does not meet minimum requirement.', () => { + return checkTool('node', '1.0.1').then( + () => fail('Expected promise to be rejected'), + reason => expect(reason).toContain('version 1.0.1 or greater, you have version 1.0.0') + ); }); }); From 4c66954c27b3913890f4530db89f684892312ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Mon, 28 Oct 2019 13:58:25 +0100 Subject: [PATCH 8/8] Simplify test for build.getDefaultSimulatorTarget --- tests/spec/unit/build.spec.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/tests/spec/unit/build.spec.js b/tests/spec/unit/build.spec.js index e0c51a23f..d6998fd95 100644 --- a/tests/spec/unit/build.spec.js +++ b/tests/spec/unit/build.spec.js @@ -415,25 +415,18 @@ describe('build', function () { // This method will require a module that supports the run method. build.__set__('require', () => { return { - run: () => { - return new Promise((resolve, reject) => { - resolve(mockedEmulators); - }); - } + run: () => Promise.resolve(mockedEmulators) }; }); const getDefaultSimulatorTarget = build.__get__('getDefaultSimulatorTarget'); - const exec = getDefaultSimulatorTarget(); - const expected = { - name: 'iPhone X', - identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-X', - simIdentifier: 'iPhone-X' - }; - - exec.then((actual) => { - expect(actual).toEqual(expected); + getDefaultSimulatorTarget().then((actual) => { + expect(actual).toEqual({ + name: 'iPhone X', + identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-X', + simIdentifier: 'iPhone-X' + }); done(); }); });