Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

652
Views
Test functions cannot both take a 'done' callback

I'm trying to create a simple test with nestjs, and I'm get this error

Test functions cannot both take a 'done' callback and return something. Either use a 'done' callback, or return a promise.

Returned value: Promise {}

The unit test is so simple, but I get an error when I use done();

it('throws an error if user signs up with email that is in use', async (done) => {
fakeUsersService.find = () => Promise.resolve([{ id: 1, email: 'a', password: '1' } as User]);
try {
  await service.signup('asdf@asdf.com', 'asdf');
} catch (err) {
  done();
}
});
10 months ago · Santiago Trujillo
3 answers
Answer question

0

You are combining Async/Await and Done.

Either use asnyc/await, or done.

it('throws an error if user signs up with email that is in use', async () => {
    try {
        await service();
        expect(...);
    } catch (err) {
    }
});

or use the done format

it('throws an error if user signs up with email that is in use', (done) => {
    ...
    service()
     .then( ...) {}
     .catch( ...) {}
    }
    done();
});
10 months ago · Santiago Trujillo Report

0

for the last version from jest, you can't use `async/await , promise and done together.

the solution is

 it("throws an error if user sings up with email that is in use", async () => {
    fakeUsersService.find = () =>
      Promise.resolve([{ id: 1, email: "a", password: "1" } as User]);
    await expect(service.signup("asdf@asdf.com", "asdf")).rejects.toThrow(
      BadRequestException
    );
  });

change BadRequestException according to your listening exception

10 months ago · Santiago Trujillo Report

0

Also, if you want to use both you can downgrade your current version of jest to : 26.6.3. Worked fine for me, I'm using async + done

10 months ago · Santiago Trujillo Report
Answer question
Find remote jobs