Skip to content

How to test dynamic API routes? #11784

Answered by dheeraj-p
dheeraj-p asked this question in Help
Discussion options

You must be logged in to vote

Got it. We can use URLSearchParams with the above approach to achieve it.

describe("/ handler", () => {
  test("responds 200 to authed GET", async () => {
    expect.assertions(1)
    let requestHandler = (req, res) => {
      return apiResolver(req, res, undefined, handler)
    }

    let server = http.createServer(requestHandler)
    const query = {
        type: 'any-value',
        path: ['some', 'dynamic', 'path'],
      };

    const response = await fetch(url + '?' + new URLSearchParams(query).toString());
    expect(response.status).toBe(200)
    return server.close()
  })
})


Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@rafinskipg
Comment options

Answer selected by dheeraj-p
Comment options

You must be logged in to vote
2 replies
@Tymek
Comment options

@danawoodman
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
7 participants