Unverified Commit ab0b2cf3 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Add a test for module.exports from a page (#7842)

Closes https://github.com/zeit/next.js/issues/7835
parent c650dca3
...@@ -269,7 +269,7 @@ export function isPageStatic( ...@@ -269,7 +269,7 @@ export function isPageStatic(
try { try {
require('next-server/config').setConfig(runtimeEnvConfig) require('next-server/config').setConfig(runtimeEnvConfig)
const mod = require(serverBundle) const mod = require(serverBundle)
const Comp = mod.default const Comp = mod.default || mod
if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') { if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') {
throw new Error('INVALID_DEFAULT_EXPORT') throw new Error('INVALID_DEFAULT_EXPORT')
...@@ -293,7 +293,7 @@ export function hasCustomAppGetInitialProps( ...@@ -293,7 +293,7 @@ export function hasCustomAppGetInitialProps(
let mod = require(_appBundle) let mod = require(_appBundle)
if (_appBundle.endsWith('_app.js')) { if (_appBundle.endsWith('_app.js')) {
mod = mod.default mod = mod.default || mod
} else { } else {
// since we don't output _app in serverless mode get it from a page // since we don't output _app in serverless mode get it from a page
mod = mod._app mod = mod._app
......
import { useRouter } from 'next/router'
export default () => {
const { query } = useRouter()
return <p>post: {query.post}</p>
}
const React = require('react')
module.exports = function () {
return (
<div>
<h1>test1</h1>
</div>
)
}
const React = require('react')
module.exports = () => <div>test2</div>
/* eslint-env jest */
/* global jasmine */
import webdriver from 'next-webdriver'
import path from 'path'
import { nextBuild, nextStart, findPort, killApp } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const appDir = path.join(__dirname, '..')
let appPort
let app
describe('Auto Export', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
it('Supports commonjs 1', async () => {
const browser = await webdriver(appPort, '/commonjs1')
const html = await browser.eval('document.body.innerHTML')
expect(html).toMatch(/test1/)
expect(html).toMatch(/nextExport/)
await browser.close()
})
it('Supports commonjs 2', async () => {
const browser = await webdriver(appPort, '/commonjs2')
const html = await browser.eval('document.body.innerHTML')
expect(html).toMatch(/test2/)
expect(html).toMatch(/nextExport/)
await browser.close()
})
it('Refreshes query on mount', async () => {
const browser = await webdriver(appPort, '/post-1')
const html = await browser.eval('document.body.innerHTML')
expect(html).toMatch(/post.*post-1/)
expect(html).toMatch(/nextExport/)
})
afterAll(async () => {
await killApp(app)
})
})
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