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

Jest won´t complete my async function for my api call function

$
0
0

I´m working on a small app that uses an API. I want to make a test to see that the function works as intended. The function works, but in the test, the function will not complete and therefore fail. I´ve looked here on stack overflow and on youtube for hours and I still do not get it to work.

Here is my test case:

import * as API from './geonames';test('Get the country code from sweden', async () => {    return API.getCountryCode("Sweden").then(res => console.log(res));}, 5000);

And here is my function:

export async function getCountryCode(country: string): Promise<any> {    // uses the ninja API to get the country code    var url = 'https://api.api-ninjas.com/v1/country?name='+ country;    try {        console.log("Before res"); // this runs in my test        let res = await fetch(url, { headers: {'X-Api-Key': API_NINJA_KEY}});        console.log("After res"); // this does not run in my test        let data = await res.json();        console.log("After data")        var countryCode = data[0]['iso2'];        return countryCode    } catch (e) {    }}

Viewing all articles
Browse latest Browse all 6287

Trending Articles



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