Skip to content
Permalink
Browse files
test: use expectAsync for rejections (#831)
  • Loading branch information
raphinesse committed Jan 12, 2020
1 parent 214dca1 commit f7f9343ca9b3c616570b5d2d0a00f2e39e0b1424
Showing 19 changed files with 213 additions and 304 deletions.
@@ -164,11 +164,9 @@ describe('HooksRunner', function () {
`;
addHooksToConfig(FAIL_HOOK);

return hooksRunner.fire('fail').then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
});
return expectAsync(
hooksRunner.fire('fail')
).toBeRejectedWithError();
});

it('Test 024 : should not error if the hook is unrecognized', function () {
@@ -115,13 +115,9 @@ describe('fetch', function () {
});
it('Test 028 : should fail when locally-available plugin is missing pacakge.json', function () {
test_plugin = path.join(plugins_dir, 'org.test.androidonly');
return fetch(test_plugin, temp)
.then(function () {
fail();
}, function (err) {
expect(err).toBeDefined();
expect(err.message).toContain('needs a valid package.json');
});
return expectAsync(
fetch(test_plugin, temp)
).toBeRejectedWithError(/needs a valid package\.json/);
});
});

@@ -154,21 +154,17 @@ describe('plugman/uninstall', () => {

describe('failure ', function () {
it('Test 004 : should throw if platform is unrecognized', function () {
return uninstall.uninstallPlatform('atari', project, 'SomePlugin')
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toContain('Platform "atari" not supported.');
});
return expectAsync(
uninstall.uninstallPlatform('atari', project, 'SomePlugin')
).toBeRejectedWithError('Platform "atari" not supported.');
});

it('Test 005 : should throw if plugin is missing', function () {
return uninstall.uninstallPlatform('android', project, 'SomePluginThatDoesntExist')
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toContain('Plugin "SomePluginThatDoesntExist" not found. Already uninstalled?');
});
return expectAsync(
uninstall.uninstallPlatform('android', project, 'SomePluginThatDoesntExist')
).toBeRejectedWithError(
'Plugin "SomePluginThatDoesntExist" not found. Already uninstalled?'
);
});
});
});
@@ -192,12 +188,11 @@ describe('plugman/uninstall', () => {
it('Test 007 : should fail if plugin is a required dependency', function () {
setupProject('uninstall.test');

return uninstall.uninstallPlugin('C', plugins_install_dir)
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toEqual('Plugin "C" is required by (A) and cannot be removed (hint: use -f or --force)');
});
return expectAsync(
uninstall.uninstallPlugin('C', plugins_install_dir)
).toBeRejectedWithError(
'Plugin "C" is required by (A) and cannot be removed (hint: use -f or --force)'
);
});

it('Test 008 : allow forcefully removing a plugin', function () {
@@ -245,21 +240,17 @@ describe('plugman/uninstall', () => {

describe('failure', function () {
it('Test 011 : should throw if platform is unrecognized', function () {
return uninstall('atari', project, 'SomePlugin')
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toContain('Platform "atari" not supported.');
});
return expectAsync(
uninstall('atari', project, 'SomePlugin')
).toBeRejectedWithError('Platform "atari" not supported.');
});

it('Test 012 : should throw if plugin is missing', function () {
return uninstall('android', project, 'SomePluginThatDoesntExist')
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toContain('Plugin "SomePluginThatDoesntExist" not found. Already uninstalled?');
});
return expectAsync(
uninstall('android', project, 'SomePluginThatDoesntExist')
).toBeRejectedWithError(
'Plugin "SomePluginThatDoesntExist" not found. Already uninstalled?'
);
});
});
});
@@ -39,29 +39,21 @@ describe('build command', function () {
describe('failure', function () {
it('Test 001 : should not run inside a project with no platforms', function () {
util.listPlatforms.and.returnValue([]);
return cordovaBuild()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toEqual(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});
return expectAsync(
cordovaBuild()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});

it('Test 002 : should not run outside of a Cordova-based project', function () {
util.isCordova.and.returnValue(false);

return cordovaBuild()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toEqual(
'Current working directory is not a Cordova-based project.'
);
});
return expectAsync(
cordovaBuild()
).toBeRejectedWithError(
'Current working directory is not a Cordova-based project.'
);
});
});

@@ -101,14 +93,12 @@ describe('build command', function () {
describe('with no platforms added', function () {
it('Test 008 : should not fire the hooker', function () {
util.listPlatforms.and.returnValue([]);
return Promise.resolve().then(cordovaBuild).then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toEqual(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});

return expectAsync(
cordovaBuild()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});
});
});
@@ -42,22 +42,17 @@ describe('compile command', function () {
describe('failure', function () {
it('Test 001 : should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function () {
list_platforms.and.returnValue([]);
return cordova.compile()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toContain('No platforms added to this project. Please use `cordova platform add <platform>`.');
});
return expectAsync(
cordova.compile()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});
it('Test 002 : should not run outside of a Cordova-based project', function () {
is_cordova.and.returnValue(false);
return cordova.compile()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
});
return expectAsync(
cordova.compile()
).toBeRejectedWithError();
});
});

@@ -97,18 +92,16 @@ describe('compile command', function () {
});

describe('with no platforms added', function () {
it('Test 008 : should not fire the hooker', function () {
it('Test 008 : should not fire the hooker', async function () {
list_platforms.and.returnValue([]);
return Promise.resolve().then(cordova.compile)
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(fire).not.toHaveBeenCalled();
expect(err.message).toContain(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});

await expectAsync(
cordova.compile()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);

expect(fire).not.toHaveBeenCalled();
});
});
});
@@ -46,22 +46,17 @@ describe('emulate command', function () {
describe('failure', function () {
it('Test 001 : should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function () {
util.listPlatforms.and.returnValue([]);
return cordovaEmulate()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toContain('No platforms added to this project. Please use `cordova platform add <platform>`.');
});
return expectAsync(
cordovaEmulate()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});
it('Test 002 : should not run outside of a Cordova-based project', function () {
util.isCordova.and.returnValue(false);
return cordovaEmulate()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
});
return expectAsync(
cordovaEmulate()
).toBeRejectedWithError();
});
});

@@ -144,15 +139,16 @@ describe('emulate command', function () {
});

describe('with no platforms added', function () {
it('Test 011 : should not fire the hooker', function () {
it('Test 011 : should not fire the hooker', async function () {
util.listPlatforms.and.returnValue([]);
return Promise.resolve().then(cordovaEmulate).then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toContain('No platforms added to this project. Please use `cordova platform add <platform>`.');
expect(HooksRunner.prototype.fire).not.toHaveBeenCalled();
});

await expectAsync(
cordovaEmulate()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);

expect(HooksRunner.prototype.fire).not.toHaveBeenCalled();
});
});
});
@@ -110,19 +110,17 @@ describe('cordova/platform/addHelper', function () {

it('should throw if platform was already added before adding', function () {
fs.existsSync.and.returnValue('/some/path/platforms/ios');
return platform_addHelper('add', hooks_mock, projectRoot, ['ios']).then(function () {
fail('addHelper success handler unexpectedly invoked');
}, function (e) {
expect(e.message).toContain('already added.');
});
return expectAsync(
platform_addHelper('add', hooks_mock, projectRoot, ['ios'])
).toBeRejectedWithError(/already added\./);
});

it('should throw if platform was not added before updating', function () {
return platform_addHelper('update', hooks_mock, projectRoot, ['atari']).then(function () {
fail('addHelper success handler unexpectedly invoked');
}, function (e) {
expect(e.message).toContain('Platform "atari" is not yet added. See `cordova platform list`.');
});
return expectAsync(
platform_addHelper('update', hooks_mock, projectRoot, ['atari'])
).toBeRejectedWithError(
'Platform "atari" is not yet added. See `cordova platform list`.'
);
});
});
describe('happy path (success conditions)', function () {
@@ -272,11 +270,11 @@ describe('cordova/platform/addHelper', function () {
describe('errors', function () {
it('should reject the promise should fetch fail', function () {
fetch_mock.and.returnValue(Promise.reject(new Error('fetch has failed, rejecting promise')));
return platform_addHelper.downloadPlatform(projectRoot, 'android', '67').then(function () {
fail('success handler unexpectedly invoked');
}, function (e) {
expect(e.message).toContain('fetch has failed, rejecting promise');
});
return expectAsync(
platform_addHelper.downloadPlatform(projectRoot, 'android', '67')
).toBeRejectedWithError(
/fetch has failed, rejecting promise/
);
});
});
describe('happy path', function () {
@@ -35,22 +35,20 @@ describe('cordova/platform/getPlatformDetailsFromDir', function () {
});

it('should throw if no config.xml or pkgJson', function () {
return platform_getPlatformDetails('dir', ['ios'])
.then(function () {
fail('Expected promise to be rejected');
}, function (reason) {
expect(reason).toMatch(/does not seem to contain a valid package.json or a valid Cordova platform/);
});
return expectAsync(
platform_getPlatformDetails('dir', ['ios'])
).toBeRejectedWithError(
/does not seem to contain a valid package.json or a valid Cordova platform/
);
});

it('should throw if no platform is provided', function () {
cordova_util.requireNoCache.and.returnValue({});
return platform_getPlatformDetails('dir')
.then(function () {
fail('Expected promise to be rejected');
}, function (reason) {
expect(reason).toMatch(/does not seem to contain a Cordova platform:/);
});
return expectAsync(
platform_getPlatformDetails('dir')
).toBeRejectedWithError(
/does not seem to contain a Cordova platform:/
);
});

it('should return a promise with platform and version', function () {

0 comments on commit f7f9343

Please sign in to comment.