0
0
NestJSframework~20 mins

Custom providers in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Custom Provider Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be the output of this NestJS custom provider injection?
Consider the following NestJS provider and service setup. What will be returned when AppService.getValue() is called?
NestJS
import { Injectable, Inject } from '@nestjs/common';

const MY_TOKEN = 'MY_TOKEN';

@Injectable()
export class AppService {
  constructor(@Inject(MY_TOKEN) private readonly value: string) {}

  getValue() {
    return this.value;
  }
}

export const myProvider = {
  provide: MY_TOKEN,
  useValue: 'Hello, NestJS!'
};
Anull
Bundefined
CThrows a runtime error: No provider for MY_TOKEN
D'Hello, NestJS!'
Attempts:
2 left
💡 Hint
Think about how the useValue property works in custom providers.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom provider using a factory function in NestJS?
You want to create a custom provider that returns a random number each time it is injected. Which option correctly defines this provider?
A{ provide: 'RANDOM', useValue: () => Math.random() }
B{ provide: 'RANDOM', useFactory: () => Math.random() }
C{ provide: 'RANDOM', useFactory: Math.random }
D{ provide: 'RANDOM', useClass: () => Math.random() }
Attempts:
2 left
💡 Hint
Remember that useFactory expects a function that returns the value.
🔧 Debug
advanced
2:30remaining
Why does this custom provider cause issues in NestJS?
Given this provider definition, what issue occurs when trying to inject 'CONFIG'?
export const configProvider = {
  provide: 'CONFIG',
  useFactory: () => {
    return fetchConfig();
  }
};

function fetchConfig() {
  return undefined;
}
AThe factory returns undefined, so NestJS cannot inject a valid value.
BThe provider token 'CONFIG' is not a valid symbol or class.
CThe factory function is missing async keyword for fetchConfig.
DNestJS requires useClass for providers, not useFactory.
Attempts:
2 left
💡 Hint
Check what the factory function returns and how NestJS handles undefined values.
state_output
advanced
2:30remaining
What are the values of count1 and count2 after injecting this custom provider twice?
Consider this provider and service:
let counter = 0;

export const counterProvider = {
  provide: 'COUNTER',
  useFactory: () => {
    counter += 1;
    return counter;
  }
};

@Injectable()
export class CounterService {
  constructor(@Inject('COUNTER') public count1: number, @Inject('COUNTER') public count2: number) {}
}
What are the values of count1 and count2 in CounterService?
Acount1 = 1, count2 = 1
Bcount1 = 2, count2 = 2
Ccount1 = 1, count2 = 2
Dcount1 = 0, count2 = 0
Attempts:
2 left
💡 Hint
Think about how NestJS caches provider instances by default.
🧠 Conceptual
expert
3:00remaining
Which statement about custom providers in NestJS is TRUE?
Select the correct statement about custom providers in NestJS.
AuseValue providers are recreated every time they are injected.
BCustom providers must always use useClass to be injectable in NestJS.
CCustom providers using useFactory can inject other providers by listing them in the inject array.
DCustom providers cannot be scoped to request or transient lifetimes.
Attempts:
2 left
💡 Hint
Think about how dependencies are injected into factory providers.