首页 > 解决方案 > 使用 jest 运行测试时如何修复“测试套件无法运行”

问题描述

我正在学习如何用 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'

标签: javascriptnode.jsunit-testingjestjs

解决方案


测试的第一行应该是

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

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


推荐阅读