Duke Jake Morgan
Duke Jake Morgan

Reputation: 17

Jest test won't pass, even if the first test is commented

I was writing tests for some authentication endpoints when I came across a bug like issue that I cannot seem to figure out.

This test won't pass:

it("Testing /auth/signup/lol", () => {
  const test = true;
  expect(test).toBe(true);
  console.log("finished test?");
});

The only way I can reproduce this issue is taking all the setup and teardown code, then moving it to another file, along with the test that's troubling me (as I have done, this test copied from auth.test.js).

I've spent the morning trying to figure out the issue, the only time the test has passed is when I removed the setup code. However on notebooks.test.js and auth.test.js (excluding the last two three tests at the bottom of the auth.tests.js script) the setup codes works as intended.

What's wrong with this code?

Steps to reproduce:

  1. Clone this repository
  2. Switch to the develop branch
  3. Go to the backend directory
  4. Install all packages
  5. Run 'npm test tests/endpoints/auth2.test.js'

I would post a small program reproducing the issue, but all attempts to do so failed.

Upvotes: 0

Views: 719

Answers (1)

Brice Miramont
Brice Miramont

Reputation: 160

there's a timeout in the afterEach() method because collections.users.drop() returns an undefined value (result is undefined). At the minimum, add an else clause when testing result value so you can exit method before reaching the 10s timeout. Maybe some additional code is required but I don't know the logic of your code and test so maybe afterEach should do more things. Here is the working code:

afterEach(done => {
    server.getTokenManager().empty()
    let collections = server.getClient()?.connection.collections
    if (collections === undefined) {
        done()
        return
    }
    if (collections.users !== undefined) {
        collections.users.drop((err: any, result: any) => {
            if (result) {
                done()
            } else {
                done()
            }
        })
    }
}, 10000)

The rest of the code is ok.

enter image description here

Upvotes: 1

Related Questions