Testing with TypeScript Quiz (original) (raw)
Why is TypeScript beneficial for writing unit tests?
- It improves runtime performance of tests.
- It ensures type safety and catches errors during compilation.
- It eliminates the need for assertions in test cases.
- It allows tests to run without a testing framework.
Which of the following is a correct TypeScript test case?
TypeScript `
import { add } from "./math";
test("should add two numbers", () => { const result: number = add(2, 3); expect(result).toBe(5); });
`
- TypeScript does not support the test function.
- The type of result must not be explicitly declared.
- This is a correct TypeScript test case using a testing framework.
- TypeScript tests must use the assert method instead of expect.
Which testing framework is commonly used with TypeScript?
How do you configure Jest to work with TypeScript?
- Use the ts-jest package and update the Jest configuration file
- Install typescript-jest and run jest --ts command.
- Replace all .ts files with .js for testing.
- Jest does not support TypeScript.
What is type-safe mocking?
- A process to mock runtime behaviors without using type definitions.
- Creating mock implementations that adhere to the original type or interface.
- Using only default exports for mock functions.
- Replacing the actual implementation with an empty function.
Which library helps create type-safe mocks in TypeScript?
How would you create a type-safe mock using Jest?
TypeScript `
interface UserService { getUser: (id: number) => string; }
const mockUserService: jest.Mocked = { getUser: jest.fn().mockReturnValue("Pranjal"), };
`
- Jest does not support type-safe mocking.
- Use the jest.Mocked utility for type-safe mocks.
- Create mocks by directly implementing the interface.
- Mocks do not require adherence to type definitions.
Why is mocking important in unit tests?
- To avoid testing external dependencies and isolate the system under test.
- To replace all type checks with assertions.
- To test multiple systems simultaneously.
- To reduce the need for test cases.
Which of the following is a valid use case for a test double?
- To represent a function or object being replaced in a test.
- To replace TypeScript’s type system with mocks.
- To execute real implementations during testing.
- To write tests without a testing framework.
What does the following mock do?
TypeScript `
jest.spyOn(console, "log").mockImplementation(() => {});
`
- Logs all console messages to the test output.
- Replaces the console.log function with a mocked implementation.
- Prevents console messages from being logged.
- Deletes the console.log method entirely.
There are 10 questions to complete.
Take a part in the ongoing discussion