Home > Enterprise >  JEST- some of the tests are not running when I run "npm run jest" in async methods
JEST- some of the tests are not running when I run "npm run jest" in async methods

Time:07-18

I am testing a service that saves data in REDIS, in my describe I have several "it" tests , the "it" tests are testing a variable that is given a value by an async function, I noticed that when I run the tests the "describe" cant see the test suites ("it") inside of it. I'm getting an error like this: " Your test suite must contain at least one test". I noticed when I'm removing the line of the variable that gets data from an async function it does see the tests but they are not relevant because they test the value of the variable that gets his data from the async function, I leaving the piece of code to help you understand better what I'm talking about.

describe("some testing", ()=>{
describe("redisProcessor", ()=>{
    console.log("---------------------------debug2")
    aisListenerSpec.run("123456")
    aisListenerSpec.redisProcessor(JSON.stringify(validMessage));

    let isOkay = true
    aisListenerSpec.redisSetter.client.hgetall("________someString______",(err, result)=>{
        console.log("---------------------------debug3")
        for (const key in result) {
            if(validMessage[key]!==undefined)
                if(JSON.stringify(validMessage[key])!==result[key]) isOkay = false;
        }

        console.log("---------------------------debug4")

        it("________someString______${missionId} should be 2 another fields: messageTime and missionId", ()=>{
            expect(result["messageTime"]).toBeTruthy();
            expect(result["missionId"]).toBe("123456");
        })
        it("________someString______${missionId} should be the same the validMessage",  ()=>{
            console.log("---------------------------debug5")
            expect(isOkay).toBeTruthy();
        })
        console.log("---------------------------debug6")
    })
})

here is the cli outputs

here is the cli outputs

It seems like that jest finished before getting to the tests.

thank ahead!

CodePudding user response:

Asynchronous code is hard to think about. A common gotcha is not realizing that the moment you call anything asynchronous, the function you are in immediately goes to the next line of code. Then, at some future and indeterminate time, the callback is invoked. Maybe.

This is what's happening in you code when you call aisListenerSpec.redisSetter.client.hgetall. All of the calls to it are inside of the callback and aren't called in the describe. They are called sometime later, after the describe has already returned.

Also, a lot of you setup code should probably be in a call to beforeEach and not inside of the describe. This gives you better control over when the setup code in invoke and by running it for each test, helps reduce side effects in your test suite.

Jest has a fairly complete page describing how to test code like this. I've synthesized a couple of quick examples that should help if you just want a quick solution.

  • Related