question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't mock library

See original GitHub issue

Describe your environment

  • React-Native version: 0.57.5
  • OS version: Mac OS 10.13.6 (17G65)
  • react-native-languages version: 3.0.1

How to repeat issue and example

After mocking

jest.mock('react-native-languages', () => ({
  RNLanguages: {
    language: 'en',
    languages: ['en'],
  },
}))

I had this error:

TypeError: Cannot set property 'locale' of undefined

      5 | I18n.locale = language
#I18n.ts
import { language } from 'react-native-languages'
import I18n from 'i18n-js'
import {ru, pl, uk, en} from './locales'

I18n.locale = language
....
export default I18n

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

17reactions
zoontekcommented, Mar 30, 2019

You can mock it like this:

jest.mock('react-native-localize', () => ({
  getLocales: () => [
    { countryCode: "GB", languageTag: "en-GB", languageCode: "en", isRTL: false },
    { countryCode: "US", languageTag: "en-US", languageCode: "en", isRTL: false },
    { countryCode: "FR", languageTag: "fr-FR", languageCode: "fr", isRTL: false },
  ],

  getNumberFormatSettings: () => ({
    decimalSeparator: ".",
    groupingSeparator: ",",
  }),

  getCalendar: () => "gregorian", // or "japanese", "buddhist"
  getCountry: () => "US", // the country code you want
  getCurrencies: () => ["USD", "EUR"], // can be empty array
  getTemperatureUnit: () => "celsius", // or "fahrenheit"
  getTimeZone: () => "Europe/Paris", // the timezone you want
  uses24HourClock: () => true,
  usesMetricSystem: () => true,

  addEventListener: jest.fn(),
  removeEventListener: jest.fn(),
}));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't mock library · Issue #24 · zoontek/react-native-localize
@stereodenis please tell me how to mock this library. In my jest setup file, i am having. jest.mock('react-native-languages', () => ({ RNLanguages: ...
Read more >
Jest mock not working with React Testing Library
I am using @testing-library/react for testing UI components. Cannot get jest mock working. It seems it cannot mock the implementation of the ...
Read more >
Manual Mocks - Jest
Manual mocks are used to stub out functionality with mock data. ... jest.mock calls cannot be hoisted to the top of the module...
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
Out of the box, Mockito cannot mock final classes and final or static methods, but if you really need it, Mockito 2 provides...
Read more >
How to mock a function in Jest for a React and Typescript app
import { getCharacter } from "./data"; test("Should render character name", async () => { const safe = getCharacter; // Cannot assign to ' ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found