ember-test-helpers icon indicating copy to clipboard operation
ember-test-helpers copied to clipboard

Timecop is freezing tests

Open Turbo87 opened this issue 7 years ago • 13 comments

We are using https://github.com/matteodepalo/ember-cli-timecop in a few component integration tests and until now this worked fine. We tried to convert those tests to use the new testing APIs, but now Timecop.freeze() seems to block the async render(...) call from continuing.

An issue similar to this has existed in the past where Backburner.js would not continue running when Date.now() is mocked and frozen, but was fixed by getting a reference to the Date object on startup and then using that reference instead of the temporary mocked global.

Turbo87 avatar Nov 15 '17 12:11 Turbo87

Since https://github.com/emberjs/ember-test-helpers/pull/239 was not merged yet it seems that we are not actually using setTimeout() at all yet, so it must something else using it 🤔

Turbo87 avatar Nov 15 '17 12:11 Turbo87

@Turbo87 - Well, Ember.run.next ultimately does use setTimeout internally (and we are using run.next now).

rwjblue avatar Nov 15 '17 14:11 rwjblue

If you have the time, could you put together a repo I could use for testing / experimenting with this?

rwjblue avatar Nov 15 '17 14:11 rwjblue

@rwjblue https://github.com/Turbo87/__timecop-issue

Turbo87 avatar Nov 15 '17 17:11 Turbo87

Interestingly https://github.com/jamesarosen/Timecop.js/issues/24#issuecomment-35451318 notes that:

Timecop doesn't modify the global setTimeout and setInterval implementations.

I updated the issue description to reflect this.

I'm wondering what is causing these freezes then 🤔

Turbo87 avatar Dec 12 '17 14:12 Turbo87

The issue is exactly what you mentioned in the initial description. https://github.com/BackburnerJS/backburner.js/commit/a6fe19eb26c2b7ed8360de19ca0cc9ae5b05f297 changed Date.now to no longer be eagerly captured. The Ember.run.next's that we are calling (in setupContext and setupRenderingContext) create a scenario where settled() would never resolve (since it internally used Date.now() to determine if it should run our scheduled callback).

However, I also tested this with the changes from https://github.com/emberjs/ember-test-helpers/pull/239 and it does pass. I suspect that any "real world" scenario would still fail though. Essentially any usage of run.next or run.later in an app would cause this specific regression...

rwjblue avatar Dec 12 '17 18:12 rwjblue

How did this work before?

Turbo87 avatar Dec 12 '17 18:12 Turbo87

Oh, I understand... https://github.com/BackburnerJS/backburner.js/pull/264 🤔

Turbo87 avatar Dec 12 '17 18:12 Turbo87

Well, even with the same Ember version if you used the older style ember-qunit setup (e.g. moduleForComponent) nothing actually sets up a run.next / run.later so we just don't fall into this trap. tl;dr its a dozy 😭

rwjblue avatar Dec 12 '17 21:12 rwjblue

Hmm, @Turbo87 I vaguely think that we discussed this in slack and you mentioned it was no longer an issue (or at least you had worked around it)?

rwjblue avatar Feb 11 '18 15:02 rwjblue

I'm not sure, but since https://github.com/emberjs/ember-test-helpers/issues/245#issuecomment-344658866 has a reproduction it should be relatively easy to verify whether this works now or not. In our real app I think we're using TimeCop.travel() now instead of .freeze() 🤔

Turbo87 avatar Feb 11 '18 15:02 Turbo87

kk, I'll try to pull it down and test...

rwjblue avatar Feb 11 '18 16:02 rwjblue

We had/have a similar issue over at ember-mockdate-shim https://github.com/Ticketfly/ember-mockdate-shim/issues/4

kiwi-josh avatar May 04 '18 05:05 kiwi-josh