Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AuthController unit test not working #307

Open
SaadBazaz opened this issue Dec 30, 2022 · 2 comments
Open

AuthController unit test not working #307

SaadBazaz opened this issue Dec 30, 2022 · 2 comments

Comments

@SaadBazaz
Copy link
Contributor

After running NODE_ENV=development yarn test, I get:

yarn run v1.22.19
$ NODE_ENV=test jest
 FAIL  src/modules/auth/auth.controller.spec.ts (134.169 s)
  AuthController
    root
      ✕ should return "http://localhost" (1 ms)

  ● AuthController › root › should return "http://localhost"

    Nest can't resolve dependencies of the AuthController (?, AuthService). Please make sure that the argument UserService at index [0] is available in the RootTestModule context.

    Potential solutions:
    - Is RootTestModule a valid NestJS module?
    - If UserService is a provider, is it part of the current RootTestModule?
    - If UserService is exported from a separate @Module, is that module imported within RootTestModule?
      @Module({
        imports: [ /* the Module containing UserService */ ]
      })

       8 |
       9 |   beforeAll(async () => {
    > 10 |     app = await Test.createTestingModule({
         |           ^
      11 |       controllers: [AuthController],
      12 |       providers: [],
      13 |       imports: [],

      at TestingInjector.lookupComponentInParentModules (../node_modules/@nestjs/core/injector/injector.js:241:19)
      at TestingInjector.resolveComponentInstance (../node_modules/@nestjs/core/injector/injector.js:194:33)
      at TestingInjector.resolveComponentInstance (../node_modules/@nestjs/testing/testing-injector.js:16:45)
      at resolveParam (../node_modules/@nestjs/core/injector/injector.js:116:38)
          at async Promise.all (index 0)
      at TestingInjector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:131:27)
      at TestingInjector.loadInstance (../node_modules/@nestjs/core/injector/injector.js:57:13)
      at TestingInjector.loadController (../node_modules/@nestjs/core/injector/injector.js:75:9)
          at async Promise.all (index 0)
      at TestingInstanceLoader.createInstancesOfControllers (../node_modules/@nestjs/core/injector/instance-loader.js:56:9)
      at ../node_modules/@nestjs/core/injector/instance-loader.js:34:13
          at async Promise.all (index 1)
      at TestingInstanceLoader.createInstances (../node_modules/@nestjs/core/injector/instance-loader.js:31:9)
      at TestingInstanceLoader.createInstancesOfDependencies (../node_modules/@nestjs/core/injector/instance-loader.js:21:9)
      at TestingInstanceLoader.createInstancesOfDependencies (../node_modules/@nestjs/testing/testing-instance-loader.js:14:9)
      at TestingModuleBuilder.compile (../node_modules/@nestjs/testing/testing-module.builder.js:47:9)
      at Object.<anonymous> (modules/auth/auth.controller.spec.ts:10:11)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        134.283 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Any ideas on why this isn't working would be appreciated. Kind of a newbie in NestJS testing.

@spotlesscoder
Copy link
Contributor

The providers that the controller depends on are not specified.
I suggest mocking them this way (note that I also changed the test itself to make it actually work)

import type { TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';

import { UserEntity } from '../user/user.entity';
import { UserService } from '../user/user.service';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { TokenPayloadDto } from './dto/TokenPayloadDto';

describe('AuthController', () => {
  const defaultUser = new UserEntity();

  let app: TestingModule;

  beforeAll(async () => {
    app = await Test.createTestingModule({
      controllers: [AuthController],
      providers: [
        {
          provide: AuthService,
          useValue: {
            createAccessToken: () =>
              Promise.resolve(
                new TokenPayloadDto({ expiresIn: 3600, accessToken: 'token' }),
              ),
            validateUser: () => Promise.resolve(defaultUser),
          },
        },
        {
          provide: UserService,
          useValue: {
            createUser: () => Promise.resolve(defaultUser),
          },
        },
      ],
      imports: [],
    }).compile();
  });

  describe('root', () => {
    it('should be defined', () => {
      const appController = app.get<AuthController>(AuthController);
      expect(appController).toBeDefined();
    });
  });
});

@spotlesscoder
Copy link
Contributor

see #322

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants