Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(next): add tests for Node-like hashbang support #27906

Merged
merged 14 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions test/integration/hashbang/src/cases/cjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/env node

module.exports = 789
3 changes: 3 additions & 0 deletions test/integration/hashbang/src/cases/js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/env node

module.exports = 123
3 changes: 3 additions & 0 deletions test/integration/hashbang/src/cases/mjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/env node

export default 456
20 changes: 20 additions & 0 deletions test/integration/hashbang/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Import hashbang modules.
*/
import js from '../cases/js.js'
import cjs from '../cases/cjs.cjs'
import mjs from '../cases/mjs.mjs'

const jsMsg = `JS: ${js}`
const mjsMsg = `MJS: ${mjs}`
const cjsMsg = `CJS: ${cjs}`

const Page = () => (
<div>
<h3>{jsMsg}</h3>
<h3>{mjsMsg}</h3>
<h3>{cjsMsg}</h3>
</div>
)

export default Page
63 changes: 63 additions & 0 deletions test/integration/hashbang/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-env jest */

import { join } from 'path'
import fs from 'fs-extra'
import {
renderViaHTTP,
findPort,
launchApp,
killApp,
nextBuild,
nextStart,
} from 'next-test-utils'

jest.setTimeout(1000 * 60 * 2)

let app
let appPort
const appDir = join(__dirname, '../')

function runTests() {
describe('first-line hashbang (#!) parse', () => {
it('should work for .js files', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch('JS: 123')
})

it('should work for .mjs files', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch('MJS: 456')
})

it('should work for .cjs files', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch('CJS: 789')
})
})
}

const nextConfig = join(appDir, 'next.config.js')

describe('Hashbang', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))

runTests(true)
})

describe('production mode', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})
})