Explore the world of TypeScript testing with these 30 multiple-choice questions. Understand how to write tests with Jest and Mocha, type test cases, and effectively use mocking and stubbing in TypeScript.
import { jest } from 'jest';import * as jest from 'jest';import jest from 'jest';import { expect, jest } from 'jest';npm install jest --save-devnpm install @jest/typesnpm install jest-tsnpm install typescript jestts-jest as a transformer in the Jest configurationmodule property to commonjs"tsSupport": true in Jest configurationdescribe()test()suite()start()it('should return true', () => { ... })test('should return true', () => { ... })expect('should return true').toEqual()describe('should return true', () => { ... })jest.fn()mockFunction()jest.mock()mockFunction.create()assert.equals(value1, value2)assertSame(value1, value2)expect(value).toBe(value)should(value).equal(value)beforeEach() method in Jest or Mocha?done()async()awaitnext()test('description', () => { ... })test<string>('description', () => { ... })it('description', () => { ... })test('description', (value: string) => { ... })jest.fn<(number) => string>()jest.fn() as jest.Mock<(number) => string>jest.fn<(string) => number>()jest.mock<(number) => string>beforeEach() method in Mocha?done callbackany for the return typebeforeEach()string in Jest?expect(value).toBeString()expect(value).toHaveType('string')expect(typeof value).toBe('string')expect(value).toBeOfType('string')describe('Test Suite', (): void => { ... })describe('Test Suite', (value: string): void => { ... })describe('Test Suite', () => { ... })describe('Test Suite', (): any => { ... })let results: string[] = []let results: Array<string> = []let results: TestResult[] = []let results: any[] = []boolean?jest.fn<boolean>()jest.fn(() => true)jest.Mock<boolean>jest.fn<boolean>().mockReturnValue(true)beforeEach method?() => void() => Promise<void>(done: () => void) => void(done: any) => voidjest.spyOn()jest.fn()jest.mock()jest.spyOn(fn, 'method')let data: Record<string, string>let data: objectlet data: any[]let data: TestData[]jest.fn() to create a mock functionjest.spyOn() on the HTTP methodmockResolvedValue() to simulate a responsesinon.stub()jest.spyOn()mockReturnValue()jest.spyOn(Class, 'method')jest.fn(Class.method)mockFunction(Class.method)jest.mock(Class.method)jest.fn()jest.mock()jest.stub()jest.spyOn()jest.mockReturnValue()jest.fn().mockReturnValue()mockReturn()jest.setMock()jest.fn().mockImplementation((arg1, arg2) => { ... })jest.fn().mockReturnValue()jest.spyOn().mockImplementation()jest.fn().mockArgs()sinon.stub()jest.stub()sinon.spy()mock.stub()jest.mock('./module')jest.spyOn('./module')mockImport('./module')jest.fn('./module')| Qno | Answer |
|---|---|
| 1 | D) import { expect, jest } from 'jest'; |
| 2 | A) npm install jest --save-dev |
| 3 | A) By adding ts-jest as a transformer in the Jest configuration |
| 4 | A) describe() |
| 5 | A) it('should return true', () => { ... }) |
| 6 | A) jest.fn() |
| 7 | C) expect(value).toBe(value) |
| 8 | B) Jasmine |
| 9 | A) To execute code before each test case runs |
| 10 | A) done() |
| 11 | D) test('description', (value: string) => { ... }) |
| 12 | B) jest.fn() as jest.Mock<(number) => string> |
| 13 | A) By typing the return value in the function signature |
| 14 | C) expect(typeof value).toBe('string') |
| 15 | C) describe('Test Suite', () => { ... }) |
| 16 | A) let results: string[] = [] |
| 17 | B) jest.fn<boolean>() |
| 18 | A) () => void |
| 19 | D) jest.spyOn(fn, 'method') |
| 20 | A) let data: Record<string, string> |
| 21 | A) To simulate real objects for testing |
| 22 | D) All of the above |
| 23 | B) By using sinon.stub() |
| 24 | A) To track method calls and arguments |
| 25 | A) jest.spyOn(Class, 'method') |
| 26 | B) jest.mock() |
| 27 | B) jest.fn().mockReturnValue() |
| 28 | A) jest.fn().mockImplementation((arg1, arg2) => { ... }) |
| 29 | A) sinon.stub() |
| 30 | A) jest.mock('./module') |