最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to avoid Jest warnings: A "describe" callback must not return a value? - Stack Overflow

programmeradmin6浏览0评论

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 acpaying 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?

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 acpaying 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?

Share Improve this question edited Mar 18, 2019 at 8:05 skyboyer 23.7k7 gold badges62 silver badges71 bronze badges asked Mar 17, 2019 at 14:13 Patrick HundPatrick Hund 20.2k12 gold badges70 silver badges95 bronze badges 5
  • 2 Any specific reason other than personal preference that you need to use shorthand syntax? This seems to be 100% resolvable by adding braces. – Alexander Staroselsky Commented Mar 17, 2019 at 15:09
  • 1 Personal preference, I'm just used to it, and I like the concise syntax – Patrick Hund Commented Mar 17, 2019 at 15:11
  • 1 I've been getting the same warning with a slightly different stack trace regardless of whether my describe()s use shorthand arrow functions or not. Turns out it's a bug in PhpStorm to be resolved in v2018.3.6. Workaround can be found here: intellij-support.jetbrains./hc/en-us/munity/posts/… – urig Commented Mar 21, 2019 at 11:12
  • This also happens when you nest a describe within a describe. These changes are bugs. If I have time I'll report this on the repo. – lonix Commented Apr 3, 2019 at 16:49
  • @Ionix Well, the fact that they give out a detailed deprecation warning makes it seem like it's not a bug, but a feature – Patrick Hund Commented Apr 3, 2019 at 17:09
Add a ment  | 

3 Answers 3

Reset to default 7

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-patible 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.

May be related to an unhandled exception thrown inside describe callback. For example, occurred to me when I wanted to access mock of non existing member (outside of test block):

logger.info1111.mockImplementation((m, d) => console.info(m, d));
发布评论

评论列表(0)

  1. 暂无评论