繁体   English   中英

笑话:ReferenceError:描述未定义

[英]Jest : ReferenceError: describe is not defined

我尝试按照本教程为我的项目设置 jest: https://dev.to/nedsoft/testing-nodejs-express-api-with-jest-and-supertest-1km6

但是当我尝试运行基本测试时,出现以下错误:

 FAIL  tests/sample.test.js
  ● Test suite failed to run

    ReferenceError: describe is not defined

    > 1 | describe('Sample Test', () => {
        | ^
      2 |     it('should test that true === true', () => {
      3 |         expect(true).toBe(true)
      4 |     })

      at Object.<anonymous> (tests/sample.test.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.521 s
Ran all test suites.
(node:15452) ExperimentalWarning: The fs.promises API is experimental
npm ERR! Test failed.  See above for more details.

我尝试在我的 package.json 中删除:

testEnvironment: 'node',

但什么都没有改变。

一个想法?

好的,问题是最新版本的 jest 与节点 10.13.0 冲突

来源: https://github.com/facebook/jest/issues/9538

所以做这个就可以了:

npm install --save-dev jest@25.2.2 supertest

“开玩笑”:“^27.2.0” 就我而言,我得到了ReferenceError: describe is not defined错误,因为我在jest.config.js中将 injectGlobals配置设置为false

关于injectGlobals配置的文档:

将 Jest 的全局变量( expecttestdescribebeforeEach等)插入到全局环境中。 如果将此设置为false ,则应从@jest/globals导入

jest.config.js

module.exports = {
  injectGlobals: true,
  testEnvironment: 'node',
};
describe('index', () => {
  test('should pass', () => {
    expect(1 + 1).toBe(2);
  });
});

injectGlobals设置为true后,它工作正常。

就我而言,这是因为我试图为我的 nextjs 应用程序的一个页面添加测试。

我没有将它添加到 pages 文件夹中,而是移动了导致Jest: ReferenceError: describe is not definedtest文件夹的有问题的测试,它开始工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM