Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables Interpolation not working properly when testing with Jest #2116

Open
psworld opened this issue Jan 14, 2024 · 1 comment
Open

Variables Interpolation not working properly when testing with Jest #2116

psworld opened this issue Jan 14, 2024 · 1 comment
Labels

Comments

@psworld
Copy link

psworld commented Jan 14, 2024

馃悰 Bug Report

variable interpolation not working when I test my react app with Jest.

To Reproduce

InputComp.tsx

import { useTranslation } from 'react-i18next';
const { t } = useTranslation();
return(
<>
{
  t("validationInputError", "Value must be between {{min}} and {{max}}", {
    min: 0,
    max: 20,
  });
}
</>
)

Jest test file

render(<InputComp />);
expect(
  screen.getByText(new RegExp(`value must be between 0 and 20`, "i")),
).toBeInTheDocument();

My .json

{
  "validationInputError": "Value must be between {{min}} and {{max}}",
}

Test is failing. Following is debug log I get from jest.

<label class="cds--label cds--label--no-margin invalidInputError">
  Value must be between {{ min }} and 20
</label>;

value of max is getting interpolated but not of min. Very weird !!!

If I remove the variables from translation and hardcode the values of min and max, the test passes successfully.

My InputComp.tsx component renders correctly in browser, i.e it shows the error message correctly in browser e.g value must be between 0 and 20 is rendered on the browser.

Expected behavior

Variables should get interpolated when testing with jest. In the above case test should pass and the screen.debug() should display

<label class="cds--label cds--label--no-margin invalidInputError">
  Value must be between 0 and 20
</label>;

and not {{min}}

Your Environment

  • runtime version: i.e. node v18.80
  • i18next version: 21.10.0
  • os: Windows
  • any other relevant information
  • jest: 29.7.0
@adrai
Copy link
Member

adrai commented Jan 14, 2024

Can you create a minimal reproducible example repository?

@adrai adrai added the stale label Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants