This intial setup has a test.js file with a basic testing function and assertion function that allows you to create tests using plain JavaScript in the browser. See the sample project here.
- The goal of this project is to be able to write and test functions being used in your JavaScript without installing any dependancies other than the test.js file itself.
- As a developer I want to test JavaScript functions without installing depencies so that I can do Test Driven Development in the browser.
-
Add the
test.js
file to your project. -
In your main JavaScript file import test and whatever functions your testing.
-
Write your tests:
test('title', functionName, () => {
expect(functionName(arg).toBe(value))
})
Example:
import { test, expect } from "./modules/test.js";
import { add, subtract, greet } from "./modules/helpers.js";
test(`adds two values`, add, () => {
expect(add(1, 2)).toBe(3);
});
test(`subtracts two values`, subtract, () => {
expect(subtract(10, 5)).toBe(5);
});
test(`greets a name`, greet, () => {
expect(greet("John")).toBe("Hello, John");
});
- Launch your server (I'm using LiveReload)
- Open your console to see your tests.