Skip to content

Commit

Permalink
test(next): add tests for Node-like hashbang support (#27906)
Browse files Browse the repository at this point in the history
* update to webpack 5.50.0

* feat: use shebang loader shim for now

* chore: snapshot formatting change

* chore: remove unneeded SSR test

* chore: test all file extensions

* chore: remove shebang loader from Webpack config

* chore: revert unnecessary changes

* Update test/integration/hashbang/test/index.test.js

Co-authored-by: Steven <steven@ceriously.com>

* chore: revert changes to yarn.lock

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
3 people committed Aug 11, 2021
1 parent 12eb812 commit 3c837ed
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/integration/hashbang/src/cases/cjs.cjs
@@ -0,0 +1,3 @@
#!/usr/env node

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

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

export default 456
20 changes: 20 additions & 0 deletions test/integration/hashbang/src/pages/index.js
@@ -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
@@ -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()
})
})

0 comments on commit 3c837ed

Please sign in to comment.