Closed Bug 998257 Opened 11 years ago Closed 10 years ago

[Messages][Refactoring] Investigate possible ways of making unit tests for promise-based code more simple and useful

Categories

(Firefox OS Graveyard :: Gaia::SMS, defect)

ARM
Gonk (Firefox OS)
defect
Not set
minor

Tracking

(Not tracked)

RESOLVED WORKSFORME

People

(Reporter: azasypkin, Unassigned)

References

Details

Attachments

(1 file)

We have several potential problems with unit tests for promise-based code that can lead to increase in time spent on writing of unit tests and\or making a bunch of code just for the sake of simple unit tests: Problem #1: Currently we can unit-test only API that exposes promise publicly, so that we can use 'then\catch' methods to catch the moment when promise is actually resolved. That won't always be true when we use promises more intensively. Example (call1 and call2 can be private\non-spyable): var publicFuncThatNeedToBeTested = function() { // make first async call that returns promise call1().then(function() { // change app state that should be verified by test }); // second async call that doesn't depend on call1 and can be issued in parallel return call2().then(function() { // change app state that also should be verified by test }); }; Solution idea: use 'Promise.flush.then(make_assertions) to resolve\reject all pending promises before making assertions. Problem #2: We'll probably want to use promises for test purposes, that may be dependent on each other. With just using 'then\catch' we may end up with too nested and unmaintainable unit tests: test(...., function(done) { click1(); waitForClick1ResultToAppear().then(function() { click2(); return waitForClick2ResultToAppear(); }).then(function() { click3(); ...... }).then(done, done) }); Solution idea: use 'yield' and later 'await' to make tests flatter: test(...., function() { click1(); yield waitForClick1ResultToAppear(); click2(); yield waitForClick2ResultToAppear(); click3(); }); Also we need to update Chai and Mocha libs to make it more Promise friendly (that is supposed to be done in the bug 991663)
Using MockPromise and Generators to flatten asynchronous unit tests.
Oleg, do you think bug 1094850 introducing chai as promised is good enough for you? See https://github.com/domenic/chai-as-promised/ for more information.
Flags: needinfo?(azasypkin)
(In reply to Julien Wajsberg [:julienw] from comment #2) > Oleg, do you think bug 1094850 introducing chai as promised is good enough > for you? See https://github.com/domenic/chai-as-promised/ for more > information. Yeah, I think it improves current situation a lot, so that there is no critical need in more custom code :) Thanks for working on this!
Status: NEW → RESOLVED
Closed: 10 years ago
Flags: needinfo?(azasypkin)
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: