Which of the following best explains why automated testing improves reliability in NestJS apps?
Think about how finding problems early helps keep apps stable.
Automated tests run code checks regularly, catching bugs before users see them. This early detection helps keep the app reliable.
Given a NestJS service with a failing unit test, what is the immediate effect on application reliability?
describe('CatsService', () => { let service: CatsService; beforeEach(() => { service = new CatsService(); }); it('should return all cats', () => { expect(service.findAll()).toEqual(['cat1', 'cat2']); }); it('should add a cat', () => { service.addCat('cat3'); expect(service.findAll()).toContain('cat3'); }); it('should fail this test', () => { expect(service.findAll().length).toBe(0); }); });
Think about what a failing test means for developers.
A failing test shows a problem in the code. Developers fix it before the app goes live, which helps keep the app reliable.
Consider this test for a NestJS controller method. What will the test output be?
import { Test, TestingModule } from '@nestjs/testing'; import { AppController } from './app.controller'; describe('AppController', () => { let appController: AppController; beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], }).compile(); appController = app.get<AppController>(AppController); }); it('should return Hello World', () => { expect(appController.getHello()).toBe('Hello World!'); }); it('should fail this test', () => { expect(appController.getHello()).toBe('Hello'); }); });
Check the expected strings in each test.
The first test expects 'Hello World!' which matches the controller output, so it passes. The second expects 'Hello' which does not match, so it fails.
Identify the option that will cause a syntax error when running a NestJS test.
describe('Sample Test', () => { it('should pass', () => { expect(true).toBe(true); }); });
Look for missing or extra parentheses or braces.
Option D has a misplaced closing parenthesis inside the arrow function, causing a syntax error.
Examine the following test code. Why does it fail with a timeout?
describe('Async Test', () => { it('should complete async operation', () => { setTimeout(() => { expect(true).toBe(true); }, 1000); }); });
Think about how Jest knows when async tests finish.
Jest needs a Promise returned or a done callback called to know async tests finished. Without this, it times out.