Describing Tests
- Label tests with descriptive texts rather than function names
describe('sorting the list of users', function() {
it('sorts in descending order by default', function() {
...
});
});
describe('sorting the list of users', function() {
it('sorts in descending order by default', function() {
...
});
});
expect(isLeapYear(2000)).toBe(true);
describe("readFileContent function", () => {
beforeEach(() => {
// Runs before each test
vi.resetAllMocks();
// Set up default mock behavior for fs.readFile
fs.readFile.mockResolvedValue("Mocked content");
});
it("should successfully read the content of a text file", async () => {
const content = await readFileContent("text-content.txt");
// Check that fs.readFile was called once
expect(fs.readFile).toHaveBeenCalledTimes(1);
expect(fs.readFile).toHaveBeenCalledWith("text-content.txt", "utf-8");
// Verify that the returned content matches the mock
expect(content).toBe("Mocked content");
});
});
| E2E | Unit |
|---|---|
| E2E tests interact with the app interface, just as a user does. | Unit tests interact with the app code. They call app functions, look at app data structures. |
| E2E tests can slow, waiting for server, browser, and network responses. | Unit tests should blazingly fast, fractions of a second. |
| A well-tested app will have hundreds of E2E tests. | A well-tested app will have thousands of unit tests. |
toStringThanks to Hakim El Hattab for RevealJS