jeudi 27 février 2020

How to initialize manually next.js app (for testing purpose)?

I try to test my web services, hosted in my Next.js app and I have an error with not found Next.js configuration.

My web service are regular one, stored in the pages/api directory. My API test fetches a constant ATTACKS_ENDPOINT thanks to this file:

/pages/api/tests/api.spec.js

import { ATTACKS_ENDPOINT } from "../config"

...

describe("endpoints", () => {
   beforeAll(buildOptionsFetch)

   it("should return all attacks for attacks endpoint", async () => {
      const response = await fetch(API_URL + ATTACKS_ENDPOINT, headers)

config.js

import getConfig from "next/config"

const { publicRuntimeConfig } = getConfig()

export const API_URL = publicRuntimeConfig.API_URL

My next.config.js is present and is used properly by the app when started.

When the test is run, this error is thrown

    TypeError: Cannot destructure property `publicRuntimeConfig` of 'undefined' or 'null'.

  1 | import getConfig from "next/config"
  2 | 
> 3 | const { publicRuntimeConfig } = getConfig()

I looked for solutions and I found this issue which talks about _manually initialise__ next app.

How to do that, given that I don't test React component but API web service ?

Aucun commentaire:

Enregistrer un commentaire