使用 jest 运行测试时如何修复“测试套件运行失败”

inn*_*nis 4 javascript unit-testing node.js jestjs

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

\n\n

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

\n\n
describe(\'First test\', async () => {\n  const OLD_ENV = process.env;\n\n  beforeEach(() => {\n    jest.resetModules();\n    process.env = { MONGO_CLUSTER_URI: <<<hidden for security reasons>>>\' };\n  })\n\n  afterEach(() => {\n    process.env = OLD_ENV;\n  })\n\n  test(\'Should pass\', async () => {\n    console.log(\'Testing...\');\n    const response = await index.handler({ event: \'input\' }, { });\n    console.log(response);\n    expect(response).toBe(true);\n  })\n})\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
tvrsky@pc:~/lambda-testes-jest/functions/cf_post_processing$ lerna run test --scope cf_post_processing\ninfo cli using local version of lerna\nlerna notice cli v3.16.4\nlerna info versioning independent\nlerna info filter [ \'cf_post_processing\' ]\nlerna info Executing command in 1 package: "npm run test"\nlerna ERR! npm run test exited 1 in \'cf_post_processing\'\nlerna ERR! npm run test stdout:\n\n> cf_post_processing@1.0.3 test /home/tvrsky/lambda-testes-jest/functions/cf_post_processing\n> jest *.test.js\n\n  console.log node_modules/jest-jasmine2/build/jasmine/Env.js:502\n      \xe2\x97\x8f Test suite failed to run\n\n        Returning a Promise from "describe" is not supported. Tests must be defined synchronously.\n        Returning a value from "describe" will fail the test in a future version of Jest.\n\n          1 | const index = require(\'./index\');\n          2 | \n        > 3 | describe(\'First test\', async () => {\n            | ^\n          4 |   const OLD_ENV = process.env;\n          5 | \n          6 |   beforeEach(() => {\n\n          at addSpecsToSuite (node_modules/jest-jasmine2/build/jasmine/Env.js:504:17)\n          at Object.describe (index.test.js:3:1)\n\n\n  console.log index.test.js:16\n    Testing...\n\n  console.log index.js:22\n    undefined\n\n  console.log index.test.js:18\n    { statusCode: 500,\n      body: { message: \'db.collection is not a function\' } }\n\n\nlerna ERR! npm run test stderr:\nFAIL ./index.test.js (9.7s)\n  First test\n    \xe2\x9c\x95 Should pass (137ms)\n\n  \xe2\x97\x8f First test \xe2\x80\xba Should pass\n\n    expect(received).toBe(expected) // Object.is equality\n\n    Expected: true\n    Received: {"body": {"message": "db.collection is not a function"}, "statusCode": 500}\n\n      17 |     const response = await index.handler({ event: \'input\' }, { });\n      18 |     console.log(response);\n    > 19 |     expect(response).toBe(true);\n         |                      ^\n      20 |   })\n      21 | })\n      22 | \n\n      at Object.toBe (index.test.js:19:22)\n\nTest Suites: 1 failed, 1 total\nTests:       1 failed, 1 total\nSnapshots:   0 total\nTime:        16.901s\nRan all test suites matching /index.test.js/i.\nnpm ERR! code ELIFECYCLE\nnpm ERR! errno 1\nnpm ERR! cf_post_processing@1.0.3 test: `jest *.test.js`\nnpm ERR! Exit status 1\nnpm ERR! \nnpm ERR! Failed at the cf_post_processing@1.0.3 test script.\nnpm ERR! This is probably not a problem with npm. There is likely additional logging output above.\n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR!     /home/tvrsky/.npm/_logs/2019-09-18T18_27_53_742Z-debug.log\n\nlerna ERR! npm run test exited 1 in \'cf_post_processing\'\n
Run Code Online (Sandbox Code Playgroud)\n

Mit*_*lie 6

你的测试的第一行应该是

describe('First test', () => {
Run Code Online (Sandbox Code Playgroud)

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