javascript - 使用 jest 运行测试时如何修复 "Test suite failed to run"

标签 javascript node.js unit-testing jestjs

我正在学习如何用 jest 编写测试,当我运行它们时,我得到了这个巨大的日志退出。

由于代码很小,因此我认为将其完整发布没有问题。

describe('First test', async () => {
  const OLD_ENV = process.env;

  beforeEach(() => {
    jest.resetModules();
    process.env = { MONGO_CLUSTER_URI: <<<hidden for security reasons>>>' };
  })

  afterEach(() => {
    process.env = OLD_ENV;
  })

  test('Should pass', async () => {
    console.log('Testing...');
    const response = await index.handler({ event: 'input' }, { });
    console.log(response);
    expect(response).toBe(true);
  })
})

我不知道它是否应该给我带来这么大的输出,但我认为它不是,因为它看起来像是抛出了某种错误。我将输出发布在下面,以便大家可以帮助我。

tvrsky@pc:~/lambda-testes-jest/functions/cf_post_processing$ lerna run test --scope cf_post_processing
info cli using local version of lerna
lerna notice cli v3.16.4
lerna info versioning independent
lerna info filter [ 'cf_post_processing' ]
lerna info Executing command in 1 package: "npm run test"
lerna ERR! npm run test exited 1 in 'cf_post_processing'
lerna ERR! npm run test stdout:

> cf_post_processing@1.0.3 test /home/tvrsky/lambda-testes-jest/functions/cf_post_processing
> jest *.test.js

  console.log node_modules/jest-jasmine2/build/jasmine/Env.js:502
      ● Test suite failed to run

        Returning a Promise from "describe" is not supported. Tests must be defined synchronously.
        Returning a value from "describe" will fail the test in a future version of Jest.

          1 | const index = require('./index');
          2 | 
        > 3 | describe('First test', async () => {
            | ^
          4 |   const OLD_ENV = process.env;
          5 | 
          6 |   beforeEach(() => {

          at addSpecsToSuite (node_modules/jest-jasmine2/build/jasmine/Env.js:504:17)
          at Object.describe (index.test.js:3:1)


  console.log index.test.js:16
    Testing...

  console.log index.js:22
    undefined

  console.log index.test.js:18
    { statusCode: 500,
      body: { message: 'db.collection is not a function' } }


lerna ERR! npm run test stderr:
FAIL ./index.test.js (9.7s)
  First test
    ✕ Should pass (137ms)

  ● First test › Should pass

    expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: {"body": {"message": "db.collection is not a function"}, "statusCode": 500}

      17 |     const response = await index.handler({ event: 'input' }, { });
      18 |     console.log(response);
    > 19 |     expect(response).toBe(true);
         |                      ^
      20 |   })
      21 | })
      22 | 

      at Object.toBe (index.test.js:19:22)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        16.901s
Ran all test suites matching /index.test.js/i.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! cf_post_processing@1.0.3 test: `jest *.test.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the cf_post_processing@1.0.3 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/tvrsky/.npm/_logs/2019-09-18T18_27_53_742Z-debug.log

lerna ERR! npm run test exited 1 in 'cf_post_processing'

最佳答案

测试的第一行应该是

describe('First test', () => {

该错误告诉您问题是什么。基本上,describe 不应该是异步的,因为多个 describe 或其中的测试将同时运行。

关于javascript - 使用 jest 运行测试时如何修复 "Test suite failed to run",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57998950/

相关文章:

javascript - sails res.view 不是函数

node.js - ENOENT : no such file or directory, mkdir 与 { recursive: true }

javascript - chainRec 的基本思想是什么?

javascript - 在html5中使用filewriter api写两次会导致错误

javascript - ng-if 和 ng-show 无法切换表的可见性

c++ - 如何修改文本文件的 "Date modified"属性而不影响使用 C++ 的内容?

matlab - 对 Simulink 模型进行集成测试

javascript - KendoUI 网格过滤日期格式

javascript - 我如何解决我的 promise 中已经运行的 promise

javascript - Jest Mock(用于异步)文档似乎没有测试实际代码