Jest tests always timing out

default discord avatar
Exo
2 months ago
3

Lately it seems that my jest tests keep timing out. The problem is with the simple test case attempting the insert.



I tried increasing various timeouts but it really seems that the request never finishes. I really just want to the the local api so I tried to do it without the express instance as well but that did not help



import express from "express";
import payload from "payload";
import { simpleCollectionSlug } from "./configs/simple/payload-config";
describe("AutoI18n Plugin Tests", () => {
  beforeAll(async () => {
    process.env["PAYLOAD_CONFIG_PATH"] =
      "src/tests/configs/simple/payload-config.ts";
    const app = express();
    app.listen(3000);

    await payload.init({
      express: app,
      secret: "SECRET",
      local: true,
      mongoURL: false,
      onInit: () => {
        console.log("Loaded simple test cfg");
        // console.log(payload.getAPIURL())
      },
    });
  });

  it("Should load", async () => {
    expect(1).toBe(1);
  });

  it("Should translate a simple entity", async () => {
    const input = {
      text: "foo",
    };

    const res = await payload.create({
      collection: simpleCollectionSlug,
      data: input,
      overrideAccess: true,
    });

    console.log(res);

    expect(1).toBe(1);
  });
});


  AutoI18n Plugin Tests
    ✓ Should load (1 ms)
    ✕ Should translate a simple entity (5003 ms)

  ● AutoI18n Plugin Tests › Should translate a simple entity

    thrown: "Exceeded timeout of 5000 ms for a test.
    Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout."

      24 |   });
      25 |
> 26 |   it("Should translate a simple entity", async () => {
         |   ^
      27 |     const input = {
      28 |       text: "foo",
      29 |     };

      at it (src/tests/plugin.spec.ts:26:3)
      at Object.describe (src/tests/plugin.spec.ts:4:1)


export const simpleCollectionSlug: string = "simpleCollection";
export default buildConfig({
  admin: {
    disable: true,
  },

  debug: true,
  telemetry: false,
  localization: {
    locales: ["de", "en", "es"],
    defaultLocale: "en",
    fallback: false,
  },

  collections: [
    {
      slug: simpleCollectionSlug,
      access: {
        create: () => true,
        read: () => true,
        update: () => true,
        delete: () => true,
      },

      fields: [
        {
          name: "text",
          type: "text",
          localized: true,
        },
        {
          name: "richText",
          type: "richText",
          localized: true,
        },
      ],
    },
  ],
});


Solution

The solution is to provide an in-memory mongo instance, such as



beforeAll(async () => {
    process.env["PAYLOAD_CONFIG_PATH"] =
      "src/tests/configs/simple/payload-config.ts";

    const mongod = await MongoMemoryServer.create()
    const uri = mongod.getUri();

    await payload.init({
      secret: "SECRET",
      local: true,
      mongoURL: uri,
      onInit: () => {
        console.log("Loaded simple test cfg");
        // console.log(payload.getAPIURL())
      },
    });
  });


It was implied that this is the default behaviour for test cases, given the jest tutorial

https://payloadcms.com/blog/typescript-jest-vscode-debugger-tutorial
    Open the post
    Continue the discussion in Discord
    Like what we're doing?
    Star us on GitHub!

    Star

    Connect with the Payload Community on Discord

    Discord

    online

    Can't find what you're looking for?

    Get help straight from the Payload team with an Enterprise License.