Describe the bug
Everything works as usual, however tests are failing, saying that variable is not defined.
This happens only when variable is used in \

All fine if constant is in the same file or is a class property etc.
To Reproduce
Steps to reproduce the behavior, possibly with minimal code sample, e.g:
import { Trans } from "@lingui/react"
import { SOME_CONSTANT } from './constants';
export default function App() {
return <Trans>Hello {SOME_CONSTANT}</Trans>
}
const wrapper = shallow(<App />);
Run tests.
Current workaround I found is to do:
import { SOME_CONSTANT } from './constants';
const _SOME_CONSTANT = SOME_CONSTANT;
...
<Trans>Hello {_SOME_CONSTANT}</Trans>
Expected behavior
It should not break
Additional context
Add any other context about the problem here.
2.8.3[email protected].babelrc) or framework you use (Create React App, Meteor, etc.){
"presets": ["@lingui/babel-preset-react", "@babel/preset-env", "@babel/preset-react"],
"plugins": [
"@babel/plugin-syntax-export-default-from",
"@babel/plugin-proposal-class-properties",
"babel-plugin-jsx-control-statements",
"macros",
"react-hot-loader/babel",
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"@pages": "./src/pages",
"@services": "./src/services",
"@sharedComponents": "./src/sharedComponents",
"@assets": "./src/assets"
}
}
]
]
}
Hey @ppozniak, that's really weird. Could you plase create a small repository with Jest, babel config and failing file?
@tricoder42
https://github.com/ppozniak/js-lingui-issue-514
Here you go.
I experienced a similar issue. We got ReferenceError: Can't find variable: CONTACT_PHONE_NUMBER, under similar circumstances. Except I saw it when running the application (compiled for "dev" mode) rather than during Jest tests. (We don't have a Jest test that executes this code path, so I don't know whether that would have worked or not.)
import {React} from 'react';
import {View, Text} from 'react-native';
import {Trans} from '@lingui/macro';
import {telephoneStyle} from 'src/styles';
import {CONTACT_PHONE_NUMBER} from 'src/contact-constants';
export const AreasScreen = () => {
return (
<View>
<Text>
<Trans>Please call{` `}
<Text style={telephoneStyle}>{CONTACT_PHONE_NUMBER}</Text>
{` `}to proceed.
</Trans>
</Text>
</View>
);
}
Expected behavior
The component displays the language string "Please call _{phone number}_ to proceed."
Actual behavior
When the application attempts to render this component, it crashes with an error message like this:
ReferenceError
Can't find variable: CONTACT_PHONE_NUMBER
This is a React native application, running on Android. We noticed this behavior when running the application both in "dev" mode (e.g. react-native start), and in prod mode (e.g. compiling to an APK). It didn't cause any problems at compile time, only at run time when the component in question actually attempted to render.
We have these library versions:
Babel config:
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
'macros',
[
'module-resolver',
{
root: ['./'],
extensions: ['.ios.js', '.android.js', '.js', '.ts', '.tsx', '.json'],
alias: {
src: ['./src/'],
},
},
],
],
};
We were able to work around the issue by assigning the imported constant to a local variable, and then using that inside the <Trans>:
import {React} from 'react';
import {View, Text} from 'react-native';
import {Trans} from '@lingui/macro';
import {telephoneStyle} from 'src/styles';
import {CONTACT_PHONE_NUMBER} from 'src/contact-constants';
export const AreasScreen = () => {
// WORKAROUND: assign imported phone number to a local variable
const phoneNumber = CONTACT_PHONE_NUMBER;
return (
<View>
<Text>
<Trans>Please call{` `}
<Text style={telephoneStyle}>{phoneNumber}</Text>
{` `}to proceed.
</Trans>
</Text>
</View>
);
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
@tricoder42
https://github.com/ppozniak/js-lingui-issue-514
Here you go.