Jest cannot find unit tests in file when using supertest inside. Example from the Jest for web frameworks example.
createServer returns Promise<Express>, an express app.
This snippet currently passes when running jest
import request from "supertest";
import assert from "assert";
import { createServer } from "./server";
describe("/diag/json", async () => {
it("should get sample json", async () => {
assert.equal(1, 1);
});
});
This snippet (almost identical to docs and blogs) fails because jest can't find any tests in the file.
import request from "supertest";
import { createServer } from "./server";
describe("/diag/json", async () => {
const app = await createServer();
it("should get sample json", async () => {
const response = await request(app).get("/diag/json");
expect(response.status).toBe(200);
});
});
Error
● Test suite failed to run
Your test suite must contain at least one test.
at node_modules/jest-cli/build/TestScheduler.js:256:22
Jest either passes running my unit tests or it fails from running my unit tests. (It currently fails from not running my unit tests)
npx envinfo --preset jest System:
OS: Linux 4.18 Ubuntu 18.10 (Cosmic Cuttlefish)
CPU: (4) x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
Binaries:
Node: 10.14.1 - ~/.nvm/versions/node/v10.14.1/bin/node
Yarn: 1.13.0 - ~/.nvm/versions/node/v10.14.1/bin/yarn
npm: 6.5.0 - ~/.nvm/versions/node/v10.14.1/bin/npm
npmPackages:
jest: * => 23.6.0
Tests must be defined synchronously, so you'll need to move createServer to a beforeEach or beforeAll, which may be async.
Took me a month, but I finally had some time to do something against these errors #7852 😄
A bit late but jest still fails with async and the documentation says very clear how to use async code. Either you have to remove this from the docs or you have to fix this issue.
Promises
If your code uses promises, there is a more straightforward way to handle asynchronous tests. Return a promise from your test, and Jest will wait for that promise to resolve. If the promise is rejected, the test will automatically fail.
For example, let's say that fetchData, instead of using a callback, returns a promise that is supposed to resolve to the string 'peanut butter'. We could test it with: `
I am doing my tests with promises and i am receiving the same error.
describe is not a test, it's just a grouping. You can use async _in_ test and it, but the test function itself must be defines synchronously.
Ohh ok I understand. Thanks a lot
Most helpful comment
Tests must be defined synchronously, so you'll need to move
createServerto abeforeEachorbeforeAll, which may beasync.