Commit 56a13440 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Debug TypeScript test suite (#8476)

* Tweak typescript test suite

* Revert "Tweak typescript test suite"

This reverts commit 9e0108da7c70e7ae45b412b60c07162383de65f3.

* Make sure HMR connection isn't being closed
while testing

* Update to not re-use NODE_ENV

* Break up/rework TypeScript test suite

* bump
parent bf114219
...@@ -493,6 +493,9 @@ export default async function getBaseWebpackConfig( ...@@ -493,6 +493,9 @@ export default async function getBaseWebpackConfig(
'process.env.NODE_ENV': JSON.stringify(webpackMode), 'process.env.NODE_ENV': JSON.stringify(webpackMode),
'process.crossOrigin': JSON.stringify(crossOrigin), 'process.crossOrigin': JSON.stringify(crossOrigin),
'process.browser': JSON.stringify(!isServer), 'process.browser': JSON.stringify(!isServer),
'process.env.__NEXT_TEST_MODE': JSON.stringify(
process.env.__NEXT_TEST_MODE
),
// This is used in client/dev-error-overlay/hot-dev-client.js to replace the dist directory // This is used in client/dev-error-overlay/hot-dev-client.js to replace the dist directory
...(dev && !isServer ...(dev && !isServer
? { ? {
......
...@@ -13,12 +13,15 @@ export default async ({ assetPrefix }) => { ...@@ -13,12 +13,15 @@ export default async ({ assetPrefix }) => {
setupPing(assetPrefix, () => Router.pathname, currentPage) setupPing(assetPrefix, () => Router.pathname, currentPage)
document.addEventListener('visibilitychange', event => { // prevent HMR connection from being closed when running tests
const state = document.visibilityState if (!process.env.__NEXT_TEST_MODE) {
if (state === 'visible') { document.addEventListener('visibilitychange', event => {
setupPing(assetPrefix, () => Router.pathname, true) const state = document.visibilityState
} else { if (state === 'visible') {
closePing() setupPing(assetPrefix, () => Router.pathname, true)
} } else {
}) closePing()
}
})
}
} }
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
}
}
export default () => <p>Hello World</p>
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
findPort,
launchApp,
killApp,
check,
getBrowserBodyText,
getReactErrorOverlayContent
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '..')
let appPort
let app
describe('TypeScript HMR', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
describe('delete a page and add it back', () => {
it('should detect the changes to typescript pages and display it', async () => {
let browser
try {
browser = await webdriver(appPort, '/hello')
await check(() => getBrowserBodyText(browser), /Hello World/)
const pagePath = join(appDir, 'pages/hello.tsx')
const originalContent = await fs.readFile(pagePath, 'utf8')
const editedContent = originalContent.replace('Hello', 'COOL page')
// change the content
await fs.writeFile(pagePath, editedContent, 'utf8')
await check(() => getBrowserBodyText(browser), /COOL page/)
// add the original content
await fs.writeFile(pagePath, originalContent, 'utf8')
await check(() => getBrowserBodyText(browser), /Hello World/)
} finally {
if (browser) {
await browser.close()
}
}
})
})
it('should recover from a type error', async () => {
let browser
const pagePath = join(appDir, 'pages/type-error-recover.tsx')
const origContent = await fs.readFile(pagePath, 'utf8')
try {
browser = await webdriver(appPort, '/type-error-recover')
const errContent = origContent.replace('() =>', '(): boolean =>')
await fs.writeFile(pagePath, errContent)
await check(
() => getReactErrorOverlayContent(browser),
/Type 'Element' is not assignable to type 'boolean'/
)
await fs.writeFile(pagePath, origContent)
await check(async () => {
const html = await browser.eval('document.documentElement.innerHTML')
return html.match(/iframe/) ? 'fail' : 'success'
}, /success/)
} finally {
if (browser) browser.close()
await fs.writeFile(pagePath, origContent)
}
})
})
{
"compilerOptions": {
"esModuleInterop": true,
"module": "esnext",
"jsx": "preserve",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
},
"exclude": ["node_modules"],
"include": ["next-env.d.ts", "pages"]
}
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import {
check,
getBrowserBodyText,
getReactErrorOverlayContent
} from 'next-test-utils'
export default (context, renderViaHTTP) => {
describe('Hot Module Reloading', () => {
describe('delete a page and add it back', () => {
it('should detect the changes to typescript pages and display it', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/hello')
await check(() => getBrowserBodyText(browser), /Hello World/)
const pagePath = join(context.appDir, 'components', 'hello.ts')
const originalContent = readFileSync(pagePath, 'utf8')
const editedContent = originalContent.replace('Hello', 'COOL page')
// change the content
writeFileSync(pagePath, editedContent, 'utf8')
await check(() => getBrowserBodyText(browser), /COOL page/)
// add the original content
writeFileSync(pagePath, originalContent, 'utf8')
await check(() => getBrowserBodyText(browser), /Hello World/)
} finally {
if (browser) {
await browser.close()
}
}
})
})
it('should recover from a type error', async () => {
let browser
const pagePath = join(context.appDir, 'pages/type-error-recover.tsx')
const origContent = readFileSync(pagePath, 'utf8')
try {
browser = await webdriver(context.appPort, '/type-error-recover')
const errContent = origContent.replace('() =>', '(): boolean =>')
writeFileSync(pagePath, errContent)
await check(
() => getReactErrorOverlayContent(browser),
/Type 'Element' is not assignable to type 'boolean'/
)
writeFileSync(pagePath, origContent)
await check(async () => {
const html = await browser.eval('document.documentElement.innerHTML')
return html.match(/iframe/) ? 'fail' : 'success'
}, /success/)
} finally {
if (browser) browser.close()
writeFileSync(pagePath, origContent)
}
})
})
}
/* eslint-env jest */ /* eslint-env jest */
/* global jasmine */ /* global jasmine */
import { join } from 'path' import { join } from 'path'
import { renderViaHTTP, findPort, launchApp, killApp } from 'next-test-utils' import cheerio from 'cheerio'
import {
renderViaHTTP,
nextBuild,
findPort,
launchApp,
killApp,
File
} from 'next-test-utils'
import hmr from './hmr' jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
import typescript from './typescript'
const context = {} const appDir = join(__dirname, '..')
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 let appPort
let app
async function get$ (path, query) {
const html = await renderViaHTTP(appPort, path, query)
return cheerio.load(html)
}
describe('TypeScript Features', () => { describe('TypeScript Features', () => {
beforeAll(async () => { describe('default behavior', () => {
context.appPort = await findPort() beforeAll(async () => {
context.appDir = join(__dirname, '../') appPort = await findPort()
context.server = await launchApp(context.appDir, context.appPort) app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
it('should render the page', async () => {
const $ = await get$('/hello')
expect($('body').text()).toMatch(/Hello World/)
})
})
// pre-build all pages at the start it('should compile the app', async () => {
await Promise.all([renderViaHTTP(context.appPort, '/hello')]) const output = await nextBuild(appDir, [], { stdout: true })
await Promise.all([renderViaHTTP(context.appPort, '/type-error-recover')]) expect(output.stdout).toMatch(/Compiled successfully/)
}) })
afterAll(() => killApp(context.server))
hmr(context, (p, q) => renderViaHTTP(context.appPort, p, q)) describe('should compile with different types', () => {
typescript(context, (p, q) => renderViaHTTP(context.appPort, p, q)) it('should compile async getInitialProps for _error', async () => {
const errorPage = new File(join(appDir, 'pages/_error.tsx'))
try {
errorPage.replace('static ', 'static async ')
const output = await nextBuild(appDir, [], { stdout: true })
expect(output.stdout).toMatch(/Compiled successfully/)
} finally {
errorPage.restore()
}
})
})
}) })
/* eslint-env jest */
import { join } from 'path'
import cheerio from 'cheerio'
import { runNextCommand, File } from 'next-test-utils'
const appDir = join(__dirname, '../')
export default (context, render) => {
async function get$ (path, query) {
const html = await render(path, query)
return cheerio.load(html)
}
describe('TypeScript', () => {
describe('default behavior', () => {
it('should render the page', async () => {
const $ = await get$('/hello')
expect($('body').text()).toMatch(/Hello World/)
})
it('should compile the app', async () => {
const output = await runNextCommand(['build', appDir], { stdout: true })
expect(output.stdout).toMatch(/Compiled successfully/)
})
})
describe('should compile with different types', () => {
const nextConfig = new File(join(appDir, 'pages/_error.tsx'))
beforeEach(() => {
nextConfig.replace('static ', 'static async ')
})
afterEach(() => {
nextConfig.restore()
})
it('should compile async getInitialProps for _error', async () => {
const output = await runNextCommand(['build', appDir], { stdout: true })
expect(output.stdout).toMatch(/Compiled successfully/)
})
})
})
}
...@@ -128,7 +128,12 @@ export function runNextCommand (argv, options = {}) { ...@@ -128,7 +128,12 @@ export function runNextCommand (argv, options = {}) {
export function runNextCommandDev (argv, stdOut, opts = {}) { export function runNextCommandDev (argv, stdOut, opts = {}) {
const cwd = path.dirname(require.resolve('next/package')) const cwd = path.dirname(require.resolve('next/package'))
const env = { ...process.env, NODE_ENV: undefined, ...opts.env } const env = {
...process.env,
NODE_ENV: undefined,
__NEXT_TEST_MODE: 'true',
...opts.env
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const instance = spawn('node', ['dist/bin/next', ...argv], { cwd, env }) const instance = spawn('node', ['dist/bin/next', ...argv], { cwd, env })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment