Unverified Commit 963d598d authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add tests for optional chaining and nullish coalescing (#9665)

* Add tests for optional chaining and nullish coalescing

* Add integration tests
parent 5fe7dcde
......@@ -9,6 +9,7 @@
"dev": "lerna run build --stream --parallel",
"dev2": "while true; do yarn --check-files && yarn dev; done",
"testonly": "jest --runInBand",
"testheadless": "HEADLESS=true yarn testonly",
"testall": "yarn run testonly -- --ci --forceExit",
"pretest": "yarn run lint",
"git-reset": "git reset --hard HEAD",
......
......@@ -95,7 +95,7 @@ module.exports = (
}
}
// spefify a preset to use instead of @babel/preset-env:
// specify a preset to use instead of @babel/preset-env:
const customModernPreset =
isLaxModern && options['experimental-modern-preset']
......
let hello
let another = ''
export default () => (
<>
<p>result1: {hello ?? 'fallback'}</p>
<p>result2: {another ?? 'fallback'}</p>
</>
)
let hello
let another = { thing: 1 }
export default () => (
<>
<p>result1: {hello?.world ? 'something' : 'nothing'}</p>
<p>result2: {another?.thing ? 'something' : 'nothing'}</p>
</>
)
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import {
renderViaHTTP,
launchApp,
nextBuild,
nextStart,
killApp,
findPort,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app
const runTests = () => {
it('should support optional chaining', async () => {
const html = await renderViaHTTP(appPort, '/optional-chaining')
expect(html).toMatch(/result1:.*?nothing/)
expect(html).toMatch(/result2:.*?something/)
})
it('should support nullish coalescing', async () => {
const html = await renderViaHTTP(appPort, '/nullish-coalescing')
expect(html).toMatch(/result1:.*?fallback/)
expect(html).not.toMatch(/result2:.*?fallback/)
})
}
describe('Optional chaining and nullish coalescing support', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('server mode', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})
runTests()
})
})
......@@ -221,6 +221,7 @@ describe('next-babel-loader', () => {
})
const pageFile = path.resolve(dir, 'pages', 'index.js')
const tsPageFile = pageFile.replace(/\.js$/, '.ts')
it('should not drop unused exports by default in a page', async () => {
const code = await babel(
......@@ -327,5 +328,75 @@ describe('next-babel-loader', () => {
`"var __jsx=React.createElement;import\\"core-js\\";import{bar}from\\"a\\";import baz from\\"b\\";import*as React from\\"react\\";import{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee}from\\"f\\";const __NEXT_COMP=function(){return __jsx(\\"div\\",null,cats+bar());};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
)
})
it('should support optional chaining for JS file', async () => {
const code = await babel(
`let hello;` +
`export default () => hello?.world ? 'something' : 'nothing' `,
{
resourcePath: pageFile,
}
)
expect(code).toMatchInlineSnapshot(
`"var hello;export default(function(){return(hello===null||hello===void 0?void 0:hello.world)?'something':'nothing';});"`
)
})
it('should support optional chaining for TS file', async () => {
const code = await babel(
`let hello;` +
`export default () => hello?.world ? 'something' : 'nothing' `,
{
resourcePath: tsPageFile,
}
)
expect(code).toMatchInlineSnapshot(
`"var hello;export default(function(){return(hello===null||hello===void 0?void 0:hello.world)?'something':'nothing';});"`
)
})
it('should support nullish coalescing for JS file', async () => {
const code = await babel(
`const res = {
status: 0,
nullVal: null,
statusText: '',
}
const status = res.status ?? 999
const nullVal = res.nullVal ?? 'another'
const statusText = res.nullVal ?? 'not found'
export default () => 'hello'
`,
{
resourcePath: pageFile,
}
)
expect(code).toMatchInlineSnapshot(
`"var _res$status,_res$nullVal,_res$nullVal2;var res={status:0,nullVal:null,statusText:''};var status=(_res$status=res.status)!==null&&_res$status!==void 0?_res$status:999;var nullVal=(_res$nullVal=res.nullVal)!==null&&_res$nullVal!==void 0?_res$nullVal:'another';var statusText=(_res$nullVal2=res.nullVal)!==null&&_res$nullVal2!==void 0?_res$nullVal2:'not found';export default(function(){return'hello';});"`
)
})
it('should support nullish coalescing for TS file', async () => {
const code = await babel(
`const res = {
status: 0,
nullVal: null,
statusText: '',
}
const status = res.status ?? 999
const nullVal = res.nullVal ?? 'another'
const statusText = res.nullVal ?? 'not found'
export default () => 'hello'
`,
{
resourcePath: tsPageFile,
}
)
expect(code).toMatchInlineSnapshot(
`"var _res$status,_res$nullVal,_res$nullVal2;var res={status:0,nullVal:null,statusText:''};var status=(_res$status=res.status)!==null&&_res$status!==void 0?_res$status:999;var nullVal=(_res$nullVal=res.nullVal)!==null&&_res$nullVal!==void 0?_res$nullVal:'another';var statusText=(_res$nullVal2=res.nullVal)!==null&&_res$nullVal2!==void 0?_res$nullVal2:'not found';export default(function(){return'hello';});"`
)
})
})
})
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