Skip to content

Commit

Permalink
type check tests (and convert next-test-utils to ts)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jun 9, 2023
1 parent 49b15df commit 9c65dcf
Show file tree
Hide file tree
Showing 27 changed files with 321 additions and 174 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"genstats": "cross-env LOCAL_STATS=true node .github/actions/next-stats-action/src/index.js",
"git-reset": "git reset --hard HEAD",
"git-clean": "git clean -d -x -e node_modules -e packages -f",
"typescript": "tsc --noEmit",
"lint-typescript": "turbo run typescript",
"lint-eslint": "eslint . --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.json --no-eslintrc",
"lint-no-typescript": "run-p prettier-check lint-eslint lint-language",
Expand Down Expand Up @@ -84,7 +85,10 @@
"@swc/helpers": "0.5.1",
"@testing-library/react": "13.0.0",
"@types/cheerio": "0.22.16",
"@types/cookie": "0.3.3",
"@types/cross-spawn": "6.0.0",
"@types/fs-extra": "8.1.0",
"@types/glob": "7.1.1",
"@types/html-validator": "5.0.3",
"@types/http-proxy": "1.17.3",
"@types/jest": "24.0.13",
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/lib/load-custom-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ function checkRedirect(route: Redirect): {
const invalidParts: string[] = []
let hadInvalidStatus: boolean = false

if (route.statusCode && !allowedStatusCodes.has(route.statusCode)) {
if (route.statusCode && !allowedStatusCodes.has(route['statusCode'])) {
hadInvalidStatus = true
invalidParts.push(`\`statusCode\` is not undefined or valid statusCode`)
}
if (typeof route.permanent !== 'boolean' && !route.statusCode) {
if (typeof route.permanent !== 'boolean' && !route['statusCode']) {
invalidParts.push(`\`permanent\` is not set to \`true\` or \`false\``)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/lib/patch-incorrect-lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function patchIncorrectLockfile(dir: string) {

const lockfileParsed = JSON.parse(content)
const lockfileVersion = parseInt(lockfileParsed?.lockfileVersion, 10)
const expectedSwcPkgs = Object.keys(nextPkgJson.optionalDependencies || {})
const expectedSwcPkgs = Object.keys(nextPkgJson['optionalDependencies'] || {})

const patchDependency = (
pkg: string,
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ createNextDescribe(

it('should support setting cookies in route handlers with the correct overrides', async () => {
const res = await next.fetch('/handler')
const setCookieHeader = res.headers.get('set-cookie') as string[]
const setCookieHeader = res.headers.get('set-cookie')
expect(setCookieHeader).toContain('bar=bar2; Path=/')
expect(setCookieHeader).toContain('baz=baz2; Path=/')
expect(setCookieHeader).toContain('foo=foo1; Path=/')
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app-dir/app-middleware/app-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path'
import cheerio from 'cheerio'
import { check, withQuery } from 'next-test-utils'
import { createNextDescribe, FileRef } from 'e2e-utils'
import type { Response } from 'node-fetch'

createNextDescribe(
'app-dir with middleware',
Expand Down
7 changes: 6 additions & 1 deletion test/e2e/app-dir/app-routes/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ type Cookies = {
* @returns any injected metadata on the request
*/
export function getRequestMeta(
headersOrCookies: Headers | Cookies | ReadonlyHeaders | ReadonlyRequestCookies
headersOrCookies:
| Headers
| import('node-fetch').Headers
| Cookies
| ReadonlyHeaders
| ReadonlyRequestCookies
): Record<string, any> {
const headerOrCookie = headersOrCookies.get(KEY)
if (!headerOrCookie) return {}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/app/standalone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ if (!(globalThis as any).isNextStart) {
/Listening on/,
{
...process.env,
PORT: appPort,
PORT: appPort.toString(),
},
undefined,
{
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/app-dir/set-cookies/set-cookies.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createNextDescribe } from 'e2e-utils'
import type { Response } from 'node-fetch'

import cookies, { nextConfigHeaders } from './cookies.mjs'

function getSetCookieHeaders(res: globalThis.Response): ReadonlyArray<string> {
function getSetCookieHeaders(res: Response): ReadonlyArray<string> {
return (
(res.headers as any).getSetCookie?.() ??
(res.headers as any).raw()['set-cookie']
Expand Down
1 change: 1 addition & 0 deletions test/e2e/edge-can-read-request-body/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
import FormData from 'form-data'
import path from 'path'
import type { Response } from 'node-fetch'

async function serialize(response: Response) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
import { createNext, FileRef } from 'e2e-utils'
import cheerio from 'cheerio'
import type { Response } from 'node-fetch'

describe('Middleware Request Headers Overrides', () => {
let next: NextInstance
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/next-font/with-proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { FileRef, createNext, NextInstance } from 'e2e-utils'
import { findPort, renderViaHTTP, fetchViaHTTP } from 'next-test-utils'
import { join } from 'path'
import { spawn } from 'cross-spawn'
import spawn from 'cross-spawn'

describe('next/font/google with proxy', () => {
let next: NextInstance
let proxy: any
let PROXY_PORT: number
let SERVER_PORT: number
let PROXY_PORT: string
let SERVER_PORT: string

if ((global as any).isNextDeploy) {
it('should skip next deploy', () => {})
return
}

beforeAll(async () => {
PROXY_PORT = await findPort()
SERVER_PORT = await findPort()
PROXY_PORT = await findPort().toString()
SERVER_PORT = await findPort().toString()

proxy = spawn('node', [require.resolve('./with-proxy/server.js')], {
stdio: 'inherit',
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/prerender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1850,8 +1850,8 @@ describe('Prerender', () => {
previewRes.headers
.get('set-cookie')
.split(',')
.forEach((c) => {
c = cookie.parse(c)
.forEach((s) => {
const c = cookie.parse(s)
const isBypass = c.__prerender_bypass

if (isBypass || c.__next_preview_data) {
Expand Down
1 change: 1 addition & 0 deletions test/integration/config-output-export/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import webdriver from 'next-webdriver'
import { join } from 'path'
import fs from 'fs'
import type { Response } from 'node-fetch'

const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js'))
Expand Down
11 changes: 7 additions & 4 deletions test/integration/draft-mode/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ describe('Test Draft Mode', () => {
const res = await fetchViaHTTP(appPort, '/api/enable')
expect(res.status).toBe(200)

const cookies = res.headers.get('set-cookie').split(',').map(cookie.parse)
const cookies = res.headers
.get('set-cookie')
.split(',')
.map((c) => cookie.parse(c))

expect(cookies[0]).toBeTruthy()
expect(cookies[0].__prerender_bypass).toBeTruthy()
Expand Down Expand Up @@ -76,7 +79,7 @@ describe('Test Draft Mode', () => {
.get('set-cookie')
.replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',')
.map(cookie.parse)
.map((c) => cookie.parse(c))

expect(cookies[0]).toBeTruthy()
})
Expand Down Expand Up @@ -154,7 +157,7 @@ describe('Test Draft Mode', () => {
expect(res.status).toBe(200)

const originalCookies = res.headers.get('set-cookie').split(',')
const cookies = originalCookies.map(cookie.parse)
const cookies = originalCookies.map((c) => cookie.parse(c))

expect(cookies.length).toBe(1)
expect(cookies[0]).toBeTruthy()
Expand Down Expand Up @@ -212,7 +215,7 @@ describe('Test Draft Mode', () => {
.get('set-cookie')
.replace(/(=(?!Lax)\w{3}),/g, '$1')
.split(',')
.map(cookie.parse)
.map((c) => cookie.parse(c))

expect(cookies[0]).toBeTruthy()
expect(cookies[0]).toMatchObject({
Expand Down
10 changes: 9 additions & 1 deletion test/integration/draft-mode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
"jsx": "preserve",
"baseUrl": "../../..",
"paths": {
"development-sandbox": ["test/lib/development-sandbox"],
"next-test-utils": ["test/lib/next-test-utils"],
"amp-test-utils": ["test/lib/amp-test-utils"],
"next-webdriver": ["test/lib/next-webdriver"],
"e2e-utils": ["test/lib/e2e-utils"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('dev mode', () => {
context.appPort = await findPort()
context.app = await launchApp(appDir, context.appPort, {
...context.handler,
env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
env: { __NEXT_TEST_WITH_DEVTOOL: '1' },
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe.each([
output = ''
appPort = await findPort()
app = await launchApp(appDir, appPort, {
env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
env: { __NEXT_TEST_WITH_DEVTOOL: '1' },
onStdout(msg) {
output += msg
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('TypeScript Image Component', () => {
nextConfig,
content.replace('// disableStaticImages', 'disableStaticImages')
)
const app = await launchApp(appDir, await findPort(), [])
const app = await launchApp(appDir, await findPort())
await killApp(app)
await fs.writeFile(nextConfig, content)
const envTypes = await fs.readFile(join(appDir, 'next-env.d.ts'), 'utf8')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('TypeScript Image Component', () => {
nextConfig,
content.replace('// disableStaticImages', 'disableStaticImages')
)
const app = await launchApp(appDir, await findPort(), [])
const app = await launchApp(appDir, await findPort())
await killApp(app)
await fs.writeFile(nextConfig, content)
const envTypes = await fs.readFile(join(appDir, 'next-env.d.ts'), 'utf8')
Expand Down
28 changes: 27 additions & 1 deletion test/jest.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
import 'jest-extended'
/// <reference types="jest" />
/// <reference types="jest-extended" />

declare namespace jest {
// https://github.com/jestjs/jest/blob/6460335f88cee3dcb9d29c49d55ab02b9d83f994/packages/expect/src/types.ts#L58-L72
interface MatcherState {
assertionCalls: number
currentTestName?: string
error?: Error
expand?: boolean
expectedAssertionsNumber: number | null
expectedAssertionsNumberError?: Error
isExpectingAssertions: boolean
isExpectingAssertionsError?: Error
isNot?: boolean
numPassingAsserts: number
promise?: string
suppressedErrors: Array<Error>
testPath?: string
}

interface Expect {
// https://github.com/jestjs/jest/blob/6460335f88cee3dcb9d29c49d55ab02b9d83f994/packages/expect/src/index.ts#L461
// https://github.com/jestjs/jest/blob/6460335f88cee3dcb9d29c49d55ab02b9d83f994/packages/expect/src/jestMatchersObject.ts#L44-L45
getState(): MatcherState
}
}
2 changes: 1 addition & 1 deletion test/lib/next-modes/next-dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { spawn } from 'cross-spawn'
import spawn from 'cross-spawn'
import { Span } from 'next/src/trace'
import { NextInstance } from './base'

Expand Down
4 changes: 2 additions & 2 deletions test/lib/next-modes/next-start.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from 'path'
import fs from 'fs-extra'
import { NextInstance } from './base'
import { spawn, SpawnOptions } from 'cross-spawn'
import spawn from 'cross-spawn'
import { Span } from 'next/src/trace'

export class NextStartInstance extends NextInstance {
private _buildId: string
private _cliOutput: string = ''
private spawnOpts: SpawnOptions
private spawnOpts: import('child_process').SpawnOptions

public get buildId() {
return this._buildId
Expand Down
Loading

0 comments on commit 9c65dcf

Please sign in to comment.