Testing And QA Flashcards

(10 cards)

1
Q

What is the difference between unit and integration testing?

A

Unit testing tests individual functions/components in isolation; integration testing tests interactions between components. Example: Test a React component (unit) vs. API call with database (integration).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you write a unit test for a JavaScript function using Jest?

A

```javascript
function add(a, b) { return a + b; }
test(‘adds 2 + 3 to equal 5’, () => {
expect(add(2, 3)).toBe(5);
});
// Explanation: Jest tests function output, ensuring correctness (O(1)).
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is mocking, and why is it used in testing?

A

Mocking simulates dependencies (e.g., APIs) to isolate tests. Example: Mock fetch in Jest to test a React component without hitting a real API, improving speed and reliability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you test a React component with Jest and React Testing Library?

A

```javascript
import { render, screen } from ‘@testing-library/react’;
import Button from ‘./Button’;
test(‘renders button with text’, () => {
render(<button>Click</button>);
expect(screen.getByText(‘Click’)).toBeInTheDocument();
});
// Explanation: Tests UI rendering, ensuring button displays text.
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is end-to-end (E2E) testing, and when is it used?

A

E2E testing simulates user workflows across the app. Example: Use Cypress to test a login flow from frontend to backend. Used to verify full system functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you write an E2E test with Cypress for a login form?

A

```javascript
describe(‘Login’, () => {
it(‘submits credentials’, () => {
cy.visit(‘/login’);
cy.get(‘input[name=username]’).type(‘user’);
cy.get(‘input[name=password]’).type(‘pass’);
cy.get(‘button’).click();
cy.url().should(‘include’, ‘/dashboard’);
});
});
// Explanation: Simulates user login, checks navigation.
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is test coverage, and why does it matter?

A

Test coverage measures the percentage of code executed by tests. Example: Aim for 80% coverage with Jest (--coverage). Ensures critical paths are tested, but 100% isn’t always practical.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you test an Express API endpoint?

A

```javascript
const request = require(‘supertest’);
const app = require(‘./app’);
test(‘GET /users returns user list’, async () => {
const res = await request(app).get(‘/users’);
expect(res.status).toBe(200);
expect(res.body).toHaveLength(2);
});
// Explanation: Uses supertest to test API response (O(1)).
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Arrange-Act-Assert pattern in testing?

A

Arrange sets up test data; Act calls the function/component; Assert verifies results. Example: Arrange let x = 2;, Act let sum = add(x, 3);, Assert expect(sum).toBe(5);.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you ensure tests run fast in a large codebase?

A

Use in-memory databases (e.g., SQLite), mock external services, and run tests in parallel with Jest’s --runInBand. Example: Mock MongoDB with mongodb-memory-server.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly