Javascript Promises in a loop behaves differently during jest test

Viewed 350

I have this function which has array map and a three level nested promise. Index.js

const promiseA=require('./promiseA')


console.log(' Starting ')
//setInterval(()=>console.log('executed'), 5000)

const sample=async ()=>{
    const newArray=[1,2,3,4,5]
    let result =undefined

    await Promise.all(newArray.map(async()=>{
        result = await promiseA();
        console.log('after calling A')
    }))
    console.log(`final result ${result}`)

    return result;
}
sample();
module.exports=sample

PromiseA

const promiseB=require('./promiseB')

const promiseA=async()=>{
    return Promise.resolve(await promiseB())
}

module.exports=promiseA

Promise B

const promiseC=require('./promiseC')


const promiseB=async()=>{
    return Promise.resolve(await promiseC())
}

module.exports=promiseB

Promise C

const promiseC=async()=>{
    return Promise.resolve([1,2,3])
}

module.exports=promiseC

When I did npm start I get the following output

 Starting 
after calling A
after calling A
after calling A
after calling A
after calling A
final result 1,2,3

The output is the same as the expected one.

Coming to jest.

index.test.js


const sample=require('../src/index')

describe('all tests', ()=>{

    it('should', async()=>{
        const result = await sample()
        expect(result).toBeDefined();
    })
})

when i did npm test, i get the following output

console.log
     Starting

      at Object.<anonymous> (src/index.js:7:9)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    final result 1,2,3

      at sample (src/index.js:33:13)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    after calling A

      at Promise.all.newArray.map (src/index.js:19:17)

  console.log
    final result 1,2,3

      at sample (src/index.js:33:13)

This is something I dont understand. The output is getting printed twice. Can someone please help. I have been stuck in a similar problem for the past 2 days.

1 Answers

index.js

const promiseA=require('./promiseA')


console.log(' Starting ')
//setInterval(()=>console.log('executed'), 5000)

const sample=async ()=>{
    const newArray=[1,2,3,4,5]
    let result =undefined

    await Promise.all(newArray.map(async()=>{
        result = await promiseA();
        console.log('after calling A')
    }))
    console.log(`final result ${result}`)

    return result;
}
sample(); // <- you are calling it here
module.exports=sample

index.test.js


const sample=require('../src/index')

describe('all tests', ()=>{

    it('should', async()=>{
        const result = await sample() // <- you are calling it here
        expect(result).toBeDefined();
    })
})

you are calling sample two times, once in index.js and once in index.test.js

Related