Lodash: sinon useFakeTimers and lodash

Created on 26 Jul 2016  路  3Comments  路  Source: lodash/lodash

I realize this has been asked twice:

It's still unclear to me how runInContext fixes the local references problem. In my project, I want to simulate input changes and count autocomplete calls.

// form.js

import { debounce } from "lodash"

class Form extends Component {
  componentWillMount () {
    this.search = debounce(this.search, 300)
  }

  search(query) {
    this.props.searchUsers(query)
  }

  onChange(ev) {
    this.search(ev.target.value)
  }
}
// form-test.js

import sinon from "sinon"
import { runInContext } from "lodash"
import Form from "./form"

it("should debounce autocomplete", () => {
  const clock = sinon.useFakeTimers()

  // runInContext somehow?

  clock.restore()
})

Wouldn't runInContext create a new lodash instance? How would my component use that instance?

I realize I could pass debounce in as a function property, stub it out, and re-implement debounce, but the purpose of the black box test is to make sure nobody removes the debouncing logic in the future.

To fix the problem, I used the debounce npm package instead.

import debounce from "debounce"

I hope this makes sense. Also, hopefully this will help others who search "sinon" and get confused about runInContext.

invalid

Most helpful comment

Still an issue on v4.17.4 :shirt:

All 3 comments

Lodash v4.14.0 should allow you to mock setTimeout, clearTimeout, and Date.now.

Still an issue on v4.17.4 :shirt:

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sofianito picture sofianito  路  3Comments

JoMas971 picture JoMas971  路  3Comments

gajus picture gajus  路  3Comments

ryyppy picture ryyppy  路  3Comments

SameerSiddiqui picture SameerSiddiqui  路  3Comments