Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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
✗ Incorrect
jest.fn() creates a mock function that can be used to replace real implementations in tests.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using useClass instead of useValue
Using useFactory without a factory function
Using useExisting which references another provider
✗ Incorrect
useValue allows you to provide a specific object or mock as the provider.
3fill in blank
hardFix 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'
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
✗ Incorrect
jest.clearAllMocks() resets the usage data of mocks between tests.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using spyOn without an object
Using mockResolvedValue for synchronous return
Using mockReturnValue without jest.fn()
✗ Incorrect
jest.fn() creates a mock function, and mockReturnValue sets its return value synchronously.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using useClass instead of useValue
Using toHaveBeenCalled() without once
Not clearing mocks after tests
✗ Incorrect
useValue provides the mock object; toHaveBeenCalledOnce() checks the mock was called once; clearAllMocks resets mocks after test.