Skip to content

Instantly share code, notes, and snippets.

@mainiak
Last active December 10, 2015 00:59
Show Gist options
  • Save mainiak/4355050 to your computer and use it in GitHub Desktop.
Save mainiak/4355050 to your computer and use it in GitHub Desktop.
testing time bending in browser - see http://tryjasmine.com - an nodejs
diff --git a/Task.coffee b/Task.coffee
index b237031..7edc852 100644
--- a/Task.coffee
+++ b/Task.coffee
@@ -3,7 +3,7 @@ class Task
constructor: (@text) ->
@intervalId = null
- hello: -> window.alert @text
+ hello: -> console.log @text
delay: 1000 # 1 sec
@@ -20,3 +20,5 @@ class Task
if @intervalId isnt null
clearInterval @intervalId
@intervalId = null
+
+exports.Task = Task
diff --git a/TaskSpec.coffee b/TaskSpec.coffee
index 4e9477a..723b0db 100644
--- a/TaskSpec.coffee
+++ b/TaskSpec.coffee
@@ -1,7 +1,9 @@
+{ Task } = require './Task'
+
describe 'Task', ->
it 'class is defined', ->
- expect(window.alert).toBeDefined()
+ expect(console.log).toBeDefined()
expect(Task).toBeDefined()
expect(Task::hello).toBeDefined()
expect(Task::timeout).toBeDefined()
@@ -16,7 +18,7 @@ describe 'Task', ->
beforeEach ->
task = new Task content
- spyOn(window,'alert') #.andCallThrough()
+ spyOn(console,'log') #.andCallThrough()
it 'is defined', ->
expect(task).toBeDefined()
@@ -25,22 +27,22 @@ describe 'Task', ->
it 'hello should work immediately', ->
task.hello()
- expect(window.alert).toHaveBeenCalledWith(content)
- expect(window.alert.callCount).toBe(1)
+ expect(console.log).toHaveBeenCalledWith(content)
+ expect(console.log.callCount).toBe(1)
## it's too slow for jasmine - will fail (expected)
xit 'should popup in 1 sec (without mock)', ->
task.timeout()
- expect(window.alert).toHaveBeenCalledWith(content)
- expect(window.alert.callCount).toBe(1)
+ expect(console.log).toHaveBeenCalledWith(content)
+ expect(console.log.callCount).toBe(1)
## mock time to make test faster
it 'should popup in 1 sec (mocked)', ->
jasmine.Clock.useMock()
task.timeout()
jasmine.Clock.tick(Task::delay)
- expect(window.alert).toHaveBeenCalledWith(content)
- expect(window.alert.callCount).toBe(1)
+ expect(console.log).toHaveBeenCalledWith(content)
+ expect(console.log.callCount).toBe(1)
it 'ticks', ->
jasmine.Clock.useMock()
@@ -53,5 +55,5 @@ describe 'Task', ->
## this shouldn't produce anything after task.stop()
jasmine.Clock.tick(Task::delay)
- expect(window.alert).toHaveBeenCalledWith(content)
- expect(window.alert.callCount).toBe(10)
+ expect(console.log).toHaveBeenCalledWith(content)
+ expect(console.log.callCount).toBe(10)
class Task
constructor: (@text) ->
@intervalId = null
hello: -> window.alert @text
delay: 1000 # 1 sec
timeout: ->
callBack = => @hello()
setTimeout callBack, @delay
## for one 1 sec this should produce 10 calls
interval: ->
callBack = => @hello()
@intervalId = setInterval callBack, (@delay/10)
stop: ->
if @intervalId isnt null
clearInterval @intervalId
@intervalId = null
describe 'Task', ->
it 'class is defined', ->
expect(window.alert).toBeDefined()
expect(Task).toBeDefined()
expect(Task::hello).toBeDefined()
expect(Task::timeout).toBeDefined()
expect(Task::interval).toBeDefined()
expect(Task::stop).toBeDefined()
expect(Task::delay).toBe(1000)
describe 'instance', ->
content = 'dummy text'
task = null
beforeEach ->
task = new Task content
spyOn(window,'alert') #.andCallThrough()
it 'is defined', ->
expect(task).toBeDefined()
expect(task.hello).toBeDefined()
expect(task.timeout).toBeDefined()
it 'hello should work immediately', ->
task.hello()
expect(window.alert).toHaveBeenCalledWith(content)
expect(window.alert.callCount).toBe(1)
## it's too slow for jasmine - will fail (expected)
xit 'should popup in 1 sec (without mock)', ->
task.timeout()
expect(window.alert).toHaveBeenCalledWith(content)
expect(window.alert.callCount).toBe(1)
## mock time to make test faster
it 'should popup in 1 sec (mocked)', ->
jasmine.Clock.useMock()
task.timeout()
jasmine.Clock.tick(Task::delay)
expect(window.alert).toHaveBeenCalledWith(content)
expect(window.alert.callCount).toBe(1)
it 'ticks', ->
jasmine.Clock.useMock()
task.interval()
## produce 10 ticks
jasmine.Clock.tick(Task::delay)
task.stop()
## this shouldn't produce anything after task.stop()
jasmine.Clock.tick(Task::delay)
expect(window.alert).toHaveBeenCalledWith(content)
expect(window.alert.callCount).toBe(10)
#!/bin/sh
# npm install -g jasmine-node
# git clone https://gist.github.com/4355050.git && cd 4355050 && patch -i nodejs.patch && \
jasmine-node --coffee --verbose .
@mainiak
Copy link
Author

mainiak commented Jan 2, 2013

Known issue with jasmine.Clock - mhevery/jasmine-node#171

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment