Skip to content

feat(testing): use zones to avoid the need for injectAsync - #5375

Closed
juliemr wants to merge 1 commit into
angular:masterfrom
juliemr:zones-for-tests
Closed

feat(testing): use zones to avoid the need for injectAsync#5375
juliemr wants to merge 1 commit into
angular:masterfrom
juliemr:zones-for-tests

Conversation

@juliemr

Copy link
Copy Markdown
Member

Use a zone counting timeouts and microtasks to determine when a test
is finished, instead of requiring the test writer to use
injectAsync and return a promise.

See #5322

@juliemrjuliemr added the area: testing Issues related to Angular testing features, such as TestBed label Nov 19, 2015
@juliemr

Copy link
Copy Markdown
MemberAuthor

cc @vicb@IgorMinar

Zones seem to work smoothly and remove complexity for the user for the async tests. All tests which use inject are now being run asynchronously, which as we discussed before adds a very small (couple ms) penalty time to each test. I like this overall - thoughts?

@juliemrjuliemr added the action: review The PR is still awaiting reviews from at least one requested reviewer label Nov 19, 2015
@juliemrjuliemr added this to the beta-00 milestone Nov 19, 2015
@juliemr

Copy link
Copy Markdown
MemberAuthor

Note that this isn't actually a breaking change, because both inject and injectAsync do the same thing now and the return value doesn't matter. A cleanup would remove injectAsync entirely.

@0x-r4bbit

Copy link
Copy Markdown
Contributor

This is awesome Julie!

On Thu, Nov 19, 2015, 3:52 AM Julie Ralph notifications@github.com wrote:

Note that this isn't actually a breaking change, because both inject and
injectAsync do the same thing now and the return value doesn't matter. A
cleanup would remove injectAsync entirely.


Reply to this email directly or view it on GitHub
#5375 (comment).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

<Zone>global.zone

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Good catch, done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

typo: asynchronous

@vicb

vicb commented Nov 19, 2015

Copy link
Copy Markdown
Contributor

A few minor comments, otherwise LGTM.
Great PR !

@juliemr
juliemrforce-pushed the zones-for-tests branch 2 times, most recently from 53f6396 to d3f814cCompareNovember 19, 2015 18:58
@IgorMinar

Copy link
Copy Markdown
Contributor

@wardbell fyi - this will affect testing docs

@IgorMinar

Copy link
Copy Markdown
Contributor

looks great! Please deprecate injectAsync and add docs about the async behavior for inject

@IgorMinarIgorMinar added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews pr_state: LGTM and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Nov 19, 2015
@IgorMinarIgorMinar assigned juliemr and unassigned IgorMinarNov 19, 2015
@IgorMinar

Copy link
Copy Markdown
Contributor

OMG, I just realized that this might mean that we don't need to refactor all of our tests in angular2/angular2 to return promises. Now I'm really stoked.

Do we properly detect errors and surface them or will we still see jasmine timeouts when an error occurs in an async test?

@juliemr

Copy link
Copy Markdown
MemberAuthor

Errors should be properly output: As in this test:

it('should fail when an asynchronous error is thrown',(done)=>{varitPromise=patchJasmineIt();it('throws an async error',inject([],()=>{setTimeout(()=>{thrownewError('bar');},0);}));itPromise.then(()=>{done.fail('Expected function to throw, but it did not');},(err)=>{expect(err.message).toEqual('bar');// <-- bar not just timeout!done();});restoreJasmineIt();});

@wardbell

Copy link
Copy Markdown
Contributor

Big win! Looking forward to trying this.

@juliemr

Copy link
Copy Markdown
MemberAuthor

Deprecation notice and docs are done.

@juliemrjuliemr added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Nov 19, 2015
@vsavkin

Copy link
Copy Markdown
Contributor

Could you rebase?

@vsavkinvsavkin added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: merge The PR is ready for merge by the caretaker labels Nov 25, 2015
Use a zone counting timeouts and microtasks to determine when a test
is finished, instead of requiring the test writer to use
injectAsync and return a promise.
See angular#5322
@juliemr

Copy link
Copy Markdown
MemberAuthor

@vsavkin done!

@juliemrjuliemr removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Nov 25, 2015
@IgorMinar

Copy link
Copy Markdown
Contributor

let's get this in! :)

@juliemrjuliemr assigned vsavkin and unassigned alxhubNov 25, 2015
@vsavkinvsavkin added action: merge The PR is ready for merge by the caretaker zomg_admin: do merge and removed zomg_admin: do merge labels Nov 30, 2015
@vsavkin

Copy link
Copy Markdown
Contributor

Merged via 0c9596a

@vsavkinvsavkin closed this Nov 30, 2015
juliemr added a commit to juliemr/angular that referenced this pull request Dec 15, 2015
Before angular#5375, injectAsync would check the return value and fail
if it was not a promise, to help users remember that they need to
return a promise from an async test. angular#5375 removed that with the
introduction of the testing zone.
This un-deprecates `injectAsync` until we can resolveangular#5515.
To be clear, this means that `inject` and `injectAsync` are now
identical except that `injectAsync` will fail if the test
does not return a promise, and `inject` will fail if the test
returns any value.
juliemr added a commit that referenced this pull request Dec 15, 2015
Before #5375, injectAsync would check the return value and fail
if it was not a promise, to help users remember that they need to
return a promise from an async test. #5375 removed that with the
introduction of the testing zone.
This un-deprecates `injectAsync` until we can resolve#5515.
To be clear, this means that `inject` and `injectAsync` are now
identical except that `injectAsync` will fail if the test
does not return a promise, and `inject` will fail if the test
returns any value.
Closes#5721
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-botangular-automatic-lock-botBot locked and limited conversation to collaborators Sep 7, 2019
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: mergeThe PR is ready for merge by the caretakerarea: testingIssues related to Angular testing features, such as TestBedcla: yes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@juliemr@0x-r4bbit@vicb@IgorMinar@wardbell@Foxandxss@alxhub@vsavkin@ajoslin@googlebot