#Jest Spyon gives error for type never?

10 messages · Page 1 of 1 (latest)

naive sierra
#
      { client_id: '1', name: 'client1' } as Client,
      { client_id: '2', name: 'client2' } as Client,
    ];

    jest.spyOn(management, 'getClients').mockResolvedValue(clients);```


error: 5: Argument of type 'Client[]' is not assignable to parameter of type 'never'.
wild flare
#

What is management? And what is its getClients method?

naive sierra
#

management is a client management to client to auth0

    management = module.get<ManagementClient>('Auth0Management');```

and the getClients method fetchs clients from auth0 

async getClients(): Promise<Client[]> {
const clients = await this.management.getClients();
return clients;
}

wild flare
#

Interesting. Jest seems to think that the getClients is not an async method

naive sierra
#

yeah I read setting the mock response array would fix it but didn't quite do the trick 😅

wild flare
#

Any chance you can share a link to the code?

naive sierra
#

I can't share to the repo since it's a company repo but I can share the contents of the files

wild flare
#

That might help

naive sierra
#

full service

import { Injectable } from '@nestjs/common';
import { Client, ManagementClient } from 'auth0';
import { InjectManagement } from '@twirelab/nestjs-auth0';
import { ConfigService } from '@nestjs/config';
import { UpdateClientDto } from './dto/update-client.dto';
import { Neo4jService } from '../neo4j/neo4j.service';

@Injectable()
export class ClientsService {
  constructor(
    private configService: ConfigService,
    @InjectManagement() private readonly management: ManagementClient,
    private readonly neo4jService: Neo4jService,
  ) {}

  async getClients(): Promise<Client[]> {
    const clients = await this.management.getClients();
    return clients;
  }

  async getClientsByFields(fields: string[]): Promise<Client[]> {
    return await this.management.getClients({ fields });
  }

  async updateClient(
    clientId: string,
    updateClientDto: UpdateClientDto,
  ): Promise<Client> {
    return await this.management.updateClient(
      { client_id: clientId },
      updateClientDto,
    );
  }
}
wild flare
#

This is probably a moment of ts-jest not fully realizing that getClients s either void (uses a callback) or returns a Promise. I bet you could // @ts-ignore it and it would be fine