Jest: Feature Request: Throw error if first argument to "it" or "test" is not a string

Created on 7 Feb 2018  路  11Comments  路  Source: facebook/jest

Coming from ava, you don't need test descriptions.

The following would work.

test(() => {
    jest.mock('../config', () => () => {
      return { protocol: 'http', host: 'localhost', port: '9999', pathPrefix: '' }
    })
    const config = require('../config')()
    expect(config.api.port).toBe('9999');
}) 

However, this doesn't do anything in jest.

Can we throw an error if there is no function as the second argument? Or if a string is not passed into the first?

Feature Request Help Wanted

Most helpful comment

Can we throw an error if there is no function as the second argument? Or if a string is not passed into the first?

Is it possible to not do this behavior, or at the very least, put the ability to toggle it off?

Maybe my use case isn't common, but I find myself writing out a bunch of test cases first, then I begin implementing them. Getting an error thrown in that scenario clutters up the terminal with errors about "Missing second argument. It must be a callback function".

Thanks!

All 11 comments

Yeah, do you wanna send a PR to make this throw? :)

@cpojer I'd love to. Thanks for the feedback.

Can I do this?

@brianmacdonald sure

@reggi Please correct me if I'm off base, but from what I can tell, jest-Jasmine2 is running 'test' and 'it', and not jest-circus, which, I'm assuming will eventually replace jest-Jasmine2. So should I get the errors to throw in both places? Or just one?

Both would be great!

jest-circus is the eventual replacement for jasmine (jest-jasmine2), but it's currently on the backburner

5558

Can we throw an error if there is no function as the second argument? Or if a string is not passed into the first?

Is it possible to not do this behavior, or at the very least, put the ability to toggle it off?

Maybe my use case isn't common, but I find myself writing out a bunch of test cases first, then I begin implementing them. Getting an error thrown in that scenario clutters up the terminal with errors about "Missing second argument. It must be a callback function".

Thanks!

I agree with @mike-robertson. It's super common in TDD to write the specs first and then implement the behavior. I don't know how this change went through. I'll open a separate issue.

Ditto what @mike-robertson said; the missing second function is (was) a deliberate feature to allow skipped tests and I use it in exactly the way that he describes, writing out test cases to be written as I think about it and filling them in later. It seems that #5558 should have checked that the first arg was a string rather than checking that two args were passed...

Workaround for this now is to pass in an empty function in addition to .skip().

Previously I could do:

test('Test to be written')

But that throws now, so I have to do:

test.skip('Test to be written', () => {})

Which works, but seems much more verbose. Was this intentional?

Was this page helpful?
0 / 5 - 0 ratings