question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing middleware with Jest: TypeError: this._headers.getAll is not a function

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:20:05 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T8101

Binaries: Node: 18.9.0 npm: 8.19.1 Yarn: 1.22.19 pnpm: N/A

Relevant packages: next: 13.0.2-canary.0 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Hello everybody,

I am currently having issues trying to test the middleware.ts in my project, and to reproduce it, I’ve created a small project from next.js/examples/with-jest template, and here is the repo.

Screenshot 2022-11-02 at 14 51 50

The middleware:

export function middleware(req: NextRequest) {
    console.log('Hello')
    return NextResponse.next({headers: new Headers()})
}

The test:

describe('Middleware', () => {
  it('middleware', async () => {
    const res = await middleware(new NextRequest('http://localhost/'))

    console.log('res', res)
  })
})

Other findings:

I’ve also dug a little bit, and it seems that node_modules/next/server/web/spec-extension/cookies/response-cookies.ts:36:35 is expected to fail, I just don’t know in which circumstances:

Screenshot_02_11_2022__14_55

Other things I’ve tried:

Changing jestEnvironment to node didn’t yield the expected result. I’ve done it by adding the following comment in the test:

/**
 * @jest-environment node
 */

Thanks in advance

Expected Behavior

I expect to be able to test the middleware.

Link to reproduction

https://github.com/firatgh/nextjs-with-jest

To Reproduce

yarn test

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:7
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
BolajiAyodejicommented, Nov 11, 2022

Same issue for me with Next13 on Netlify.

Screenshot 2022-11-11 at 7 04 33 PM
4reactions
AndrewVoscommented, Nov 9, 2022

This happens for me in production when I upgrade to Next.js 13:

image

Literally just some text. This is on Netlify.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Middleware and get TypeError: is not a function
This module authorize user and transfers to controller the token. The controller send token for client. – Sergei R. Mar 21, 2018 at...
Read more >
Unit test Express middleware - TypeScript, Jest
A middleware checks if the request contains authorization header or not. ... Here is the unit tests with Express Request, Response and Next...
Read more >
Changelog - Cypress Documentation
This command caches and restores cookies , localStorage , and sessionStorage in order to recreate a consistent browser context between tests. Addresses #20977....
Read more >
Stubbing HTTP Requests with Sinon - Michael Herman
Let's look at how to stub HTTP requests with Sinon.js during test runs.
Read more >
AWS JavaScript SDK v3 - usage, problems, testing - Better Dev
And it's not only for DynamoDB. You will find functions like this for other clients as well. DynamoDB. The SDK v2 DynamoDB DocumentClient,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found