Skip to content

Commit

Permalink
Add hydration callback for testing (#10195)
Browse files Browse the repository at this point in the history
* Add hydration callback for testing

* Update size-limit test
  • Loading branch information
ijjk authored and Timer committed Jan 21, 2020
1 parent fa65442 commit 53e9983
Show file tree
Hide file tree
Showing 33 changed files with 217 additions and 208 deletions.
8 changes: 8 additions & 0 deletions packages/next/client/index.js
Expand Up @@ -114,6 +114,14 @@ class Container extends React.Component {
}
)
}

if (process.env.__NEXT_TEST_MODE) {
window.__NEXT_HYDRATED = true

if (window.__NEXT_HYDRATED_CB) {
window.__NEXT_HYDRATED_CB()
}
}
}

componentDidUpdate() {
Expand Down
4 changes: 0 additions & 4 deletions test/integration/auto-export/test/index.test.js
Expand Up @@ -8,7 +8,6 @@ import {
findPort,
killApp,
launchApp,
waitFor,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
Expand Down Expand Up @@ -42,14 +41,12 @@ const runTests = () => {

it('should update asPath after mount', async () => {
const browser = await webdriver(appPort, '/zeit/cmnt-2')
await waitFor(500)
const html = await browser.eval(`document.documentElement.innerHTML`)
expect(html).toMatch(/\/zeit\/cmnt-2/)
})

it('should not replace URL with page name while asPath is delayed', async () => {
const browser = await webdriver(appPort, '/zeit/cmnt-1')
await waitFor(500)
const val = await browser.eval(`!!window.pathnames.find(function(p) {
return p !== '/zeit/cmnt-1'
})`)
Expand Down Expand Up @@ -84,7 +81,6 @@ describe('Auto Export', () => {

it('should not show hydration warning from mismatching asPath', async () => {
const browser = await webdriver(appPort, '/zeit/cmnt-1')
await waitFor(500)

const numCaught = await browser.eval(`window.caughtWarns.length`)
expect(numCaught).toBe(0)
Expand Down
10 changes: 1 addition & 9 deletions test/integration/chunking/test/index.test.js
Expand Up @@ -3,13 +3,7 @@
import { join } from 'path'
import express from 'express'
import http from 'http'
import {
nextBuild,
waitFor,
nextServer,
promiseCall,
stopApp,
} from 'next-test-utils'
import { nextBuild, nextServer, promiseCall, stopApp } from 'next-test-utils'
import { readdir, readFile, unlink, access } from 'fs-extra'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
Expand Down Expand Up @@ -134,7 +128,6 @@ describe('Chunking', () => {

it('should hydrate with granularChunks config', async () => {
const browser = await webdriver(appPort, '/page2')
await waitFor(1000)
const text = await browser.elementByCss('#padded-str').text()

expect(text).toBe('__rad__')
Expand All @@ -144,7 +137,6 @@ describe('Chunking', () => {

it('should load chunks when navigating', async () => {
const browser = await webdriver(appPort, '/page3')
await waitFor(1000)
const text = await browser
.elementByCss('#page2-link')
.click()
Expand Down
2 changes: 0 additions & 2 deletions test/integration/config/test/client.js
Expand Up @@ -8,8 +8,6 @@ export default (context, render) => {
describe('Configuration', () => {
it('should have config available on the client', async () => {
const browser = await webdriver(context.appPort, '/next-config')
// Wait for client side to load
await waitFor(10000)

const serverText = await browser.elementByCss('#server-only').text()
const serverClientText = await browser
Expand Down
Expand Up @@ -7,7 +7,6 @@ import {
findPort,
killApp,
renderViaHTTP,
waitFor,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
Expand All @@ -25,7 +24,6 @@ describe('Errors on conflict between public file and page file', () => {
/A conflicting public file and page file was found for path/
)
}
await waitFor(1000)
await killApp(app)
})

Expand Down
4 changes: 0 additions & 4 deletions test/integration/css-client-nav/test/index.test.js
Expand Up @@ -5,7 +5,6 @@ import { remove } from 'fs-extra'
import {
nextBuild,
nextStart,
waitFor,
findPort,
killApp,
renderViaHTTP,
Expand Down Expand Up @@ -80,9 +79,6 @@ describe('CSS Module client-side navigation in Production', () => {
let browser
try {
browser = await webdriver(appPort, '/blue')

await waitFor(2000) // Ensure hydration

await browser.eval(`window.__did_not_ssr = 'make sure this is set'`)

const redColor = await browser.eval(
Expand Down
1 change: 0 additions & 1 deletion test/integration/css-modules/test/index.test.js
Expand Up @@ -178,7 +178,6 @@ xdescribe('Can hot reload CSS Module without losing state', () => {
// FIXME: this is broken
it('should update CSS color without remounting <input>', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(2000) // ensure application hydrates

const desiredText = 'hello world'
await browser.elementById('text-input').type(desiredText)
Expand Down
3 changes: 0 additions & 3 deletions test/integration/css/test/index.test.js
Expand Up @@ -304,7 +304,6 @@ describe('CSS Support', () => {
let browser
try {
browser = await webdriver(appPort, '/page1')
await waitFor(2000) // ensure application hydrates

const desiredText = 'hello world'
await browser.elementById('text-input').type(desiredText)
Expand Down Expand Up @@ -703,7 +702,6 @@ describe('CSS Support', () => {

it('should have the correct color (css ordering)', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(2000) // ensure application hydrates

const currentColor = await browser.eval(
`window.getComputedStyle(document.querySelector('.my-text')).color`
Expand All @@ -729,7 +727,6 @@ describe('CSS Support', () => {

it('should have the correct color (css ordering)', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(2000) // ensure application hydrates

const currentColor = await browser.eval(
`window.getComputedStyle(document.querySelector('.my-text')).color`
Expand Down
2 changes: 0 additions & 2 deletions test/integration/duplicate-pages/test/index.test.js
Expand Up @@ -8,7 +8,6 @@ import {
launchApp,
renderViaHTTP,
killApp,
waitFor,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
Expand All @@ -34,7 +33,6 @@ describe('Handles Duplicate Pages', () => {
onStderr: handleOutput,
})
await renderViaHTTP(appPort, '/hello')
await waitFor(3000)
await killApp(app)
expect(output).toMatch(/Duplicate page detected/)
})
Expand Down
4 changes: 0 additions & 4 deletions test/integration/dynamic-routing/test/index.test.js
Expand Up @@ -304,7 +304,6 @@ function runTests(dev) {
expect(html).toMatch(/onmpost:.*pending/)

const browser = await webdriver(appPort, '/on-mount/post-1')
await waitFor(1000)
const text = await browser.eval(`document.body.innerHTML`)
expect(text).toMatch(/onmpost:.*post-1/)
})
Expand All @@ -316,14 +315,12 @@ function runTests(dev) {

it('should update with a hash in the URL', async () => {
const browser = await webdriver(appPort, '/on-mount/post-1#abc')
await waitFor(1000)
const text = await browser.eval(`document.body.innerHTML`)
expect(text).toMatch(/onmpost:.*post-1/)
})

it('should scroll to a hash on mount', async () => {
const browser = await webdriver(appPort, '/on-mount/post-1#item-400')
await waitFor(1000)

const text = await browser.eval(`document.body.innerHTML`)
expect(text).toMatch(/onmpost:.*post-1/)
Expand All @@ -334,7 +331,6 @@ function runTests(dev) {

it('should scroll to a hash on client-side navigation', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(1000)
await browser.elementByCss('#view-dynamic-with-hash').click()
await browser.waitForElementByCss('p')

Expand Down
Expand Up @@ -49,7 +49,6 @@ describe('Empty Project', () => {

it('should show empty object warning during client transition', async () => {
const browser = await webdriver(appPort, '/static')
await waitFor(1000)
await browser.eval(`(function() {
window.gotWarn = false
const origWarn = console.warn
Expand Down
Expand Up @@ -9,7 +9,6 @@ import {
startCleanStaticServer,
stopApp,
renderViaHTTP,
waitFor,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60
Expand Down Expand Up @@ -41,7 +40,6 @@ describe('Export Dyanmic Pages', () => {
expect.assertions(1)
const browser = await webdriver(port, '/regression/jeff-is-cool')
try {
await waitFor(3000)
expect(await browser.eval(`window.__AS_PATHS`)).toEqual([
'/regression/jeff-is-cool',
])
Expand Down
2 changes: 0 additions & 2 deletions test/integration/export-dynamic-pages/test/index.test.js
Expand Up @@ -9,7 +9,6 @@ import {
startCleanStaticServer,
stopApp,
renderViaHTTP,
waitFor,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60
Expand Down Expand Up @@ -41,7 +40,6 @@ describe('Export Dyanmic Pages', () => {
expect.assertions(1)
const browser = await webdriver(port, '/regression/jeff-is-cool')
try {
await waitFor(3000)
expect(await browser.eval(`window.__AS_PATHS`)).toEqual([
'/regression/jeff-is-cool',
])
Expand Down
3 changes: 1 addition & 2 deletions test/integration/export-serverless/test/browser.js
@@ -1,6 +1,6 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { check, waitFor, getBrowserBodyText } from 'next-test-utils'
import { check, getBrowserBodyText } from 'next-test-utils'

export default function(context) {
describe('Render via browser', () => {
Expand Down Expand Up @@ -184,7 +184,6 @@ export default function(context) {

it('should update query after mount', async () => {
const browser = await webdriver(context.port, '/query-update?hello=world')
await waitFor(2000)
const query = await browser.elementByCss('#query').text()
expect(JSON.parse(query)).toEqual({ hello: 'world', a: 'blue' })
await browser.close()
Expand Down
3 changes: 1 addition & 2 deletions test/integration/export/test/browser.js
@@ -1,6 +1,6 @@
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { check, waitFor, getBrowserBodyText } from 'next-test-utils'
import { check, getBrowserBodyText } from 'next-test-utils'

export default function(context) {
describe('Render via browser', () => {
Expand Down Expand Up @@ -191,7 +191,6 @@ export default function(context) {

it('should update query after mount', async () => {
const browser = await webdriver(context.port, '/query-update?hello=world')
await waitFor(2000)
const query = await browser.elementByCss('#query').text()
expect(JSON.parse(query)).toEqual({ hello: 'world', a: 'blue' })
await browser.close()
Expand Down
2 changes: 0 additions & 2 deletions test/integration/future/test/index.test.js
Expand Up @@ -8,7 +8,6 @@ import {
startApp,
stopApp,
renderViaHTTP,
waitFor,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
Expand All @@ -34,7 +33,6 @@ describe('future.excludeDefaultMomentLocales', () => {

it('should load momentjs', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(5000)
expect(await browser.elementByCss('h1').text()).toMatch(/current time/i)
const locales = await browser.eval('moment.locales()')
expect(locales).toEqual(['en'])
Expand Down
9 changes: 1 addition & 8 deletions test/integration/hydration/test/index.test.js
Expand Up @@ -3,13 +3,7 @@
import path from 'path'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import {
nextBuild,
nextStart,
findPort,
waitFor,
killApp,
} from 'next-test-utils'
import { nextBuild, nextStart, findPort, killApp } from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const appDir = path.join(__dirname, '..')
Expand All @@ -27,7 +21,6 @@ describe('Hydration', () => {

it('Hydrates correctly', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(2000)
expect(await browser.eval('window.didHydrate')).toBe(true)
})
})
4 changes: 1 addition & 3 deletions test/integration/initial-ref/test/index.test.js
Expand Up @@ -7,7 +7,6 @@ import {
nextStart,
launchApp,
findPort,
waitFor,
killApp,
} from 'next-test-utils'

Expand All @@ -19,12 +18,11 @@ let appPort
const runTest = () => {
it('Has correct initial ref values', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(2000)
expect(await browser.elementByCss('#ref-val').text()).toContain('76px')
})
}

describe('Hydration', () => {
describe('Initial Refs', () => {
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
Expand Down
1 change: 0 additions & 1 deletion test/integration/invalid-href/test/index.test.js
Expand Up @@ -64,7 +64,6 @@ const showsError = async (

const noError = async (pathname, click = false) => {
const browser = await webdriver(appPort, '/')
await waitFor(2000)
await browser.eval(`(function() {
window.caughtErrors = []
window.addEventListener('error', function (error) {
Expand Down
1 change: 0 additions & 1 deletion test/integration/link-ref/test/index.test.js
Expand Up @@ -35,7 +35,6 @@ const noError = async pathname => {

const didPrefetch = async pathname => {
const browser = await webdriver(appPort, pathname)
await waitFor(500)
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

Expand Down
2 changes: 0 additions & 2 deletions test/integration/next-dynamic/test/index.test.js
Expand Up @@ -7,7 +7,6 @@ import {
findPort,
launchApp,
killApp,
waitFor,
runNextCommand,
nextServer,
startApp,
Expand All @@ -29,7 +28,6 @@ function runTests() {

it('should render dynamic server rendered values on client mount', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(5000)
const text = await browser.elementByCss('#first-render').text()

// Failure case is 'Index<!-- -->3<!-- --><!-- -->'
Expand Down
3 changes: 0 additions & 3 deletions test/integration/next-plugins/test/index.test.js
Expand Up @@ -8,7 +8,6 @@ import {
findPort,
launchApp,
killApp,
waitFor,
nextBuild,
nextStart,
renderViaHTTP,
Expand Down Expand Up @@ -57,7 +56,6 @@ function runTests() {

it('should call clientInit from plugin correctly', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(250)
expect(await browser.eval('window.didClientInit')).toBe(true)
})

Expand Down Expand Up @@ -125,7 +123,6 @@ describe('Next.js plugins', () => {

it('should expose a plugins config', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(500)
expect(await browser.eval('window.initClientConfig')).toBe('world')
})
})
Expand Down

0 comments on commit 53e9983

Please sign in to comment.