[英]How to fix “Test suite failed to run” when running a test with 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
不應是異步的,因為其中的多個describes
或測試將同時運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.