Files
next.js/test/e2e/api-resolver-query-writeable/api-resolver-query-writeable.test.ts
T
Alessandro GrosselleGitHubAlessandro Grossellegraphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>Sebastian BeltranTim Neutkens
456d8e7b8c fix: ensure req.query is writable (#81573)
### What?
- Upgrade development dependency **Express** to the latest version
(5.1.0).
- Replace direct assignment of `apiReq.query` with
`Object.defineProperty` to ensure the property is writable, enumerable,
and configurable.

### Why?
- To use the most up-to-date and supported version of Express instead of
a legacy one.
- To fix
[expressjs/express#6633](https://github.com/expressjs/express/issues/6633),
where Express 5.x defines `req.query` as a read-only getter, preventing
Next.js from overriding it directly.

### How?
- Update the Express version in `package.json` and run `pnpm install` to
apply the change.
- Replace:

```js
apiReq.query = query;
```
With 
```js
Object.defineProperty(apiReq, 'query', {
  value: { ...query },
  writable: true,
  enumerable: true,
  configurable: true,
});
```

---------

Co-authored-by: Alessandro Grosselle <[email protected]>
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: Sebastian Beltran <[email protected]>
Co-authored-by: Tim Neutkens <[email protected]>
2025-10-28 12:15:05 +01:00

32 lines
824 B
TypeScript

import { nextTestSetup } from 'e2e-utils'
describe('api-resolver-query-writeable', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
startCommand: 'node server.js',
serverReadyPattern: /Next mode: (production|development)/,
dependencies: {
'get-port': '5.1.1',
express: '5.1.0',
},
})
if (skipped) {
return
}
it('should allow req.query to be writable and reflect changes made in the API handler', async () => {
const res = await next.fetch('/api?hello=yes', {
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
})
if (!res.ok) {
throw new Error('Fetch failed')
}
const data = await res.json()
expect(data).toEqual({ query: { hello: 'yes', changed: 'yes' } })
})
})