0
votes

im doing a simple test with my new framework Nest js, but the first time I run the command npm run test im getting this error

●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "SUCCESS conection to MEMCACHED server".

      21 |   // this function Stores a new value in Memcached.
      22 |   setKey(key : string, value : string , timeExp:number) : Promise<boolean>{
    > 23 |     // Transform the callback into a promise to be used in the controller
         |                 ^
      24 |     return new Promise((resolve,reject) =>{
      25 |       // Using memcached api to set a key
      26 |       memcached.set(key, value, timeExp, async function (err) {

      at BufferedConsole.log (../node_modules/@jest/console/build/BufferedConsole.js:201:10)
      at modules/cache/application/cache.service.ts:23:17
      at allocate (../node_modules/jackpot/index.js:125:5)
      at Socket.either (../node_modules/jackpot/index.js:166:5)

but this is my test file

describe('Cache Controller', () => {
  let controller: CacheController;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      controllers: [CacheController],
    }).compile();

    controller = module.get<CacheController>(CacheController);
  });

  it('should be defined', () => {
    expect(controller).toBeDefined();
  });
});

  • This is the function that are getting the error (don't know why if im not testing this function)
// this function Stores a new value in Memcached.
  async setKey(key : string, value : string , timeExp:number) : Promise<boolean>{
    // Transform the callback into a promise to be used in the controller
    return await new Promise((resolve,reject) =>{
      // Using memcached api to set a key
      memcached.set(key, value, timeExp, function (err) {
        if (err) return reject(err);
        resolve(true)
      });  
    });
  }
1

1 Answers

2
votes

Your async function will return a promise.

Define your test cases as async function and call await to wait for the return values.

it('Test case name', async () => {
   // Await something
   // Expect something
})