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

sinon useFakeTimers and lodash #2533

Closed
aj0strow opened this issue Jul 26, 2016 · 3 comments
Closed

sinon useFakeTimers and lodash #2533

aj0strow opened this issue Jul 26, 2016 · 3 comments
Labels

Comments

@aj0strow
Copy link

aj0strow commented Jul 26, 2016

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.

@jdalton
Copy link
Member

jdalton commented Jul 26, 2016

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

@FDiskas
Copy link

FDiskas commented Nov 20, 2017

Still an issue on v4.17.4 👕

@lock
Copy link

lock bot commented Dec 26, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants