Challenge - 5 Problems
NestJS Custom Provider Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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!' };
Attempts:
2 left
💡 Hint
Think about how the
useValue property works in custom providers.✗ Incorrect
The provider uses
useValue to supply a fixed string 'Hello, NestJS!'. Injecting MY_TOKEN into AppService constructor assigns this string to value. So, getValue() returns 'Hello, NestJS!'.📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Remember that
useFactory expects a function that returns the value.✗ Incorrect
Option B correctly uses
useFactory with an arrow function returning a random number. Option B uses useValue which would inject the function itself, not the result. Option B passes Math.random directly, which works but is less clear and can cause context issues. Option B is invalid because useClass expects a class, not a function.🔧 Debug
advanced2: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;
}Attempts:
2 left
💡 Hint
Check what the factory function returns and how NestJS handles undefined values.
✗ Incorrect
NestJS expects the factory to return a defined value. Returning undefined means the injection token 'CONFIG' has no usable value, resulting in undefined being injected which causes runtime errors when used.
❓ state_output
advanced2: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?Attempts:
2 left
💡 Hint
Think about how NestJS caches provider instances by default.
✗ Incorrect
NestJS creates the provider once and reuses it. The factory runs once, increments counter to 1, and returns 1. Both injections receive the same value 1.
🧠 Conceptual
expert3:00remaining
Which statement about custom providers in NestJS is TRUE?
Select the correct statement about custom providers in NestJS.
Attempts:
2 left
💡 Hint
Think about how dependencies are injected into factory providers.
✗ Incorrect
Option C is true: useFactory providers can declare dependencies via the inject array to receive other providers. Option C is false because useFactory and useValue are valid. Option C is false because useValue providers are singletons. Option C is false because custom providers can be scoped.