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.
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.
Most helpful comment
Still an issue on v4.17.4 :shirt: