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.

support .withArgs() on expect(fn)

See original GitHub issue

expect.js has a feature to pass arguments to a function using the following syntax

expect(fn).withArgs(some, args).to.throw();

Right now I’m having to write the following

expect(() => fn(some, args)).to.throw();

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
keithamuscommented, Jul 22, 2016
fn.should.throw.with.args(some, args);

Would probably fit more with the rest of the code. Having args as the assertion actually makes for some interesting developments:

addNumbers.should
  .throw.with.args(1, 'a')
  .and.args('a', 2)
  .but.not.throw.with.args(1, 2)

It becomes challenging though - because we can’t assert on the error that is being thrown in a sensible way:

addNumbers.should.throw/* a typeerror, but I can't assert this */.with.args(1, 'a')

Whereas if the args was in the chain, then we can:

addNumbers.should.with.args(1, 'a').throw(TypeError)
1reaction
lucasfcostacommented, Jul 22, 2016

@blockloop IMO that is not a viable solution because it adds another property to the Object.prototype. I think that having should is already enough. If we did that it would be like a new Assertion interface.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I mock functions with specific arguments using Jest?
I found this library that a colleague of mine wrote recently: jest-when import { when } from 'jest-when'; const fn = jest.fn(); when(fn)....
Read more >
Mock Functions - Jest
Mock functions allow you to test the links between code by erasing the actual implementation of a function, capturing calls to the function ......
Read more >
Stubs - Sinon.JS
Test stubs are functions (spies) with pre-programmed behavior. They support the full test spy API in addition to methods which can be used...
Read more >
Jest assert over single or specific argument/parameters with ...
You can use expect.anything() to ignore certain parameters that a mock Jest function is called with, see the following:.
Read more >
jest-when - npm
const fn = jest.fn() // 1) Start with any normal jest mock function when(fn) ... spyOn functions with when(); Supports function matchers ...
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