From 1d56c64b9fe1f5dd39e2d698ab3c2dd9802c6a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Thu, 30 Aug 2018 19:51:54 +0200 Subject: [PATCH 1/3] Fix test timeout --- spec/cli.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/cli.spec.js b/spec/cli.spec.js index 8212523e3..a64434246 100644 --- a/spec/cli.spec.js +++ b/spec/cli.spec.js @@ -69,8 +69,8 @@ describe('cordova cli', function () { cli(['node', 'cordova', '--version'], function () { expect(logger.results.calls.mostRecent().args[0]).toMatch(version); done(); - }, 60000); - }); + }); + }, 60000); it('Test#003 : will spit out the version with -v anywhere', function (done) { cli(['node', 'cordova', 'one', '-v', 'three'], function () { From 55a454704e7d8a117e807216b19aa618d00ec612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Thu, 30 Aug 2018 19:50:30 +0200 Subject: [PATCH 2/3] Let Jasmine handle promises --- spec/cli.spec.js | 225 +++++++++++++++++++--------------------------- spec/help.spec.js | 6 +- 2 files changed, 93 insertions(+), 138 deletions(-) diff --git a/spec/cli.spec.js b/spec/cli.spec.js index a64434246..c2e3d81f6 100644 --- a/spec/cli.spec.js +++ b/spec/cli.spec.js @@ -58,24 +58,21 @@ describe('cordova cli', function () { describe('version', function () { var version = require('../package').version; - it('Test#001 : will spit out the version with -v', function (done) { - cli(['node', 'cordova', '-v'], function () { + it('Test#001 : will spit out the version with -v', () => { + return cli(['node', 'cordova', '-v']).then(() => { expect(logger.results.calls.mostRecent().args[0]).toMatch(version); - done(); }); }, 60000); - it('Test#002 : will spit out the version with --version', function (done) { - cli(['node', 'cordova', '--version'], function () { + it('Test#002 : will spit out the version with --version', () => { + return cli(['node', 'cordova', '--version']).then(() => { expect(logger.results.calls.mostRecent().args[0]).toMatch(version); - done(); }); }, 60000); - it('Test#003 : will spit out the version with -v anywhere', function (done) { - cli(['node', 'cordova', 'one', '-v', 'three'], function () { + it('Test#003 : will spit out the version with -v anywhere', () => { + return cli(['node', 'cordova', 'one', '-v', 'three']).then(() => { expect(logger.results.calls.mostRecent().args[0]).toMatch(version); - done(); }); }, 60000); }); @@ -86,45 +83,39 @@ describe('cordova cli', function () { spyOn(cordova, 'build').and.returnValue(Promise.resolve()); }); - it('Test#005 : will call command with all arguments passed through', function (done) { - cli(['node', 'cordova', 'build', 'blackberry10', '--', '-k', 'abcd1234'], function () { + it('Test#005 : will call command with all arguments passed through', () => { + return cli(['node', 'cordova', 'build', 'blackberry10', '--', '-k', 'abcd1234']).then(() => { expect(cordova.build).toHaveBeenCalledWith({ platforms: ['blackberry10'], options: { argv: ['-k', 'abcd1234'] }, verbose: false, silent: false, browserify: false, fetch: true, nohooks: [ ], searchpath: undefined }); - done(); }); }, 60000); - it('Test#006 : will consume the first instance of -d', function (done) { - cli(['node', 'cordova', '-d', 'build', 'blackberry10', '--', '-k', 'abcd1234', '-d'], function () { + it('Test#006 : will consume the first instance of -d', () => { + return cli(['node', 'cordova', '-d', 'build', 'blackberry10', '--', '-k', 'abcd1234', '-d']).then(() => { expect(cordova.build).toHaveBeenCalledWith({ platforms: ['blackberry10'], options: { verbose: true, argv: ['-k', 'abcd1234', '-d'] }, verbose: true, silent: false, browserify: false, fetch: true, nohooks: [ ], searchpath: undefined }); - done(); }); }); - it('Test#007 : will consume the first instance of --verbose', function (done) { - cli(['node', 'cordova', '--verbose', 'build', 'blackberry10', '--', '-k', 'abcd1234', '--verbose'], function () { + it('Test#007 : will consume the first instance of --verbose', () => { + return cli(['node', 'cordova', '--verbose', 'build', 'blackberry10', '--', '-k', 'abcd1234', '--verbose']).then(() => { expect(cordova.build).toHaveBeenCalledWith({ platforms: ['blackberry10'], options: { verbose: true, argv: ['-k', 'abcd1234', '--verbose'] }, verbose: true, silent: false, browserify: false, fetch: true, nohooks: [ ], searchpath: undefined }); - done(); }); }); - it('Test#008 : will consume the first instance of either --verbose or -d', function (done) { - cli(['node', 'cordova', '--verbose', 'build', 'blackberry10', '--', '-k', 'abcd1234', '-d'], function () { + it('Test#008 : will consume the first instance of either --verbose or -d', () => { + return cli(['node', 'cordova', '--verbose', 'build', 'blackberry10', '--', '-k', 'abcd1234', '-d']).then(() => { expect(cordova.build).toHaveBeenCalledWith({ platforms: ['blackberry10'], options: { verbose: true, argv: ['-k', 'abcd1234', '-d'] }, verbose: true, silent: false, browserify: false, fetch: true, nohooks: [ ], searchpath: undefined }); - done(); }); }); - it('Test#009 : will consume the first instance of either --verbose or -d', function (done) { - cli(['node', 'cordova', '-d', 'build', 'blackberry10', '--', '-k', 'abcd1234', '--verbose'], function () { + it('Test#009 : will consume the first instance of either --verbose or -d', () => { + return cli(['node', 'cordova', '-d', 'build', 'blackberry10', '--', '-k', 'abcd1234', '--verbose']).then(() => { expect(cordova.build).toHaveBeenCalledWith({ platforms: ['blackberry10'], options: { verbose: true, argv: ['-k', 'abcd1234', '--verbose'] }, verbose: true, silent: false, browserify: false, fetch: true, nohooks: [ ], searchpath: undefined }); - done(); }); }); - it('Test#010 : will consume the first instance of --silent', function (done) { - cli(['node', 'cordova', '--silent', 'build', 'blackberry10', '--', '-k', 'abcd1234', '--silent'], function () { + it('Test#010 : will consume the first instance of --silent', () => { + return cli(['node', 'cordova', '--silent', 'build', 'blackberry10', '--', '-k', 'abcd1234', '--silent']).then(() => { expect(cordova.build).toHaveBeenCalledWith({ platforms: ['blackberry10'], options: { silent: true, argv: ['-k', 'abcd1234', '--silent'] }, verbose: false, silent: true, browserify: false, fetch: true, nohooks: [ ], searchpath: undefined }); - done(); }); }); }); @@ -134,10 +125,9 @@ describe('cordova cli', function () { spyOn(cordova, 'create').and.returnValue(Promise.resolve()); }); - it('Test#011 : calls cordova create', function (done) { - cli(['node', 'cordova', 'create', 'a', 'b', 'c', '--link-to', 'c:\\personalWWW'], function () { + it('Test#011 : calls cordova create', () => { + return cli(['node', 'cordova', 'create', 'a', 'b', 'c', '--link-to', 'c:\\personalWWW']).then(() => { expect(cordova.create).toHaveBeenCalledWith('a', 'b', 'c', jasmine.any(Object), jasmine.any(Object)); - done(); }); }); }); @@ -147,8 +137,8 @@ describe('cordova cli', function () { spyOn(cordova, 'plugin').and.returnValue(Promise.resolve()); }); - it('Test#012 : will pass variables', function (done) { - cli(['node', 'cordova', 'plugin', 'add', 'facebook', '--variable', 'FOO=foo'], function () { + it('Test#012 : will pass variables', () => { + return cli(['node', 'cordova', 'plugin', 'add', 'facebook', '--variable', 'FOO=foo']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'add', ['facebook'], @@ -156,12 +146,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.cli_variables.FOO).toBe('foo'); - done(); }); }); - it('Test#013 : will support variables with =', function (done) { - cli(['node', 'cordova', 'plugin', 'add', 'facebook', '--variable', 'MOTO=DELTA=WAS=HERE'], function () { + it('Test#013 : will support variables with =', () => { + return cli(['node', 'cordova', 'plugin', 'add', 'facebook', '--variable', 'MOTO=DELTA=WAS=HERE']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'add', ['facebook'], @@ -169,12 +158,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.cli_variables.MOTO).toBe('DELTA=WAS=HERE'); - done(); }); }); - it('Test#014 : will pass hook patterns to suppress', function (done) { - cli(['node', 'cordova', 'plugin', 'add', 'facebook', '--nohooks', 'before_plugin_add'], function () { + it('Test#014 : will pass hook patterns to suppress', () => { + return cli(['node', 'cordova', 'plugin', 'add', 'facebook', '--nohooks', 'before_plugin_add']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'add', ['facebook'], @@ -182,12 +170,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.nohooks[0]).toBe('before_plugin_add'); - done(); }); }); - it('Test #015 : (add) will pass save:true', function (done) { - cli(['node', 'cordova', 'plugin', 'add', 'device'], function () { + it('Test #015 : (add) will pass save:true', () => { + return cli(['node', 'cordova', 'plugin', 'add', 'device']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'add', ['device'], @@ -195,12 +182,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save).toBe(true); - done(); }); }); - it('Test #016 : (add) will pass save:false', function (done) { - cli(['node', 'cordova', 'plugin', 'add', 'device', '--nosave'], function () { + it('Test #016 : (add) will pass save:false', () => { + return cli(['node', 'cordova', 'plugin', 'add', 'device', '--nosave']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'add', ['device'], @@ -208,12 +194,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save).toBe(false); - done(); }); }); - it('Test #017: (remove) will pass save:false', function (done) { - cli(['node', 'cordova', 'plugin', 'remove', 'device', '--nosave'], function () { + it('Test #017: (remove) will pass save:false', () => { + return cli(['node', 'cordova', 'plugin', 'remove', 'device', '--nosave']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'remove', ['device'], @@ -221,12 +206,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save).toBe(false); - done(); }); }); - it('Test #018 : (remove) autosave is default and will pass save:true', function (done) { - cli(['node', 'cordova', 'plugin', 'remove', 'device'], function () { + it('Test #018 : (remove) autosave is default and will pass save:true', () => { + return cli(['node', 'cordova', 'plugin', 'remove', 'device']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'remove', ['device'], @@ -234,12 +218,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save).toBe(true); - done(); }); }); - it('Test #020 : (add) fetch is true by default and will pass fetch:true', function (done) { - cli(['node', 'cordova', 'plugin', 'add', 'device'], function () { + it('Test #020 : (add) fetch is true by default and will pass fetch:true', () => { + return cli(['node', 'cordova', 'plugin', 'add', 'device']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'add', ['device'], @@ -247,12 +230,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.fetch).toBe(true); - done(); }); }); - it('(add) will pass save-exact:true', function (done) { - cli(['node', 'cordova', 'plugin', 'add', 'device', '--save-exact'], function () { + it('(add) will pass save-exact:true', () => { + return cli(['node', 'cordova', 'plugin', 'add', 'device', '--save-exact']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'add', ['device'], @@ -260,12 +242,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save_exact).toBe(true); - done(); }); }); - it('Test #021 : (remove) fetch is true by default and will pass fetch:true', function (done) { - cli(['node', 'cordova', 'plugin', 'remove', 'device'], function () { + it('Test #021 : (remove) fetch is true by default and will pass fetch:true', () => { + return cli(['node', 'cordova', 'plugin', 'remove', 'device']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'remove', ['device'], @@ -273,12 +254,11 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.fetch).toBe(true); - done(); }); }); - it('(add) will pass noprod:true and production:false', function (done) { - cli(['node', 'cordova', 'plugin', 'add', 'device', '--noprod'], function () { + it('(add) will pass noprod:true and production:false', () => { + return cli(['node', 'cordova', 'plugin', 'add', 'device', '--noprod']).then(() => { expect(cordova.plugin).toHaveBeenCalledWith( 'add', ['device'], @@ -286,76 +266,71 @@ describe('cordova cli', function () { ); var opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.production).toBe(false); - done(); }); }); }); describe('telemetry', function () { - it("Test#023 : skips prompt when user runs 'cordova telemetry X'", function (done) { + it("Test#023 : skips prompt when user runs 'cordova telemetry X'", () => { var wasPromptShown = false; spyOn(telemetry, 'showPrompt').and.callFake(function () { wasPromptShown = true; }); - cli(['node', 'cordova', 'telemetry', 'on'], function () { - cli(['node', 'cordova', 'telemetry', 'off'], function () { + return Promise.resolve() + .then(_ => cli(['node', 'cordova', 'telemetry', 'on'])) + .then(_ => cli(['node', 'cordova', 'telemetry', 'off'])) + .then(() => { expect(wasPromptShown).toBeFalsy(); - done(); }); - }); }); - it("Test#024 : is NOT collected when user runs 'cordova telemetry on' while NOT opted-in", function (done) { + it("Test#024 : is NOT collected when user runs 'cordova telemetry on' while NOT opted-in", () => { spyOn(telemetry, 'isOptedIn').and.returnValue(false); spyOn(telemetry, 'isCI').and.returnValue(false); spyOn(telemetry, 'track'); - cli(['node', 'cordova', 'telemetry', 'on'], function () { + return cli(['node', 'cordova', 'telemetry', 'on']).then(() => { expect(telemetry.track).not.toHaveBeenCalled(); - done(); }); }); - it("Test#025 : is collected when user runs 'cordova telemetry off' while opted-in", function (done) { + it("Test#025 : is collected when user runs 'cordova telemetry off' while opted-in", () => { spyOn(telemetry, 'isOptedIn').and.returnValue(true); spyOn(telemetry, 'isCI').and.returnValue(false); spyOn(telemetry, 'track'); - cli(['node', 'cordova', 'telemetry', 'off'], function () { + return cli(['node', 'cordova', 'telemetry', 'off']).then(() => { expect(telemetry.track).toHaveBeenCalledWith('telemetry', 'off', 'via-cordova-telemetry-cmd', 'successful'); - done(); }); }); - it('Test#026 : tracks platforms/plugins subcommands', function (done) { + it('Test#026 : tracks platforms/plugins subcommands', () => { spyOn(telemetry, 'isOptedIn').and.returnValue(true); spyOn(telemetry, 'isCI').and.returnValue(false); spyOn(telemetry, 'hasUserOptedInOrOut').and.returnValue(true); spyOn(cordova, 'platform').and.returnValue(Promise.resolve()); spyOn(telemetry, 'track'); - cli(['node', 'cordova', 'platform', 'add', 'ios'], function () { + return cli(['node', 'cordova', 'platform', 'add', 'ios']).then(() => { expect(telemetry.track).toHaveBeenCalledWith('platform', 'add', 'successful'); - done(); }); }); - it('Test#027 : shows prompt if user neither opted in or out yet', function (done) { + it('Test#027 : shows prompt if user neither opted in or out yet', () => { spyOn(cordova, 'prepare').and.returnValue(Promise.resolve()); spyOn(telemetry, 'hasUserOptedInOrOut').and.returnValue(false); spyOn(telemetry, 'isCI').and.returnValue(false); spyOn(telemetry, 'isNoTelemetryFlag').and.returnValue(false); spyOn(telemetry, 'showPrompt').and.returnValue(Promise.resolve(false)); - cli(['node', 'cordova', 'prepare'], function () { + return cli(['node', 'cordova', 'prepare']).then(() => { expect(telemetry.showPrompt).toHaveBeenCalled(); - done(); }); }); // note: we override telemetry timeout here so we don't need to wait 30 seconds - it('Test#028 : opts-out if prompt times out AND it tracks opt-out', function (done) { + it('Test#028 : opts-out if prompt times out AND it tracks opt-out', () => { // Remove any optOut settings that might have been saved // ... and force prompt to be shown telemetry.clear(); @@ -364,74 +339,69 @@ describe('cordova cli', function () { spyOn(telemetry, 'track'); telemetry.timeoutInSecs = 1; - cli(['node', 'cordova', '--version'], function () { + return cli(['node', 'cordova', '--version']).then(() => { if (process.env.CI) { expect(telemetry.isOptedIn()).toBeTruthy(); } else { expect(telemetry.isOptedIn()).toBeFalsy(); } expect(telemetry.track).toHaveBeenCalledWith('telemetry', 'off', 'via-cli-prompt-choice', 'successful'); - done(); }); }); - it("Test#029 : is NOT collected in CI environments and doesn't prompt", function (done) { + it("Test#029 : is NOT collected in CI environments and doesn't prompt", () => { spyOn(telemetry, 'hasUserOptedInOrOut').and.returnValue(true); spyOn(telemetry, 'isOptedIn').and.returnValue(true); spyOn(telemetry, 'isCI').and.returnValue(true); spyOn(telemetry, 'showPrompt'); spyOn(telemetry, 'track'); - cli(['node', 'cordova', '--version'], function () { + return cli(['node', 'cordova', '--version']).then(() => { expect(telemetry.showPrompt).not.toHaveBeenCalled(); expect(telemetry.track).not.toHaveBeenCalled(); - done(); }); }); - it("Test#030 : is NOT collected when --no-telemetry flag found and doesn't prompt", function (done) { + it("Test#030 : is NOT collected when --no-telemetry flag found and doesn't prompt", () => { spyOn(telemetry, 'hasUserOptedInOrOut').and.returnValue(false); spyOn(telemetry, 'isOptedIn').and.returnValue(true); spyOn(telemetry, 'isCI').and.returnValue(false); spyOn(telemetry, 'showPrompt'); spyOn(telemetry, 'track'); - cli(['node', 'cordova', '--version', '--no-telemetry'], function () { + return cli(['node', 'cordova', '--version', '--no-telemetry']).then(() => { expect(telemetry.showPrompt).not.toHaveBeenCalled(); expect(telemetry.track).not.toHaveBeenCalled(); - done(); }); }); - it('Test#031 : is NOT collected if user opted out', function (done) { + it('Test#031 : is NOT collected if user opted out', () => { spyOn(telemetry, 'hasUserOptedInOrOut').and.returnValue(true); spyOn(telemetry, 'isOptedIn').and.returnValue(false); spyOn(telemetry, 'isCI').and.returnValue(false); spyOn(telemetry, 'showPrompt'); spyOn(telemetry, 'track'); - cli(['node', 'cordova', '--version'], function () { + return cli(['node', 'cordova', '--version']).then(() => { expect(telemetry.showPrompt).not.toHaveBeenCalled(); expect(telemetry.track).not.toHaveBeenCalled(); - done(); }); }); - it('Test#032 : is collected if user opted in', function (done) { + it('Test#032 : is collected if user opted in', () => { spyOn(telemetry, 'hasUserOptedInOrOut').and.returnValue(true); spyOn(telemetry, 'isOptedIn').and.returnValue(true); spyOn(telemetry, 'isCI').and.returnValue(false); spyOn(telemetry, 'showPrompt'); spyOn(telemetry, 'track'); - cli(['node', 'cordova', '--version'], function () { + return cli(['node', 'cordova', '--version']).then(() => { expect(telemetry.showPrompt).not.toHaveBeenCalled(); expect(telemetry.track).toHaveBeenCalled(); - done(); }); }); - it("Test#033 : track opt-out that happened via 'cordova telemetry off' even if user is NOT opted-in ", function (done) { + it("Test#033 : track opt-out that happened via 'cordova telemetry off' even if user is NOT opted-in ", () => { spyOn(telemetry, 'isCI').and.returnValue(false); spyOn(telemetry, 'isOptedIn').and.returnValue(false); // same as calling `telemetry.turnOff();` spyOn(telemetry, 'hasUserOptedInOrOut').and.returnValue(true); @@ -439,10 +409,9 @@ describe('cordova cli', function () { expect(telemetry.isOptedIn()).toBeFalsy(); - cli(['node', 'cordova', 'telemetry', 'off'], function () { + return cli(['node', 'cordova', 'telemetry', 'off']).then(() => { expect(telemetry.isOptedIn()).toBeFalsy(); expect(telemetry.track).toHaveBeenCalledWith('telemetry', 'off', 'via-cordova-telemetry-cmd', 'successful'); - done(); }); }); }); @@ -455,8 +424,8 @@ describe('platform', function () { logger.setLevel('error'); }); - it('Test #034 : (add) autosave is the default setting for platform add', function (done) { - cli(['node', 'cordova', 'platform', 'add', 'ios'], function () { + it('Test #034 : (add) autosave is the default setting for platform add', () => { + return cli(['node', 'cordova', 'platform', 'add', 'ios']).then(() => { expect(cordova.platform).toHaveBeenCalledWith( 'add', ['ios'], @@ -464,12 +433,11 @@ describe('platform', function () { ); var opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.save).toBe(true); - done(); }); }); - it('Test #035 : (add) platform is not saved when --nosave is passed in', function (done) { - cli(['node', 'cordova', 'platform', 'add', 'ios', '--nosave'], function () { + it('Test #035 : (add) platform is not saved when --nosave is passed in', () => { + return cli(['node', 'cordova', 'platform', 'add', 'ios', '--nosave']).then(() => { expect(cordova.platform).toHaveBeenCalledWith( 'add', ['ios'], @@ -477,12 +445,11 @@ describe('platform', function () { ); var opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.save).toBe(false); - done(); }); }); - it('Test #036 : (remove) autosave is the default setting for platform remove', function (done) { - cli(['node', 'cordova', 'platform', 'remove', 'ios'], function () { + it('Test #036 : (remove) autosave is the default setting for platform remove', () => { + return cli(['node', 'cordova', 'platform', 'remove', 'ios']).then(() => { expect(cordova.platform).toHaveBeenCalledWith( 'remove', ['ios'], @@ -490,12 +457,11 @@ describe('platform', function () { ); var opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.save).toBe(true); - done(); }); }); - it('Test #037 : (remove) platform is not removed when --nosave is passed in', function (done) { - cli(['node', 'cordova', 'platform', 'remove', 'ios', '--nosave'], function () { + it('Test #037 : (remove) platform is not removed when --nosave is passed in', () => { + return cli(['node', 'cordova', 'platform', 'remove', 'ios', '--nosave']).then(() => { expect(cordova.platform).toHaveBeenCalledWith( 'remove', ['ios'], @@ -503,12 +469,11 @@ describe('platform', function () { ); var opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.save).toBe(false); - done(); }); }); - it('Test #039 : (add) fetch is true by default and will pass fetch:true', function (done) { - cli(['node', 'cordova', 'platform', 'add', 'device'], function () { + it('Test #039 : (add) fetch is true by default and will pass fetch:true', () => { + return cli(['node', 'cordova', 'platform', 'add', 'device']).then(() => { expect(cordova.platform).toHaveBeenCalledWith( 'add', ['device'], @@ -516,12 +481,11 @@ describe('platform', function () { ); var opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.fetch).toBe(true); - done(); }); }); - it('Test #040 : (remove) fetch is true by default and will pass fetch:true', function (done) { - cli(['node', 'cordova', 'platform', 'remove', 'device'], function () { + it('Test #040 : (remove) fetch is true by default and will pass fetch:true', () => { + return cli(['node', 'cordova', 'platform', 'remove', 'device']).then(() => { expect(cordova.platform).toHaveBeenCalledWith( 'remove', ['device'], @@ -529,7 +493,6 @@ describe('platform', function () { ); var opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.fetch).toBe(true); - done(); }); }); }); @@ -575,50 +538,44 @@ describe('config', function () { confHolder = undefined; }); - it('Test#042 : config set autosave is called with true', function (done) { - cli(['node', 'cordova', 'config', 'set', 'autosave', 'true', '--silent'], function () { + it('Test#042 : config set autosave is called with true', () => { + return cli(['node', 'cordova', 'config', 'set', 'autosave', 'true', '--silent']).then(() => { expect(cordovaConfig.autosave).toBe('true'); - done(); }); }); - it('Test#043 : config delete autosave is called', function (done) { - cli(['node', 'cordova', 'config', 'delete', 'autosave'], function () { + it('Test#043 : config delete autosave is called', () => { + return cli(['node', 'cordova', 'config', 'delete', 'autosave']).then(() => { expect(cordovaConfig.autosave).toBeUndefined(); - done(); }); }); - it('Test#044 : config set is called even without value, defaults to true', function (done) { - cli(['node', 'cordova', 'config', 'set', 'autosave'], function () { + it('Test#044 : config set is called even without value, defaults to true', () => { + return cli(['node', 'cordova', 'config', 'set', 'autosave']).then(() => { expect(cordovaConfig.autosave).toBe(true); - done(); }); }); - it('Test #045 : config get is called', function (done) { - cli(['node', 'cordova', 'config', 'get', 'autosave'], function () { + it('Test #045 : config get is called', () => { + return cli(['node', 'cordova', 'config', 'get', 'autosave']).then(() => { expect(confHolder).toBe(true); - done(); }); }); - it('Test #046 : config edit is called', function (done) { - cli(['node', 'cordova', 'config', 'edit'], function () { + it('Test #046 : config edit is called', () => { + return cli(['node', 'cordova', 'config', 'edit']).then(() => { expect(path.basename(editorArgs)).toEqual('cordova-config.json'); expect(confHolder).toEqual('Pathcalled'); - done(); }); }); - it('Test #047 : config ls is called', function (done) { + it('Test #047 : config ls is called', () => { spyOn(fs, 'readFile').and.callFake(function (confPath, cb) { confHolder = confPath(); }); - cli(['node', 'cordova', 'config', 'ls'], function () { + return cli(['node', 'cordova', 'config', 'ls']).then(() => { expect(path.basename(confHolder)).toEqual('cordova-config.json'); - done(); }); }); diff --git a/spec/help.spec.js b/spec/help.spec.js index 1697a97ff..9f8c910a7 100644 --- a/spec/help.spec.js +++ b/spec/help.spec.js @@ -30,11 +30,10 @@ describe('help', function () { }); describe('return results, and no long lines', function () { allcommands.forEach(function (k) { - it(k, function (done) { + it(k, () => { var result = help([k]); expect(result).toMatch(/^Synopsis/); expect(result.split('\n').filter(function (l) { return l.length > 130; }).length).toBe(0); - done(); }); }); }); @@ -48,10 +47,9 @@ describe('help', function () { cordova.binname = binname; }); allcommands.forEach(function (k) { - it(k || '(default)', function (done) { + it(k || '(default)', () => { var result = help([k]); expect(result.split('\n')[2]).toMatch(RegExp(testname + ' (?:' + k + '|command)\\b')); - done(); }); }); }); From a72032bf4436bb534c2f8c033122953f405d79b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Thu, 30 Aug 2018 20:17:20 +0200 Subject: [PATCH 3/3] Use some ES6 features in tests --- spec/cli.spec.js | 122 +++++++++++++++++++++------------------------- spec/help.spec.js | 36 +++++++------- 2 files changed, 74 insertions(+), 84 deletions(-) diff --git a/spec/cli.spec.js b/spec/cli.spec.js index c2e3d81f6..13294b506 100644 --- a/spec/cli.spec.js +++ b/spec/cli.spec.js @@ -15,33 +15,26 @@ under the License. */ -var rewire = require('rewire'); -var cli = rewire('../src/cli'); -var cordova_lib = require('cordova-lib'); -var events = cordova_lib.events; -var cordova = cordova_lib.cordova; -var telemetry = require('../src/telemetry'); -var path = require('path'); -var fs = require('fs'); -var logger = require('cordova-common').CordovaLogger.get(); +const fs = require('fs'); +const path = require('path'); +const rewire = require('rewire'); +const { events, cordova } = require('cordova-lib'); +const logger = require('cordova-common').CordovaLogger.get(); +const telemetry = require('../src/telemetry'); +const cli = rewire('../src/cli'); // avoid node complaining of too many event listener added process.setMaxListeners(0); -describe('cordova cli', function () { - beforeEach(function () { +describe('cordova cli', () => { + beforeEach(() => { // Event registration is currently process-global. Since all jasmine // tests in a directory run in a single process (and in parallel), // logging events registered as a result of the "--verbose" flag in // CLI testing below would cause lots of logging messages printed out by other specs. // This is required so that fake events chaining works (events.on('log').on('verbose')...) - var FakeEvents = function FakeEvents () {}; - FakeEvents.prototype.on = function fakeOn () { - return new FakeEvents(); - }; - - spyOn(events, 'on').and.returnValue(new FakeEvents()); + spyOn(events, 'on').and.returnValue({ on () { return this; } }); // Spy and mute output spyOn(logger, 'results'); @@ -49,14 +42,14 @@ describe('cordova cli', function () { spyOn(console, 'log'); // Prevent accidentally turning telemetry on/off during testing - telemetry.turnOn = function () {}; - telemetry.turnOff = function () {}; - telemetry.track = function () {}; + telemetry.turnOn = () => {}; + telemetry.turnOff = () => {}; + telemetry.track = () => {}; }); - describe('options', function () { - describe('version', function () { - var version = require('../package').version; + describe('options', () => { + describe('version', () => { + const version = require('../package').version; it('Test#001 : will spit out the version with -v', () => { return cli(['node', 'cordova', '-v']).then(() => { @@ -78,8 +71,8 @@ describe('cordova cli', function () { }); }); - describe('Test#004 : project commands other than plugin and platform', function () { - beforeEach(function () { + describe('Test#004 : project commands other than plugin and platform', () => { + beforeEach(() => { spyOn(cordova, 'build').and.returnValue(Promise.resolve()); }); @@ -120,8 +113,8 @@ describe('cordova cli', function () { }); }); - describe('create', function () { - beforeEach(function () { + describe('create', () => { + beforeEach(() => { spyOn(cordova, 'create').and.returnValue(Promise.resolve()); }); @@ -132,8 +125,8 @@ describe('cordova cli', function () { }); }); - describe('plugin', function () { - beforeEach(function () { + describe('plugin', () => { + beforeEach(() => { spyOn(cordova, 'plugin').and.returnValue(Promise.resolve()); }); @@ -144,7 +137,7 @@ describe('cordova cli', function () { ['facebook'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.cli_variables.FOO).toBe('foo'); }); }); @@ -156,7 +149,7 @@ describe('cordova cli', function () { ['facebook'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.cli_variables.MOTO).toBe('DELTA=WAS=HERE'); }); }); @@ -168,7 +161,7 @@ describe('cordova cli', function () { ['facebook'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.nohooks[0]).toBe('before_plugin_add'); }); }); @@ -180,7 +173,7 @@ describe('cordova cli', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save).toBe(true); }); }); @@ -192,7 +185,7 @@ describe('cordova cli', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save).toBe(false); }); }); @@ -204,7 +197,7 @@ describe('cordova cli', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save).toBe(false); }); }); @@ -216,7 +209,7 @@ describe('cordova cli', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save).toBe(true); }); }); @@ -228,7 +221,7 @@ describe('cordova cli', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.fetch).toBe(true); }); }); @@ -240,7 +233,7 @@ describe('cordova cli', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.save_exact).toBe(true); }); }); @@ -252,7 +245,7 @@ describe('cordova cli', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.fetch).toBe(true); }); }); @@ -264,16 +257,16 @@ describe('cordova cli', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.plugin.calls.argsFor(0)[2]; + const opts = cordova.plugin.calls.argsFor(0)[2]; expect(opts.production).toBe(false); }); }); }); - describe('telemetry', function () { + describe('telemetry', () => { it("Test#023 : skips prompt when user runs 'cordova telemetry X'", () => { - var wasPromptShown = false; - spyOn(telemetry, 'showPrompt').and.callFake(function () { + let wasPromptShown = false; + spyOn(telemetry, 'showPrompt').and.callFake(() => { wasPromptShown = true; }); @@ -417,9 +410,9 @@ describe('cordova cli', function () { }); }); -describe('platform', function () { +describe('platform', () => { - beforeEach(function () { + beforeEach(() => { spyOn(cordova, 'platform').and.returnValue(Promise.resolve()); logger.setLevel('error'); }); @@ -431,7 +424,7 @@ describe('platform', function () { ['ios'], jasmine.any(Object) ); - var opts = cordova.platform.calls.argsFor(0)[2]; + const opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.save).toBe(true); }); }); @@ -443,7 +436,7 @@ describe('platform', function () { ['ios'], jasmine.any(Object) ); - var opts = cordova.platform.calls.argsFor(0)[2]; + const opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.save).toBe(false); }); }); @@ -455,7 +448,7 @@ describe('platform', function () { ['ios'], jasmine.any(Object) ); - var opts = cordova.platform.calls.argsFor(0)[2]; + const opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.save).toBe(true); }); }); @@ -467,7 +460,7 @@ describe('platform', function () { ['ios'], jasmine.any(Object) ); - var opts = cordova.platform.calls.argsFor(0)[2]; + const opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.save).toBe(false); }); }); @@ -479,7 +472,7 @@ describe('platform', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.platform.calls.argsFor(0)[2]; + const opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.fetch).toBe(true); }); }); @@ -491,38 +484,35 @@ describe('platform', function () { ['device'], jasmine.any(Object) ); - var opts = cordova.platform.calls.argsFor(0)[2]; + const opts = cordova.platform.calls.argsFor(0)[2]; expect(opts.fetch).toBe(true); }); }); }); -describe('config', function () { - var clirevert; - var confrevert; - var editorArgs; - var cordovaConfig = {}; - var confHolder; +describe('config', () => { + let clirevert, confrevert, editorArgs, confHolder; + const cordovaConfig = {}; - var confMock = { - set: function (key, value) { + const confMock = { + set (key, value) { cordovaConfig[key] = value; }, - del: function (key) { + del (key) { delete cordovaConfig[key]; }, - path: function () { + path () { confHolder = 'Pathcalled'; return 'some/path/cordova-config.json'; }, - get: function (key) { + get (key) { confHolder = cordovaConfig[key]; return cordovaConfig[key]; } }; - beforeEach(function () { - clirevert = cli.__set__('editor', function (path1, cb) { + beforeEach(() => { + clirevert = cli.__set__('editor', (path1, cb) => { editorArgs = path1(); cb(); }); @@ -532,7 +522,7 @@ describe('config', function () { // spyOn(console, 'log'); }); - afterEach(function () { + afterEach(() => { clirevert(); confrevert(); confHolder = undefined; @@ -570,7 +560,7 @@ describe('config', function () { }); it('Test #047 : config ls is called', () => { - spyOn(fs, 'readFile').and.callFake(function (confPath, cb) { + spyOn(fs, 'readFile').and.callFake((confPath, cb) => { confHolder = confPath(); }); diff --git a/spec/help.spec.js b/spec/help.spec.js index 9f8c910a7..d02b4f471 100644 --- a/spec/help.spec.js +++ b/spec/help.spec.js @@ -14,41 +14,41 @@ specific language governing permissions and limitations under the License. */ -var cordova_lib = require('cordova-lib'); -var cordova = cordova_lib.cordova; -var help = require('../src/help'); -var allcommands = [ + +const { cordova } = require('cordova-lib'); +const help = require('../src/help'); +const allcommands = [ '', 'prepare', 'build', 'config', 'emulate', 'plugin', 'plugins', 'serve', 'platform', 'platforms', 'compile', 'run', 'info', 'targets', 'requirements', 'projectMetadata', 'clean' ]; -describe('help', function () { - describe('commands should', function () { - afterEach(function () { +describe('help', () => { + describe('commands should', () => { + afterEach(() => { cordova.removeAllListeners('results'); }); - describe('return results, and no long lines', function () { - allcommands.forEach(function (k) { + describe('return results, and no long lines', () => { + allcommands.forEach(k => { it(k, () => { - var result = help([k]); + const result = help([k]); expect(result).toMatch(/^Synopsis/); - expect(result.split('\n').filter(function (l) { return l.length > 130; }).length).toBe(0); + expect(result.split('\n').filter(l => l.length > 130).length).toBe(0); }); }); }); - describe('use cordova-cli instead of cordova:', function () { - var binname = cordova.binname; - var testname = 'testgap'; - beforeEach(function () { + describe('use cordova-cli instead of cordova:', () => { + const binname = cordova.binname; + const testname = 'testgap'; + beforeEach(() => { cordova.binname = testname; }); - afterEach(function () { + afterEach(() => { cordova.binname = binname; }); - allcommands.forEach(function (k) { + allcommands.forEach(k => { it(k || '(default)', () => { - var result = help([k]); + const result = help([k]); expect(result.split('\n')[2]).toMatch(RegExp(testname + ' (?:' + k + '|command)\\b')); }); });