用玩笑运行测试时如何修复“测试套件无法运行”

时间:2019-09-18 18:34:28

标签: javascript node.js unit-testing jestjs

我正在学习如何开玩笑地编写测试,并且在运行它们时得到了巨大的日志出口。

由于这是一个很小的代码,我认为完全发布它没有问题。

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'

1 个答案:

答案 0 :(得分:1)

测试的第一行应该是

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

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