This document provides a reference for the testing framework used in this repository. It covers the main classes, functions, and assertions that can be used to write and execute tests for JavaScript projects.
The Test class runs the tests contained within the provided testClass. It looks for methods prefixed with test_ and executes them.
testClass: An instance of the class that contains your test methods.
run(): Runs all test methods intestClass. Handles setup (before) and teardown (after) methods before and after each test.
const testObject = new MyTest();
new Test(testObject).run();before(): Runs before each test to set up any required state.after(): Runs after each test to clean up.
These methods are optional but useful for managing test setup and teardown.
Assertions are used to verify that the code behaves as expected. If an assertion fails, the test fails.
- Ensures that
valueistrue. - Throws an error if
valueis nottrue.
Assert.isTrue(1 < 2); // Passes
Assert.isTrue(false); // Fails- Checks strict equality (
===) betweenexpectedandactual. - Throws an error if the values are not equal.
Assert.equals(5, 5); // Passes
Assert.equals(5, '5'); // Fails- Verifies that two arrays, including nested arrays, are equal.
- Throws an error if the arrays are not equal.
const array1 = [1, [2, 3], 4];
const array2 = [1, [2, 3], 4];
Assert.deepEquals(array1, array2); // Passes- All test methods must be prefixed with
test_to be recognized by theTestrunner. - Use descriptive names to indicate what the test is verifying (e.g.,
test_userAuthentication).
class UserTest {
test_userAuthentication() {
// Code to test user authentication
}
}- The framework uses
Logger.logto print test execution details. - Logs include which test is being run, whether it passes or fails, and any error messages.
Running test: userAuthentication
Test passed: userAuthentication
1 tests passed
0 tests failed
- Use the
Testclass to run your test methods. - Include optional
beforeandaftermethods for setup and teardown. - Utilize the
Assertobject to verify expected outcomes. - Prefix test methods with
test_for the framework to recognize them.
This reference should help you effectively use the testing framework to create and run tests for your JavaScript code.