Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(babel): add more language tests (#27709)
# Why Add tests to ensure our babel preset can parse these language features that are used in native-component-list. This will help us with refactors and additions to the babel preset. --------- Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/babel-preset-expo/src/__tests__/__snapshots__/compiler.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`allows destructuring in the catch block 1`] = ` | ||
"Object.defineProperty(exports, "__esModule", { value: true });exports.App = App;var _jsxRuntime = require("react/jsx-runtime");function App() { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
var foo = function () { | ||
try { | ||
console.log('hey'); | ||
} catch (_ref) {var message = _ref.message; | ||
// NOTE(EvanBacon): React compiler throws: | ||
// (BuildHIR::lowerAssignment) Could not find binding for declaration. | ||
console.error(message); | ||
} | ||
}; | ||
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { children: "Hello" }); | ||
}" | ||
`; | ||
|
||
exports[`supports functions with discarded return values in try/catch blocks that run in memos 1`] = ` | ||
"Object.defineProperty(exports, "__esModule", { value: true });exports.useSideEffectMayThrow = useSideEffectMayThrow;var _react = require("react"); | ||
function SIDE_EFFECT_MAY_THROW() {} | ||
// NOTE(EvanBacon): React compiler throws: | ||
// Cannot read properties of undefined (reading 'preds') | ||
function useSideEffectMayThrow() { | ||
return (0, _react.useMemo)(function () { | ||
try { | ||
SIDE_EFFECT_MAY_THROW(); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}, []); | ||
}" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as babel from '@babel/core'; | ||
import * as path from 'node:path'; | ||
|
||
import preset from '..'; | ||
|
||
function getCaller(props: Record<string, string | boolean>): babel.TransformCaller { | ||
return props as unknown as babel.TransformCaller; | ||
} | ||
|
||
const options = { | ||
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'ios', isDev: false }), | ||
babelrc: false, | ||
presets: [preset], | ||
sourceMaps: false, | ||
|
||
compact: false, | ||
comments: true, | ||
retainLines: true, | ||
}; | ||
|
||
it(`allows destructuring in the catch block`, () => { | ||
// Ensuring the transform doesn't throw. | ||
const { code } = babel.transformFileSync( | ||
path.resolve(__dirname, 'samples/destructure-catch.tsx'), | ||
options | ||
)!; | ||
|
||
expect(code).toMatchSnapshot(); | ||
}); | ||
it(`supports functions with discarded return values in try/catch blocks that run in memos`, () => { | ||
// Ensuring the transform doesn't throw. | ||
const { code } = babel.transformFileSync( | ||
path.resolve(__dirname, 'samples/try-catch-hook.tsx'), | ||
options | ||
)!; | ||
|
||
expect(code).toMatchSnapshot(); | ||
}); |
13 changes: 13 additions & 0 deletions
13
packages/babel-preset-expo/src/__tests__/samples/destructure-catch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function App() { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const foo = () => { | ||
try { | ||
console.log('hey'); | ||
} catch ({ message }) { | ||
// NOTE(EvanBacon): React compiler throws: | ||
// (BuildHIR::lowerAssignment) Could not find binding for declaration. | ||
console.error(message); | ||
} | ||
}; | ||
return <div>Hello</div>; | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/babel-preset-expo/src/__tests__/samples/try-catch-hook.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useMemo } from 'react'; | ||
|
||
function SIDE_EFFECT_MAY_THROW() {} | ||
|
||
// NOTE(EvanBacon): React compiler throws: | ||
// Cannot read properties of undefined (reading 'preds') | ||
export function useSideEffectMayThrow() { | ||
return useMemo(() => { | ||
try { | ||
SIDE_EFFECT_MAY_THROW(); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}, []); | ||
} |