|
| 1 | +# Testing Practices Cheat Sheet |
| 2 | + |
| 3 | +## Testing Libraries and Frameworks |
| 4 | + |
| 5 | +- Jest: Main testing framework used for unit and integration tests |
| 6 | +- Mockito: Used for mocking objects and dependencies |
| 7 | +- PowerMock: Employed for mocking static methods and constructors |
| 8 | + |
| 9 | +## Mocking and Stubbing Strategies |
| 10 | + |
| 11 | +1. Jest Mock Functions |
| 12 | + |
| 13 | + ```javascript |
| 14 | + const mockFunction = jest.fn(); |
| 15 | + mockFunction.mockReturnValue(expectedValue); |
| 16 | + ``` |
| 17 | + |
| 18 | +2. Mockito Mocks |
| 19 | + |
| 20 | + ```java |
| 21 | + MyClass mockedObject = Mockito.mock(MyClass.class); |
| 22 | + Mockito.when(mockedObject.someMethod()).thenReturn(expectedValue); |
| 23 | + ``` |
| 24 | + |
| 25 | +3. PowerMock for Static Methods |
| 26 | + ```java |
| 27 | + PowerMockito.mockStatic(StaticClass.class); |
| 28 | + PowerMockito.when(StaticClass.staticMethod()).thenReturn(expectedValue); |
| 29 | + ``` |
| 30 | + |
| 31 | +## Fake Implementations |
| 32 | + |
| 33 | +1. In-memory Database |
| 34 | + |
| 35 | + ```javascript |
| 36 | + const fakeDatabase = { |
| 37 | + users: [], |
| 38 | + addUser: function (user) { |
| 39 | + this.users.push(user); |
| 40 | + }, |
| 41 | + getUser: function (id) { |
| 42 | + return this.users.find((u) => u.id === id); |
| 43 | + }, |
| 44 | + }; |
| 45 | + ``` |
| 46 | + |
| 47 | +2. HTTP Request Mocking |
| 48 | + ```javascript |
| 49 | + jest.mock('axios'); |
| 50 | + axios.get.mockResolvedValue({ data: mockResponseData }); |
| 51 | + ``` |
| 52 | + |
| 53 | +## Test Structure |
| 54 | + |
| 55 | +1. Describe-It Pattern |
| 56 | + |
| 57 | + ```javascript |
| 58 | + describe('MyComponent', () => { |
| 59 | + it('should render correctly', () => { |
| 60 | + // Test implementation |
| 61 | + }); |
| 62 | + }); |
| 63 | + ``` |
| 64 | + |
| 65 | +2. Setup and Teardown |
| 66 | + |
| 67 | + ```javascript |
| 68 | + beforeEach(() => { |
| 69 | + // Setup code |
| 70 | + }); |
| 71 | + |
| 72 | + afterEach(() => { |
| 73 | + // Teardown code |
| 74 | + }); |
| 75 | + ``` |
| 76 | + |
| 77 | +## Assertion Styles |
| 78 | + |
| 79 | +1. Jest Expect |
| 80 | + |
| 81 | + ```javascript |
| 82 | + expect(result).toBe(expectedValue); |
| 83 | + expect(mockFunction).toHaveBeenCalledTimes(1); |
| 84 | + ``` |
| 85 | + |
| 86 | +2. Mockito Verify |
| 87 | + ```java |
| 88 | + Mockito.verify(mockedObject, Mockito.times(1)).someMethod(); |
| 89 | + ``` |
| 90 | + |
| 91 | +## Test Data Generation |
| 92 | + |
| 93 | +1. Factory Functions |
| 94 | + |
| 95 | + ```javascript |
| 96 | + function createTestUser(overrides = {}) { |
| 97 | + return { |
| 98 | + id: 1, |
| 99 | + name: 'Test User', |
| 100 | + |
| 101 | + ...overrides, |
| 102 | + }; |
| 103 | + } |
| 104 | + ``` |
| 105 | + |
| 106 | +2. Faker Library (if used) |
| 107 | + ```javascript |
| 108 | + const faker = require('faker'); |
| 109 | + const testUser = { |
| 110 | + name: faker.name.findName(), |
| 111 | + email: faker.internet.email(), |
| 112 | + }; |
| 113 | + ``` |
| 114 | + |
| 115 | +## Asynchronous Testing |
| 116 | + |
| 117 | +1. Async/Await |
| 118 | + |
| 119 | + ```javascript |
| 120 | + it('should fetch data asynchronously', async () => { |
| 121 | + const result = await fetchData(); |
| 122 | + expect(result).toBeDefined(); |
| 123 | + }); |
| 124 | + ``` |
| 125 | + |
| 126 | +2. Promise Chaining |
| 127 | + ```javascript |
| 128 | + it('should handle promises', () => { |
| 129 | + return somePromiseFunction().then((result) => { |
| 130 | + expect(result).toBe(expectedValue); |
| 131 | + }); |
| 132 | + }); |
| 133 | + ``` |
| 134 | + |
| 135 | +## Code Coverage |
| 136 | + |
| 137 | +- Jest is configured to collect code coverage |
| 138 | +- Aim for high coverage, especially in critical paths |
| 139 | +- Use `istanbul` ignore comments for intentionally uncovered code |
| 140 | + |
| 141 | +## Best Practices |
| 142 | + |
| 143 | +1. Test one thing per test case |
| 144 | +2. Use descriptive test names |
| 145 | +3. Avoid test interdependence |
| 146 | +4. Mock external dependencies |
| 147 | +5. Use setup and teardown for common operations |
| 148 | +6. Prefer realistic test data |
| 149 | +7. Test both positive and negative scenarios |
| 150 | +8. Keep tests DRY (Don't Repeat Yourself) |
| 151 | +9. Regularly run and maintain tests |
0 commit comments