Home > Software design >  How to share global variables in jest test
How to share global variables in jest test

Time:10-04

I would like to use variable Shopresult in jest test. this variable will be defined in beforeAll section.

describe("test",()=>{
    
    let Shopresult:Shop;

    beforeAll(async()=>{
        await getConnection(LocalOrmconfig).then(async()=>{
          const Shopresult = await getRepository(Shop).save(mockData)
        })
    })
    afterAll(async()=>{
        await getConnection(LocalOrmconfig).then(async()=>{
            await getRepository(PricingPattern)
            .createQueryBuilder()
            .delete()
            .where({shopId:Shopresult.shopId})
            .execute()
        })
    })
    

But above code invoked following error.

'Shopresult' is declared but its value is never read.

What is the root cause of this? I declared them in let clause.

If someone has opinion,will you please let me know. thanks

CodePudding user response:

The problem is with this line. it is not using the Shopresult defined outside but creating a new variable. Remove the const keyword

const Shopresult = await getRepository(Shop).save(mockData)
  • Related