0
0
NestJSframework~20 mins

Testing module setup in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this NestJS testing module setup?
Consider this NestJS test setup code. What will be the value of service.getHello() after the test module is compiled and the service is retrieved?
NestJS
import { Test, TestingModule } from '@nestjs/testing';
import { AppService } from './app.service';

describe('AppService', () => {
  let service: AppService;

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

    service = module.get<AppService>(AppService);
  });

  it('should return greeting', () => {
    expect(service.getHello()).toBe('Hello World!');
  });
});
A'Hello NestJS!'
B'Hello World!'
Cundefined
DThrows an error because service is not defined
Attempts:
2 left
💡 Hint
Look at how the service is provided and retrieved from the testing module.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in NestJS testing module setup?
Identify which code snippet will cause a syntax error when setting up a NestJS testing module.
NestJS
import { Test, TestingModule } from '@nestjs/testing';

async function setup() {
  const module = await Test.createTestingModule({
    providers: [
      {
        provide: 'MyService',
        useClass: MyService,
      }
    ]
  }).compile();
  return module;
}
AMissing comma after useClass: MyService
BUsing useValue with an object literal
CProviding a string token without useClass or useValue
DUsing useClass without quotes around MyService
Attempts:
2 left
💡 Hint
Check the commas between object properties carefully.
🔧 Debug
advanced
3:00remaining
Why does this NestJS test fail with 'Nest can't resolve dependencies' error?
Given this testing module setup, why does the test fail with a dependency resolution error?
NestJS
import { Test, TestingModule } from '@nestjs/testing';
import { AppService } from './app.service';
import { AppController } from './app.controller';

describe('AppController', () => {
  let controller: AppController;

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

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

  it('should return greeting', () => {
    expect(controller.getHello()).toBe('Hello World!');
  });
});
AAppService is not provided in the testing module providers array
BAppController is not declared in controllers array
CMissing async keyword in beforeEach
DIncorrect import path for AppController
Attempts:
2 left
💡 Hint
Check what dependencies AppController needs and if they are provided.
state_output
advanced
2:30remaining
What is the value of 'result' after this NestJS test runs?
In this test, what will be the value of the variable result after calling service.getData()?
NestJS
import { Test, TestingModule } from '@nestjs/testing';
import { DataService } from './data.service';

class MockDataService {
  getData() {
    return ['mock1', 'mock2'];
  }
}

describe('DataService', () => {
  let service: DataService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        DataService,
        { provide: DataService, useClass: MockDataService },
      ],
    }).compile();

    service = module.get<DataService>(DataService);
  });

  it('should return mocked data', () => {
    const result = service.getData();
    expect(result).toEqual(['mock1', 'mock2']);
  });
});
A['real1', 'real2']
B[]
CThrows error because of duplicate providers
D['mock1', 'mock2']
Attempts:
2 left
💡 Hint
Check which provider is used when multiple providers for the same token exist.
🧠 Conceptual
expert
2:00remaining
Which option best describes the purpose of NestJS TestingModule's compile() method?
What does the compile() method do when called on a NestJS TestingModule builder?
AIt compiles TypeScript code to JavaScript for the module
BIt runs all tests defined in the module automatically
CIt creates an instance of the module and resolves all dependencies for testing
DIt resets the module to its initial state before each test
Attempts:
2 left
💡 Hint
Think about what is needed before you can use the module's providers and controllers in tests.