After upgrading Jest from version 23 to version 24, when running my tests, I get a warning message like this for almost every test:
A "describe" callback must not return a value. Returning a value from "describe" will fail the test in a future version of Jest.
The accompaying stack trace points to this module:
addSpecsToSuite (node_modules/jest-jasmine2/build/jasmine/Env.js:443:15)
The reason for this is that I like to use the short-hand version of arrow-functions for my tests, omitting curly braces when the function body contains only one statement, for example:
describe('true', () =>
it('should be truthy', () =>
expect(true).toBeTruthy()));
The it
statement apparently returns something other than undefined
, hence the warning.
I've found two ways of fixing this:
① Don't Use Shorthand Arrow Functions
describe('true', () => {
it('should be truthy', () =>
expect(true).toBeTruthy());
});
② Use void
to Force Returning Undefined
describe('true', () =>
void it('should be truthy', () =>
expect(true).toBeTruthy()));
I find neither of these options acceptable, I don't want to refactor thousands of tests just to make Jest (or Jasmine) happy.
So my question is:
Is there a way of configuring Jest so that these warnings are not issued when using shorthand arrow functions?
I guess if you really want to keep your existing test syntax and just want to avoid the warning you can do this:
const realDescribe = describe;
describe = ((name, fn) => { realDescribe(name, () => { fn(); }); });
Just add that code to a module included in your setupFilesAfterEnv
and it will run "immediately after the test framework has been installed in the environment" and "before each test".
The above code will set the global describe
to a function that calls the real describe
but wraps the function
parameter in an anonymous function that doesn't return anything.
This problem also shows up if you're using global functions that Jest v24 doesn't recognize. I'm converting some Mocha tests to Jest, and Mocha's before()
was throwing the same error:
A "describe" callback must not return a value. Returning a value from "describe" will fail the test in a future version of Jest.
The stack trace pointed to describe()
being the culprit, but the fix was converting the nested before()
calls to a Jest-compatible beforeAll()
. There might be a related issue here with trying to use it()
instead of test()
, but that might be grasping, there's definitely an it()
in Jest's test environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With