Skip to content

Commit

Permalink
test(unit): remove the global "timer"
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Aug 21, 2013
1 parent b6d7127 commit 72da3fb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
16 changes: 6 additions & 10 deletions test/unit/browser.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe 'browser', ->
b = require '../../lib/browser'
e = require '../../lib/events'

Timer = require('timer-shim').Timer
createMockTimer = require './mocks/timer'

beforeEach -> sinon.stub(Date, 'now')
afterEach -> Date.now.restore()
Expand Down Expand Up @@ -207,8 +207,7 @@ describe 'browser', ->
timer = null

beforeEach ->
timer = new Timer
timer.pause()
timer = createMockTimer()
browser = new b.Browser 'fake-id', 'full name', collection, emitter, socket, timer, 10
browser.init()

Expand Down Expand Up @@ -247,8 +246,7 @@ describe 'browser', ->
describe 'onReconnect', ->

it 'should cancel disconnecting', ->
timer = new Timer
timer.pause()
timer = createMockTimer()

browser = new b.Browser 'id', 'Chrome 19.0', collection, emitter, socket, timer, 10
browser.init()
Expand All @@ -264,7 +262,7 @@ describe 'browser', ->
it 'should ignore disconnects on old sockets, but accept other messages', ->
# IE on polling sometimes reconnect on another socket (before disconnecting)

browser = new b.Browser 'id', 'Chrome 19.0', collection, emitter, socket, timer, 10
browser = new b.Browser 'id', 'Chrome 19.0', collection, emitter, socket, null, 0
browser.init()
browser.state = b.Browser.STATE_EXECUTING

Expand Down Expand Up @@ -351,8 +349,7 @@ describe 'browser', ->
describe 'scenario:', ->

it 'reconnecting during the run', ->
timer = new Timer
timer.pause()
timer = createMockTimer()
browser = new b.Browser 'fake-id', 'full name', collection, emitter, socket, timer, 10
browser.init()
browser.state = b.Browser.STATE_EXECUTING
Expand All @@ -374,8 +371,7 @@ describe 'browser', ->
it 'disconecting during the run', ->
spy = sinon.spy()
emitter.on 'browser_complete', spy
timer = new Timer
timer.pause()
timer = createMockTimer()
browser = new b.Browser 'fake-id', 'full name', collection, emitter, socket, timer, 10
browser.init()
browser.state = b.Browser.STATE_EXECUTING
Expand Down
15 changes: 8 additions & 7 deletions test/unit/launchers/Base.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe 'launchers Base', ->
fsMock = nodeMocks.fs
path = require 'path'

fakeTimer = null
setTimeoutMock = null

mockSpawn = sinon.spy (cmd, args) ->
process = new events.EventEmitter
Expand Down Expand Up @@ -36,13 +36,14 @@ describe 'launchers Base', ->
env:
TMP: '/tmp'
nextTick: process.nextTick
setTimeout: timer.setTimeout
setTimeout: (fn, delay) -> setTimeoutMock fn, delay


m = loadFile __dirname + '/../../../lib/launchers/Base.js', mocks, globals


beforeEach ->
setTimeoutMock = sinon.stub().callsArg 0
mockSpawn.reset()
mockSpawn._processes = []
mockRimraf.reset()
Expand All @@ -63,7 +64,7 @@ describe 'launchers Base', ->
sinon.stub browser, '_onTimeout'

browser.start '/some'
expect(timer.setTimeout).not.to.have.been.called
expect(setTimeoutMock).not.to.have.been.called
expect(browser._onTimeout).not.to.have.been.called


Expand Down Expand Up @@ -175,7 +176,7 @@ describe 'launchers Base', ->
browserProcess = mockSpawn._processes.shift()

# timeout
expect(timer.setTimeout).to.have.been.called
expect(setTimeoutMock).to.have.been.called

# expect killing browser
expect(browserProcess.kill).to.have.been.called
Expand Down Expand Up @@ -205,7 +206,7 @@ describe 'launchers Base', ->
browserProcess = mockSpawn._processes.shift()

# timeout
expect(timer.setTimeout).to.have.been.called
expect(setTimeoutMock).to.have.been.called

# expect killing browser
expect(browserProcess.kill).to.have.been.called
Expand All @@ -220,7 +221,7 @@ describe 'launchers Base', ->
browserProcess = mockSpawn._processes.shift()

# timeout
expect(timer.setTimeout).to.have.been.called
expect(setTimeoutMock).to.have.been.called

# expect killing browser
expect(browserProcess.kill).to.have.been.called
Expand All @@ -238,7 +239,7 @@ describe 'launchers Base', ->
browserProcess = mockSpawn._processes.shift()

# timeout
expect(timer.setTimeout).to.have.been.called
expect(setTimeoutMock).to.have.been.called

# expect killing browser
expect(browserProcess.kill).to.have.been.called
Expand Down
4 changes: 0 additions & 4 deletions test/unit/mocha-globals.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ chai = require 'chai'
logger = require '../../lib/logger'

# publish globals that all specs can use
global.timer = require 'timer-shim'
global.expect = chai.expect
global.should = chai.should()
global.sinon = sinon
Expand All @@ -12,9 +11,6 @@ global.sinon = sinon
chai.use(require 'chai-as-promised')
chai.use(require 'sinon-chai')

# TODO(vojta): remove this global stub
sinon.stub(timer, 'setTimeout').callsArg 0

beforeEach ->
global.sinon = sinon.sandbox.create()

Expand Down
15 changes: 15 additions & 0 deletions test/unit/mocks/timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Timer = require('timer-shim').Timer;

module.exports = function() {
var timer = new Timer();
timer.pause();

return {
setTimeout: timer.setTimeout,
clearTimeout: timer.clearTimeout,
setInterval: timer.setInterval,
clearInterval: timer.clearInterval,
wind: timer.wind
};
};

0 comments on commit 72da3fb

Please sign in to comment.