34

I'm trying to create a simple test with nestjs, and I'm getting 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 a user signs up with an 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();
}
});
1

9 Answers 9

36

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();
});
2
  • 1
    this answer is misleading, its about jest v27 & not using jasmine2.
    – To Kra
    Apr 26, 2022 at 18:58
  • which is the config most people in the future will have, right? @ToKra
    – Riscie
    Sep 12, 2022 at 13:51
13

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

1
  • Thanks very helpful, using expect().rejects.toThrow() was the only solution that worked for me.
    – Yariv
    May 29, 2022 at 6:43
3

Before v27, jest use jest-jasmine2 by default.

For version 27, jest uses jest-circus which doesn’t support done callback.

So you need to change the default testRunner.

Override with react-app-rewired worked for me

// config-overrides.js
module.exports.jest = (config) => {
    config.testRunner = 'jest-jasmine2';
    return config;
};
1
  • Thank you, Your recomendation was usefull for me Jan 24 at 15:33
2

Just use return instead of calling done():

it('throws an error if a user signs up with an email that is in use', async () => {
  fakeUsersService.find = () =>
    Promise.resolve([{ id: 1, email: 'a', password: '1' } as User]);
  try {
    await service.signup('asdf@asdf.com', 'asdf');
  } catch {
    return;
  }
});
1

For the last version from jest, you can't use `async/await , promise and done together (Test functions cannot both take a 'done' callback and return something. Either use a 'done' callback, or return a promise.).

the solution is

user.entity.ts

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  AfterInsert,
  AfterRemove,
  AfterUpdate,
} from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  email: string;

  @Column()

  password: string;

  @AfterInsert()
  logInsert() {
    console.log('Inserted User with id', this.id);
  }

  @AfterUpdate()
  logUpdate() {
    console.log('Updated User with id', this.id);
  }

  @AfterRemove()
  logRemove() {
    console.log('Removed User with id', this.id);
  }
}

auth.service.spec.ts

  it('throws an error if user signs up with email that is in use', async () => {
    fakeUsersService.find = () =>
      Promise.resolve([{ id: 1, email: 'typescript@nestjs.jestjs', password: '1' } as User]);

    expect(async () => {
      const email = 'asdf@asdf.com';
      const password = 'asdf';
      await service.signup(email, password);
    }).rejects.toThrow(BadRequestException);
  });
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

2
  • 2
    Please don't post answers like this because this is a comment. If you keep doing this you will be rendered unable to ask questions in SO due to the downvotes you going to receive. So please read this, I'm trying to help not being rood.
    – S4NDM4N
    Mar 12, 2022 at 6:59
  • 1
    This is an answer that worked for me. Aug 26, 2022 at 16:47
0
it('throws an error if a user signs up with an email that is in use', async () => {
    await service.signup('asdf@asdf.com', 'asdf');
    try {
     await service.signup('asdf@asdf.com', 'asdf');
    } catch (e) {
      expect(e.toString()).toMatch('email in use');
    }
  });
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Ethan
    Nov 11, 2022 at 3:21
0

in order for it to work, you can do the following:

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

You can use this hack for some cases =)

it('should make an api request', (done) => {
  const asyncCall = async () => {
    await callbackWithApiInside();

    setTimeout(() => {
      expect(api).toHaveBeenNthCalledWith(1, payload);
      done();
    }, 1000);
  };

  asyncCall();
});
1
  • this is terrible and not what anyone should do. Flaky tests that now take a ton of time because of some setTimeout. Apr 23 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.