Js-lingui: Imported constants in <Trans> break Jest/Enzyme tests

Created on 3 Jun 2019  路  4Comments  路  Source: lingui/js-lingui

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 \.
image

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.

  • jsLingui version 2.8.3
  • Babel version [email protected]
  • Your Babel config (e.g. .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"
        }
      }
    ]
  ]
}
wontfix 馃悶bug 馃摝 macro

Most helpful comment

All 4 comments

Hey @ppozniak, that's really weird. Could you plase create a small repository with Jest, babel config and failing file?

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.)

To reproduce

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

Additional context ===

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:

  • @lingui/core: 2.9.0
  • @lingui/macro: 2.9.0
  • @lingui/react: 2.9.0
  • react-native: 0.61.4
  • @babel/core: 7.7.7

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/'],
        },
      },
    ],
  ],
};

Workaround

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.

Was this page helpful?
0 / 5 - 0 ratings