Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let Jasmine handle async test results #619

Merged
merged 2 commits into from
Jun 22, 2018

Conversation

raphinesse
Copy link
Contributor

There was a lot of sub-optimal error handling in the tests.

Instead of treating every promise like the unique little sparkly star that it is, we just return them from the test functions and let Jasmine handle them.

This also fixes tons of negative tests that often looked something like this:

sparkleSparkle('NOT!').then(function () {
    expect('Roses').toBe('Blue');
}).catch(function (err) {
    expect('' + err).toMatch('not so sparkly after all');
});

These were normalized to something like this (spot all the differences! 😉):

sparkleSparkle('NOT!').then(function () {
    fail('Expected promise to be rejected');
}, function (err) {
    expect(err).toEqual(jasmine.any(Error));
    expect(err.message).toMatch('not so sparkly after all');
});

This commit has multiple benefits

  • Removes 700 LOC w/out removing any testing
  • Improves output when async tests fail
  • Should make async tests less error prone
  • Reduces Q-API usage to prepare for removal of Q (CB-14159)

I really don't expect anyone to review this ~2k LOC change set. Since it only affects the tests and they still pass I would merge this without a review if no one objects.

There was a lot of sub-optimal error handling in the tests.

This commit has multiple benefits
- Removes 700 LOC w/out removing any testing
- Improves output when async tests fail
- Should make async tests less error prone
- Reduces Q-API usage to prepare for removal of Q (CB-14159)
@@ -31,6 +31,8 @@ var TIMEOUT = 240 * 1000;
* should initially be in pkg.json and/or config.xml.
*/

/* eslint "promise/catch-or-return": "error" */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why put eslint "promise/catch-or-return": "error" here? Shouldn't it be in a .eslint config file (doesn't have to be top-level)?

Copy link
Contributor Author

@raphinesse raphinesse Jun 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an oversight. Thanks, will remove! I used that during the refactor to spot places where a Promise needed to be returned.

@brody4hire
Copy link

+1 from a quick scan 🎉

@raphinesse
Copy link
Contributor Author

All right, gonna merge this...

@raphinesse raphinesse merged commit da4b4b3 into apache:master Jun 22, 2018
@raphinesse
Copy link
Contributor Author

@brodybits I haven't actively looked into reasons for CI failures. But if anything, this should help make the tests more robust, yes.

Having skimmed through almost all of the tests now, I have to say there's some horrible code in there and setup/teardown of test dirs is often pretty crude too. So I rather expect that there are still tests that are note stable. #613 will hopefully further improve the situation.

@raphinesse raphinesse deleted the async-tests branch June 22, 2018 13:49
@raphinesse
Copy link
Contributor Author

raphinesse commented Jun 22, 2018

@brodybits wrote: Nice work [...] wonder if we should apply similar updates to some of the other Cordova repos?

Definitely. I've done that for one or two already (create/fetch I guess). Should be easier for everything other than lib. Maybe an issue with sub-tasks would be in order.

However, I mainly started this to reduce Q-usage. Basically we need to remove usage of Q instance methods in depending packages to be able to return native Promises in the dependencies. Thus, plugman and cordova-cli would be next on my priority list.

@brody4hire
Copy link

Should be easier for everything other than lib.

That would definitely be good!

Maybe an issue with sub-tasks would be in order.

I am starting to agree with this. JIRA issues are really good for logging & tracking changes, but if I label PR discussions for multiple components under the same JIRA issue it becomes cluttered pretty quickly.

reduce Q-usage.

💯 I was already thinking to raise an issue to reduce or hopefully eliminate Q usage, guess I should leave it in your hands for now.

@raphinesse wrote in #613 (comment): I'm still working on other cordova stuff [...]

Yup, understood. I guess we are all in the same boat:)

@raphinesse
Copy link
Contributor Author

I was already thinking to raise an issue to reduce or hopefully eliminate Q usage

https://issues.apache.org/jira/browse/CB-14158

@raphinesse raphinesse mentioned this pull request Aug 15, 2018
7 tasks
brody4hire pushed a commit to brody4hire/cordova-lib that referenced this pull request Sep 13, 2018
There was a lot of sub-optimal error handling in the tests.

Instead of handling Promise rejection and resolution ourselves, we now
just return them from the test functions and let Jasmine handle them.

Moreover, this also fixes tons of negative tests that looked something like this:

    f().then(function () {
        expect('foo').toBe('bar');
    }).catch(function (err) {
        expect('' + err).toMatch('some message');
    });

Problems being:
- `expect('foo').toBe('bar')` is less obvious than `fail(...)`
- catches its own Error from the `.then` only to fail because `err` is missing
- Does not check that err is an Error and that the message is in `.message`

So these were normalized to look like this:

    f().then(function () {
        fail('Expected promise to be rejected');
    }).catch(function (err) {
        expect(err).toEqual(jasmine.any(Error));
        expect(err.message).toMatch('some message');
    });

All in all, this commit has the following benefits:
- Removes 700 LOC w/out removing any testing
- Improves output when async tests fail
- Should make async tests less error prone
- Reduces Q-API usage to prepare for removal of Q (CB-14159)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants