Skip to content
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

Detailed error and warnings upon next() call #13539

Merged
merged 7 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions errors/invalid-server-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# It looks like the next instance is being instantiated incorrectly.

#### Why This Error Occurred

You have passed a null or undefined parameter to the next() call.

#### Possible Ways to Fix It

Make sure you are passing the variables properly:

```js
const app = next()
```

And make sure you're passing dev as shown in the examples below:

```js
const app = next({ dev: boolean })
```

### Useful Links

- [custom-server-express](https://github.com/vercel/next.js/blob/6ca00bfe312c8d3fc5c20d25a9cd8d2741a29332/examples/custom-server-express/server.js#L6)
- [custom-server](https://github.com/vercel/next.js/blob/6ca00bfe312c8d3fc5c20d25a9cd8d2741a29332/examples/custom-server/server.js#L6)
- [custom-server-typescript](https://github.com/vercel/next.js/blob/6ca00bfe312c8d3fc5c20d25a9cd8d2741a29332/examples/custom-server-typescript/server/index.ts#L7)
12 changes: 12 additions & 0 deletions packages/next/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type NextServerConstructor = Omit<ServerConstructor, 'staticMarkup'> & {
function createServer(options: NextServerConstructor): Server {
const standardEnv = ['production', 'development', 'test']

if (typeof options === 'undefined') {
Timer marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options'
)
}

if (
!(options as any).isNextDevCommand &&
process.env.NODE_ENV &&
Expand All @@ -22,6 +28,12 @@ function createServer(options: NextServerConstructor): Server {
}

if (options.dev) {
if (typeof options.dev !== 'boolean') {
console.warn(
"Warning: 'dev' is not a boolean which could introduce unexpected behavior. https://err.sh/next.js/invalid-server-options"
)
}

const DevServer = require('./next-dev-server').default
return new DevServer(options)
}
Expand Down
1 change: 1 addition & 0 deletions test/integration/invalid-server-options/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'test'
59 changes: 59 additions & 0 deletions test/integration/invalid-server-options/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import next from 'next'
import { join } from 'path'
const dir = join(__dirname, '../')
const warningMessage =
"Warning: 'dev' is not a boolean which could introduce unexpected behavior. https://err.sh/next.js/invalid-server-options"

describe('Invalid server options', () => {
test('next() called with no parameters should throw error', () => {
expect(() => next()).toThrowError(
'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options'
)
})

test('next() called with undefined parameters should throw error', () => {
expect(() => next(undefined)).toThrowError(
'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options'
)
})
Timer marked this conversation as resolved.
Show resolved Hide resolved

test('next() called with dev as string should send warning', () => {
const consoleSpy = jest.spyOn(console, 'warn')
const dev = 'string'
next({ dev, dir })

expect(consoleSpy).toHaveBeenCalledWith(warningMessage)
})

test('next() called with dev as number should send warning', () => {
const consoleSpy = jest.spyOn(console, 'warn')
const dev = 123
next({ dev, dir })

expect(consoleSpy).toHaveBeenCalledWith(warningMessage)
})

test('next() called with dev as array should send warning', () => {
const consoleSpy = jest.spyOn(console, 'warn')
const dev = ['array']
next({ dev, dir })

expect(consoleSpy).toHaveBeenCalledWith(warningMessage)
})

test('next() called with dev as object should send warning', () => {
const consoleSpy = jest.spyOn(console, 'warn')
const dev = { test: 'goes here' }
next({ dev, dir })

expect(consoleSpy).toHaveBeenCalledWith(warningMessage)
})

test('next() called with dev as function should send warning', () => {
const consoleSpy = jest.spyOn(console, 'warn')
const dev = () => console.log('test')
next({ dev, dir })

expect(consoleSpy).toHaveBeenCalledWith(warningMessage)
})
})