Expose next function type (#7726)
* Expose next function type * Add type testing for typescript custom server * Move types test to integration folder
This commit is contained in:
parent
e00a2c5d64
commit
12f1a63442
8 changed files with 95 additions and 0 deletions
4
packages/next/types/index.d.ts
vendored
4
packages/next/types/index.d.ts
vendored
|
@ -13,6 +13,8 @@ import {
|
|||
|
||||
import { PageConfig } from 'next-server/types'
|
||||
|
||||
import next from 'next/dist/server/next'
|
||||
|
||||
// Extend the React types with missing properties
|
||||
declare module 'react' {
|
||||
// <html amp=""> support
|
||||
|
@ -52,3 +54,5 @@ export {
|
|||
NextApiRequest,
|
||||
PageConfig,
|
||||
}
|
||||
|
||||
export default next
|
||||
|
|
2
test/integration/custom-server-types/.gitignore
vendored
Normal file
2
test/integration/custom-server-types/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
server.js
|
||||
pages/index.jsx
|
2
test/integration/custom-server-types/next-env.d.ts
vendored
Normal file
2
test/integration/custom-server-types/next-env.d.ts
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/// <reference types="next" />
|
||||
/// <reference types="next/types/global" />
|
1
test/integration/custom-server-types/pages/index.tsx
Normal file
1
test/integration/custom-server-types/pages/index.tsx
Normal file
|
@ -0,0 +1 @@
|
|||
export default () => <p>hello world</p>
|
17
test/integration/custom-server-types/server.ts
Normal file
17
test/integration/custom-server-types/server.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import next from 'next'
|
||||
import http from 'http'
|
||||
|
||||
const dir = __dirname
|
||||
const port = process.env.PORT || 3000
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
|
||||
const app = next({ dev, dir })
|
||||
const handleNextRequests = app.getRequestHandler()
|
||||
|
||||
app.prepare().then(() => {
|
||||
const server = new http.Server((req, res) => {
|
||||
handleNextRequests(req, res)
|
||||
})
|
||||
|
||||
server.listen(port)
|
||||
})
|
14
test/integration/custom-server-types/test/index.test.js
Normal file
14
test/integration/custom-server-types/test/index.test.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
/* eslint-env jest */
|
||||
/* global jasmine */
|
||||
import { join } from 'path'
|
||||
import { buildTS } from 'next-test-utils'
|
||||
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
|
||||
|
||||
const appDir = join(__dirname, '../')
|
||||
|
||||
describe('Custom Server TypeScript', () => {
|
||||
it('should build server.ts correctly', async () => {
|
||||
await buildTS([], appDir)
|
||||
})
|
||||
})
|
29
test/integration/custom-server-types/tsconfig.json
Normal file
29
test/integration/custom-server-types/tsconfig.json
Normal file
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"jsx": "preserve",
|
||||
"target": "es5",
|
||||
"lib": [
|
||||
"dom",
|
||||
"dom.iterable",
|
||||
"esnext"
|
||||
],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": false,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noEmit": true,
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
],
|
||||
"include": [
|
||||
"next-env.d.ts",
|
||||
"**/*.ts",
|
||||
"**/*.tsx"
|
||||
]
|
||||
}
|
|
@ -176,6 +176,32 @@ export function nextStart (dir, port, opts = {}) {
|
|||
return runNextCommandDev(['start', '-p', port, dir], undefined, opts)
|
||||
}
|
||||
|
||||
export function buildTS (args = [], cwd, env = {}) {
|
||||
cwd = cwd || path.dirname(require.resolve('next/package'))
|
||||
env = { ...process.env, NODE_ENV: undefined, ...env }
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const instance = spawn(
|
||||
'node',
|
||||
[require.resolve('typescript/lib/tsc'), ...args],
|
||||
{ cwd, env }
|
||||
)
|
||||
let output = ''
|
||||
|
||||
const handleData = chunk => {
|
||||
output += chunk.toString()
|
||||
}
|
||||
|
||||
instance.stdout.on('data', handleData)
|
||||
instance.stderr.on('data', handleData)
|
||||
|
||||
instance.on('exit', code => {
|
||||
if (code) { return reject(new Error('exited with code: ' + code + '\n' + output)) }
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Kill a launched app
|
||||
export async function killApp (instance) {
|
||||
await new Promise((resolve, reject) => {
|
||||
|
|
Loading…
Reference in a new issue