How to test service in Nestjs?

59 views Asked by At

service.ts

@Injectable()
export class FileUploadService {
  private readonly logger = new Logger(FileUploadService.name);

  constructor(private readonly httpService: HttpService) {}
}

spec.ts

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      imports: [],
      providers: [FileUploadService],
    }).compile();

    fileService = app.get<FileUploadService>(FileUploadService);
  });

error Test Error

Who can help me? I tried all of stackoverflow and still couldn't solve it.

1

There are 1 answers

0
buddy little On
import { createMock } from '@golevelup/ts-jest'

const module = await Test.createTestingModule({
  providers: [
    MainService
  ]
})
.useMocker(() => createMock())
.compile()

It help me to run test successfully.

But I don't know if it's right???