New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to test middleware with Jest #32848
Comments
P.S. I've tried the following resolutions to no avail:
And follow-up question: Just to get through this, is there something similar to |
P.P.S. Maybe this is a documentation issue because if I include EDIT: I can get rid of the Jest error if I do that but then the middleware doesn't work, so this isn't a solution and I don't think it's just documentation. |
This comment has been minimized.
This comment has been minimized.
Do you have a more elaborate reproduction? I would like to see your test file, or get an idea of what you are trying to do. Have you followed this guide? https://nextjs.org/docs/testing#setting-up-jest-with-the-rust-compiler |
I'm experiencing the same issue using babel. @davidrhoderick have you found any solution for this? |
I'm also having this error: #31152 (comment) @balazsorban44 here you can quickly verify: https://codesandbox.io/s/laughing-moon-ykbrs?file=/pages/index.test.ts Just uncomment line 6 in |
Jest only understands commonjs that's the problem |
Yeah I guessed so, but what's weird is that In case someone else is interested: // jest.config.js
const ignoredModules = ["next"].join("|")
module.exports = {
clearMocks: true,
transformIgnorePatterns: [`/node_modules/(?!${ignoredModules})`],
testEnvironment: "jest-environment-jsdom",
transform: {
"^.+\\.(t|j)sx?$": ["@swc/jest"],
},
setupFiles: ["<rootDir>/jest.setup.js"],
} The real deal is adding |
Thank you for leaving some breadcrumbs here, @wovalle , I'm definitely struggling with the same challenge at the moment. I'm surprised that |
I struggle with the same issue, when transforming with babel-jest I got my middleware used for testing is simple
using this jest config:
|
@gom3s yeah node doesn't include fetch and you have to use a polyfill. Import node-fetch or isomorphic-fetch in your |
It works!! thank you @wovalle final setup that works for me
|
My solution was create import { NextRequest } from 'next/dist/server/web/spec-extension/request';
import { NextResponse } from 'next/dist/server/web/spec-extension/response';
export { NextRequest, NextResponse }; |
Did this solution work for others? I added this but now getting |
We aim to ship very soon a Jest runtime that will allow you to test middleware code without doing anything special. Just a heads up that we are working on this! |
I like this solution. This works for me. 🎉 |
This issue does not occur only with jest. The solution proposed by @vxcamiloxv is fine because jest has a I posted #35089 (comment) for anyone using a framework relying on Basically the solution is to setup Basically: // cucumber.js || nyc.js || whatever config file
// This prevents `Request is not defined` error
require("isomorphic-fetch");
const ignoredModules = ["next/server"].join("|");
require("ts-node").register({
transpileOnly: true,
ignore: [`node_modules/(?!${ignoredModules})`],
// Do not add custom tsconfig here, it is managed under root tsconfig.json
}); // tsconfig.json
{
"ts-node": {
"compilerOptions": {
// ...
"module": "commonjs",
"jsx": "react-jsx", // override jsx for ts-node
"allowJs": true
// ...
}
},
"compilerOptions": {
// ...
"jsx": "preserve" // required by NextJS: will be automatically updated if you use another value
//...
},
// ...
} |
If different contexts (using Jest, using Nx, etc), server.js is getting imported when using middleware and throwing the error: "SyntaxError: Unexpected token 'export'". fixes #32848 ## Bug - [x] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
12.0.7
What version of Node.js are you using?
16.13.1
What browser are you using?
Firefox
What operating system are you using?
Pop!_OS
How are you deploying your application?
Vercel
Describe the Bug
When trying to test
_middleware.js
with Jest, I'm unable toimport { NextResponse } from 'next/server'
without throwing an errorSyntaxError: Unexpected token 'export'
becausenext/server
usesexport { NextRequest }
in line 1.Expected Behavior
I would like to be able to test my
_middleware.js
authentication methodology.To Reproduce
_middleware.js
NextResponse
fromnext/server
.The text was updated successfully, but these errors were encountered: