The assert library helps you check if your code works as expected by providing functions to write simple checks (assertions). If a check fails, it will show an error.
describe('Addition', () => { it('should add two numbers correctly', () => { assert.ok(3 + 4 === 7); // Check if 3 + 4 equals 7 }); });
The `assert.ok()` function checks if a condition is true. If it’s not, it shows an error. It’s useful for simple checks.
describe('Addition', () => { it('should add two numbers correctly', () => { assert.ok(3 + 4 === 7); // Check if 3 + 4 equals 7 }); });
`assert.equal()` checks if two values are equal, but it’s less strict than `assert.strictEqual()`. It’s good for situations where exact matches aren’t critical.
const landAnimals = ['giraffe', 'squirrel']; const waterAnimals = ['shark', 'stingray']; landAnimals.push('frog'); waterAnimals.push('frog'); assert.equal(landAnimals[2], waterAnimals[2]); // Check if both arrays have 'frog' at the same index
`assert.strictEqual()` checks if two values are exactly the same, including their type. It’s stricter than `assert.equal()`.
const a = 3; const b = '3'; assert.strictEqual(a, b); // This will fail because 3 (number) is not the same as '3' (string)
`assert.deepEqual()` checks if two objects have the same values for all their properties, even if they are different objects.
const a = {relation: 'twin', age: '17'}; const b = {relation: 'twin', age: '17'}; assert.deepEqual(a, b); // Check if both objects have the same properties and values
The `before()` function runs once before any of the tests in a file. It’s used to set up anything needed for the tests.
before(() => { path = './message.txt'; // Set up a file path before tests run });
The `beforeEach()` function runs before each individual test. It’s useful for setting up or resetting conditions for each test.
beforeEach(() => { testCounter++; // Increment counter before each test });
The `after()` function runs once after all the tests in a file. It’s used to clean up or print results after all tests are done.
after(() => { console.log('Number of tests: ' + testCounter); // Print the number of tests after all tests have run });
The `afterEach()` function runs after each test. It’s used to clean up or reset conditions specific to each test.
afterEach(() => { path = './message.txt'; // Reset the file path after each test });
Testing helps find problems in your code before users see it. It ensures your code works as expected.
describe('group of tests', () => { // Write individual tests here });
Test frameworks help organize and run your tests automatically. They provide feedback when something goes wrong.
describe('Addition', () => { it('should add two numbers correctly', () => { // Write your checks here }); });
The `describe()` function groups related tests together. It describes what the tests are for and contains individual test cases.
describe('Array pop method', () => { it('should remove the last item in the array', () => { const array = ['item1', 'item2']; const removed = array.pop(); assert.ok(removed === 'item2'); // Check if the last item was removed }); });
The `it()` function defines individual tests. It describes what the test is checking and contains the code to perform the test.
describe('Array pop method', () => { it('should remove the last item in the array', () => { const array = ['item1', 'item2']; const removed = array.pop(); assert.ok(removed === 'item2'); // Check if the last item was removed }); });
In the Setup phase, you prepare everything needed for the tests, like setting up variables or creating objects.
describe('Array pop method', () => { it('should remove the last item in the array', () => { // Setup const array = ['item1', 'item2']; // Exercise const removed = array.pop(); // Verify assert.ok(removed === 'item2'); }); });
In the Exercise phase, you run the code that you are testing. This is where you perform the actions you want to check.
it('creates a new file with a string of text', () => { // Setup const path = './message.txt'; const str = 'Hello, World!'; // Exercise: write to file fs.appendFileSync(path, str); // Verify: check file contents const contents = fs.readFileSync(path); assert.equal(contents.toString(), str); // Teardown: clean up fs.unlinkSync(path); });
In the Verify phase, you check if the results of your test match your expectations. This is where you use assertions to validate the outcome.
it('should correctly add two numbers', () => { const result = add(2, 3); assert.equal(result, 5); // Check if the result is 5 });
In the Teardown phase, you clean up or reset the environment to ensure each test runs in a fresh state. This avoids tests affecting each other.
afterEach(() => { fs.unlinkSync('./message.txt'); // Clean up file after each test });
Each test should run on its own without depending on other tests. This means tests should not affect each other’s results.
Ensure each test sets up its own conditions and cleans up after itself to avoid interference.
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!