Closed aymkin closed 3 years ago
Hi @aymkin! For jest, simply treat it as a native dependency and mock it. For example:
jest.doMock('react-native-ultimate-config', () => {
return {
APP_NAME: "fooBar",
BUNDLE_ID: "com.foo.bar",
}
})
Hi @aymkin ,
there's nothing special about mocking this library. It can be mocked with jets's module mocks. Something as simple as:
jest.mock('react-native-ultimate-config', () => ({
APP_NAME: "fooBar",
BUNDLE_ID: "com.foo.bar",
}))
if you got more troubles with these - please consider sharing a reproducible example. thank you.
What I actually did
__mocks__
(sibling to node_modules
) I created a file as a library name, so I got __mocks__/react-native-ultimate-config.ts
import { ConfigVariables } from 'react-native-ultimate-config';
const ReactNativeUltimateConfig: ConfigVariables = { APP_NAME: 'foo', APP_VERSION: 'bar', BUNDLE_ID: 'baz', };
That is it, PROFIT!
To be honest I'm slightly confused with how this can work at all.
Seems like some typescript definitions are not 100% accurate.
There is no exported variable ConfigVariables
- there is only one default
export: https://github.com/maxkomarychev/react-native-ultimate-config/blob/51e133ee0f45f157fe82475a57c9ff0b7dd79fac/index.js#L1-L6
and the default export should be mocked, not ConfigVariables
.
that said I have never tried to mock it myself, I need to try it.
There are ConfigVaribles
in node_modules/react-native-ultimate-config/index.d.ts
yes, that's the problem - this not a variable that is exported. it's rather ephemeral variable which exists only in typescript but no in js
I added example of mock here https://github.com/maxkomarychev/react-native-ultimate-config/blob/master/packages/example/__tests__/App-test.js
One of the files has (translation setup) imports the RNUC. And the selection of the right translations is heavily relies on .env variables. During the runtime, it resolves properly:
But if I run the test
the output is
That means I need to mock the library somehow, and I did not found the proper instructions in README
Please guide me on how to implement that.
Thank you a lot
@isilher @maxkomarychev