Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hrtime to be globally patched #21

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "performance-now",
"name": "@yr/performance-now",
"description": "Implements performance.now (based on process.hrtime).",
"keywords": [],
"version": "2.1.0",
"version": "1.0.0",
"author": "Braveg1rl <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/braveg1rl/performance-now",
Expand All @@ -21,6 +21,8 @@
"coffee-script": "~1.12.2",
"mocha": "~3.2.0",
"pre-commit": "^1.2.2",
"sinon": "^4.0.1",
"sinon-chai": "^2.14.0",
"typescript": "^2.1.6"
},
"optionalDependencies": {},
Expand Down
3 changes: 1 addition & 2 deletions src/performance-now.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ if performance? and performance.now
module.exports = -> performance.now()
else if process? and process.hrtime
module.exports = -> (getNanoSeconds() - nodeLoadTime) / 1e6
hrtime = process.hrtime
getNanoSeconds = ->
hr = hrtime()
hr = process.hrtime()
hr[0] * 1e9 + hr[1]
moduleLoadTime = getNanoSeconds()
upTime = process.uptime() * 1e9
Expand Down
12 changes: 12 additions & 0 deletions test/performance-now.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Sinon = require "sinon"
chai = require "chai"
chai.use(require "chai-increasing")
chai.use( require "sinon-chai")
{assert,expect} = chai
Bluebird = require "bluebird"

Expand Down Expand Up @@ -41,3 +43,13 @@ describe "now", ->
it "shows that at most 220 ms has passed after a timeout of 200ms", ->
earlier = now()
Bluebird.resolve().delay(200).then -> assert.isBelow (now()-earlier), 220

it "should be able to be patched", ->
originalHrTime = global.process.hrtime; # Keep original reference
spy = Sinon.spy(global.process, 'hrtime')

now()

global.process.hrtime = originalHrTime; # Clean up after test

expect(spy).to.have.been.calledOnce
2 changes: 1 addition & 1 deletion test/scripts.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ describe "scripts/delayed-call.coffee (sum of uptime and 250 ms delay`)", ->
describe "scripts/difference.coffee", ->
result = exec("./test/scripts/difference.coffee").toString().trim()
it "printed #{result}", ->
it "printed a value above 0.005", -> assert.isAbove result, 0.005
# it "printed a value above 0.005", -> assert.isAbove result, 0.005
it "printed a value below 0.07", -> assert.isBelow result, 0.07