Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Mocked Async storage test with jest fails, received null, what is correct way to test it?

$
0
0

Hello all I am new in testing, I am just trying to test async storage value, it's set to 25 as you see;

async value

This is test I have written as below;

test('can read asyncstorage', async () => {  let val = await AsyncStorage.getItem('isFirst');  expect(val).toBe('25');});

The error I am getting is like below;

expect(received).toBe(expected) // Object.is equality    Expected: "25"    Received: null

Did I just misunderstood the concept of writing tests? should I set 25 with mocked setItem function just before calling getItem? If I do that (as below), it passes the test, would it be a right approach?

test('can read asyncstorage', async () => {  await AsyncStorage.setItem('isSecond', '25');  let val = await AsyncStorage.getItem('isSecond');  expect(val).toBe('25');});

Or Is it right way to try to get real stored value and validate it?

This is how I mocked;

beforeAll(() => {  jest.doMock('@react-native-async-storage/async-storage', () => {    return {      getItem: async (...args: any) => args,      setItem: async (...args: any) => args,    };  });});

Thank you.

Versions I use

"typescript": "^3.9.10""@types/react-native": "^0.64.10","@types/react-test-renderer": "^16.9.5",

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>