0
0
NestJSframework~10 mins

Mocking providers in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a mock provider for testing.

NestJS
const mockService = { findAll: jest.[1]() };
Drag options to blanks, or click blank then click option'
Amock
Bcall
Cfn
DspyOn
Attempts:
3 left
💡 Hint
Common Mistakes
Using jest.mock instead of jest.fn
Using jest.spyOn without an object
Using jest.call which does not exist
2fill in blank
medium

Complete the code to provide the mock service in the testing module.

NestJS
providers: [{ provide: UserService, use[1]: mockService }],
Drag options to blanks, or click blank then click option'
AValue
BClass
CFactory
DExisting
Attempts:
3 left
💡 Hint
Common Mistakes
Using useClass instead of useValue
Using useFactory without a factory function
Using useExisting which references another provider
3fill in blank
hard

Fix the error in the test setup by completing the missing method to reset mocks.

NestJS
beforeEach(() => { jest.[1](); });
Drag options to blanks, or click blank then click option'
ArestoreAllMocks
BresetAllMocks
CresetModules
DclearAllMocks
Attempts:
3 left
💡 Hint
Common Mistakes
Using resetModules which resets the module registry
Using restoreAllMocks which restores original implementations
Using a non-existent resetAllMocks method
4fill in blank
hard

Fill both blanks to mock a method and specify its return value.

NestJS
mockService.findAll = jest.[1]().mock[2](['user1', 'user2']);
Drag options to blanks, or click blank then click option'
Afn
BmockReturnValue
CmockResolvedValue
DspyOn
Attempts:
3 left
💡 Hint
Common Mistakes
Using spyOn without an object
Using mockResolvedValue for synchronous return
Using mockReturnValue without jest.fn()
5fill in blank
hard

Fill all three blanks to create a testing module with a mocked provider and verify the mock was called.

NestJS
const module = await Test.createTestingModule({ providers: [{ provide: UserService, use[1]: mockService }] }).compile(); const service = module.get<UserService>(UserService); await service.findAll(); expect(mockService.findAll).toHaveBeenCalled[2](); jest.[3]();
Drag options to blanks, or click blank then click option'
AValue
BOnce
CclearAllMocks
Dmock
Attempts:
3 left
💡 Hint
Common Mistakes
Using useClass instead of useValue
Using toHaveBeenCalled() without once
Not clearing mocks after tests