5

I am new in Node/Jest world. I am trying to test the following function:

export async function visitPage(url: string) {
    const cookie =  ''
    let headers = {
    'authority': 'www.example.com',
    'pragma': 'no-cache',
    'cache-control': 'no-cache',
    'upgrade-insecure-requests': '1',
    'dnt': '1',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'rtt': '350',
    'downlink': '1.45',
    'ect': '3g',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'referer': 'https://google.com',
    'accept-language': 'en-US,en;q=0.9,ur;q=0.8,zh-CN;q=0.7,zh;q=0.6',    
    'Accept-Encoding': 'gzip',
    'cookie': cookie,

    }

    try {
        const response = await axios({
            method: 'get',
            url: url,
            headers: headers
        });
        if (response.status == 200) {
            let html = response.data
            if(html.includes('Hello, Sign in')) {
                // console.log('Not logged in')
                return false
              }
        }
    } catch(error) {
        if (error.response) {
            console.log(error.response.data);   
        }
    }
    
    return true;
}

In test I am doing like the below:

describe('Product', () => {
    it('visitPage', async () => {
        const pageData = await visitPage('https://example.com'); 
        // console.log(pageData)
        expect(pageData).toBe(false);
    });
});

The test gets passed but then it prints the following:

an all test suites.

Jest has detected the following 1 open handle potentially keeping Jest from exiting:



●  TLSWRAP

      31 |
      32 |     try {
    > 33 |         const response = await axios({
         |                                     ^
      34 |             method: 'get',
      35 |             url: url,
      36 |             headers: headers
Volatil3
  • 14,253
  • 38
  • 134
  • 263

1 Answers1

1

Use nextTick before axios call as stated in here

await process.nextTick(() => {})

That solves my issue.

Redd.o
  • 147
  • 2
  • 5