Questions tagged [nestjs-testing]

34 questions
9
votes
2 answers

Nestjs overrideProvider vs provider in unit testing

I see two ways of mocking services in NestJS for unit testing, the first one is the same as we define providers in real modules like: const module = await Test.createTestingModule({ providers: [ UserService, { provide:…
Vahid Najafi
  • 4,654
  • 11
  • 43
  • 88
9
votes
3 answers

Jest throwing reference error about an import inside a node_modules dependency

I have a nestjs monorepo application with working tests via Jest. This is in relation to the global unit tests which take their configuration from the nestjs CLI-created configuration within package.json. My storage.service.ts uses jimp in one of…
SebastianG
  • 8,563
  • 8
  • 47
  • 111
9
votes
1 answer

Testing Class with @Injectable Scope / @Inject(REQUEST) NestJS

I have set up a MongooseConfigService to allow us to dynamically switch out the connection string for certain requests and am trying to get the tests set up correctly. @Injectable({scope: Scope.REQUEST}) export class MongooseConfigService implements…
RemeJuan
  • 823
  • 9
  • 25
5
votes
1 answer

Nestjs e2e testing of an application within a monorepo fails to resolve @app import from library with jest despite config in package.json

I'm trying to run e2e tests for a monorepo application that also utilises several libraries from within the monorepo, all imports throughout the application are resolved using "@app" imports, for example import { ConfigService } from…
SebastianG
  • 8,563
  • 8
  • 47
  • 111
4
votes
0 answers

NestJS how to get apolloClient from the graphql module to test using `apollo-server-testing`?

Although the title is similar to Access to Apollo server for NestJS GraphQL test, the question itself is old and the answers in that question all address behaviour that no longer seems to exist in nestjs. I have followed the PR here…
SebastianG
  • 8,563
  • 8
  • 47
  • 111
3
votes
0 answers

Pass Array to creat function in NestJs Unit Test with Type ORM

In my Typeform create function. I'm accepting an array of objects in my service. but when I try to write a unit test for that it's throwing an error because mockReturnValue only accepts a single object. This is my test spec export class…
NicoleZ
  • 1,485
  • 3
  • 22
  • 43
3
votes
2 answers

Override provider in single test in nest.js e2e testing

I have e2e test of module with controller and some providers: beforeAll(async () => { const moduleRef = await Test.createTestingModule({ controllers: [TestController], providers: [ TestService, { provide:…
Furman
  • 2,017
  • 3
  • 25
  • 43
2
votes
2 answers

Nest.js testing module, can not override provider injected into another provider, undefined received

I have Nest.js app, where certain provider is injected into another provider: export class AppService { public constructor(private readonly appInnerService: AppInnerService) {} } AppService has a method publish which calls…
Furman
  • 2,017
  • 3
  • 25
  • 43
2
votes
2 answers

Testing Service with Mongoose in NestJS

I am trying to test my LoggingService in NestJS and while I cannot see anything that is wrong with the test the error I am getting is Error: Cannot spy the save property because it is not a function; undefined given instead The function being tested…
RemeJuan
  • 823
  • 9
  • 25
1
vote
0 answers

Mocking a NestJS Service with many dependencies - is there a cleaner way of doing so?

I'm writing unit tests for a NestJS Controller and I'm trying to mock its Service. My test suite currently works fine and my tests are passing, but I was wondering if there was a cleaner way to mock the Service as opposed to the current approach…
simon777
  • 51
  • 1
  • 5
1
vote
0 answers

How to solve circular dependency issue with NestJs and Mongodb?

I am try trying to implement unit test code for my nestjs application but I keep getting this error. A circular dependency has been detected inside RootTestModule. Please, make sure that each side of a bidirectional relationships are decorated with…
1
vote
1 answer

How to override provider for tests in NestJs and not create transitive dependencies that are not needed?

I am writing end-to-end tests in NestJs and using the "overrideProvider" function to setup test versions of various services (e.g. services that require database connections). I noticed though that even when I do this, the original implementation…
okhobb
  • 758
  • 8
  • 22
1
vote
1 answer

E2E testing with JWT guard

I’m trying to get e2e testing to work for this user update function. When user logs in their id and email are saved into JWT token and stored in HttpOnly Cookie. My problem is how to get this token so that it passes through JWT guard, gets id from…
ThisDude
  • 13
  • 4
1
vote
1 answer

How to mock a logger dependency of a provider in NestJS?

First of all, I'm not sure if that's even a thing, it's my first time writing unit tests for NestJs. I am trying to write some unit tests for a controller which has the following dependencies in its…
M_Zarnowski
  • 126
  • 2
  • 8
0
votes
1 answer

Problems using NodeJS test runner in a NestJS project

I'm trying to run tests in my NestJS application using NodeJS native test runner, but I'm having some trouble. 1 - I can't run multiple test files with this script "test": "node -r tsconfig-paths/register -r ts-node/register --test ./test". It only…
1
2 3