Titanium JIRA Archive
Appcelerator Community (AC)

[AC-3003] global `setTimeout()` short-circuits the target object

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionInvalid
Resolution Date2012-05-29T22:34:17.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelssettimeout, testing
ReporterKenneth Kan
AssigneeBetty Tran
Created2012-05-25T15:52:27.000+0000
Updated2016-03-08T07:48:01.000+0000

Description

setTimeout seems to break method replacements by spies. The this in object a does not refer to the same c as replaced by the spy. Tried this on SDK 1.7.0, 1.8.2, and 2.0.1 with Sinon.js and Jasmine testing frameworks. {noformat} a = { b: function() { Ti.API.debug("a.b() called"); this.c(); }, c: function() { Ti.API.debug("a.c() called"); } }; describe("some test suite", function(){ it("some test case", function(){ setTimeout(function(){ a.b(); }, 1000); // or setTimeout(function(){ eval("a.b();"); }, 1000); spyOn(a, "b"); spyOn(a, "c"); // or sinon.spy(a, "b"); // sinon.spy(a, "c"); // After 1 second... // "[DEBUG] a.b() called" is printed to terminal // "[DEBUG] a.c() called" is printed to terminal expect(a.b.callCount).toEqual(0); // succeed expect(a.b).toHaveBeenCalled(); // fail expect(a.c.callCount).toEqual(0); // succeed expect(a.c).toHaveBeenCalled(); // fail }); }); {noformat}

Comments

  1. Kenneth Kan 2012-05-29

    My apology. This is actually valid behavior. Because I used jasmine-titanium the test suite output is on the simulator and I made incorrect assumption. After having attached timestamp to the output. The order is correct. Someone with the authority to close this ticket please do so. Thank you.
  2. Betty Tran 2012-05-29

    Ticket closed as this is valid behavior.
  3. Shak Hossain 2013-12-27

    Closing as per Kenneth's comment.

JSON Source