question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using nock with sinon fake timers

See original GitHub issue

This is more of a question rather than an issue report.

When I use Sinon to fake the current date and time like so: Sinon.useFakeTimers(new Date("2016-01-01T00:11:22+10:00").getTime());

And then I call Nock to intercept, it never registers the URL (confirmed by turning debugging on).

However if I don’t use Sinon fake timers and I use something like mockdate js, it works.

Just curious as to what is happening here.

Thanks.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
LKaycommented, Oct 18, 2016

Having the same problem as described above. I have a method that does long polling (and uses setTimeout to trigger itself in time intervals). When I use sinon fake timers nock requests are not fired at all.

My test code looks like this:

import "../utils"
import { expect } from "chai"
import * as sinon from "sinon"
import nock = require("nock")
import { Resource } from "../../src/resources/Resource"
import { Scope } from "nock"
import { POLLING_INTERVAL } from "../../src/constants"

describe("Charges", () => {
    let resource: Resource
    let scope: Scope
    const testEndpoint = "http://localhost:80"
    let sandbox: Sinon.SinonSandbox

    beforeEach(() => {
        resource = new Resource()
        scope = nock(testEndpoint)
        sandbox = sinon.sandbox.create({
            properties: ["spy", "clock"],
            useFakeTimers: true
        })
    })

    afterEach(() => {
        nock.cleanAll()
        sandbox.restore()
    })

    context("route GET /poll", () => {
        it("should perform long polling until condition is met", () => {
            let repeats = 3
            const spy = sandbox.spy(global, "fetch")
            const scopeScope = scope
                .get(/\/poll$/i)
                .thrice()
                .reply(200, () => {
                    sandbox.clock.tick(POLLING_INTERVAL)
                    return { status : repeats-- === 0 ? "success" : "pending" }
                }, { "Content-Type" : "application/json" })

            return resource.poll("1", "1").should.eventually.eql({ status : "success" })
                .then(() => expect(spy).to.have.been.calledThrice)
        })
    })

})
0reactions
lock[bot]commented, Oct 7, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fake timers - Sinon.JS
Fake timers are synchronous implementations of setTimeout and friends that Sinon.JS can overwrite the global functions with to allow you to more easily...
Read more >
Using nock with sinon fake timers - Bountysource
When I use Sinon to fake the current date and time like so: Sinon.useFakeTimers(new Date("2016-01-01T00:11:22+10:00").getTime());.
Read more >
How to use Fake timers with nightwatch.js and sinon.js?
If you just want to use a fake Date, you can write it like this: clock = sinon.useFakeTimers(new Date(2015, 7, 20).getTime(), "Date").
Read more >
Best Practices for Spies, Stubs and Mocks in Sinon.js
You may need to disable fake timers for async tests when using sinon.test . This is a potential source of confusion when using...
Read more >
When using Sinon's useFakeTimers, access the real time and ...
I'm using Sinon's useFakeTimers to test a time-dependent module. When in "fake time" mode, I need to use the real setTimeout, meaning that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found