Testing Plugins
Use QUnit (jQuery's own test framework) to unit test plugins. Test initialization, options, methods, events, and cleanup.
Use QUnit (jQuery's own test framework) to unit test plugins. Test initialization, options, methods, events, and cleanup.
QUnit.test("myPlugin initializes correctly", function(assert) {
const $el = $("<div>").appendTo("#qunit-fixture");
$el.myPlugin({ color: "red" });
assert.ok($el.data("myPlugin"), "Instance stored in data");
assert.equal($el.css("color"), "rgb(255, 0, 0)", "Color applied");
});
QUnit.test("myPlugin destroy cleans up", function(assert) {
const $el = $("<div>").myPlugin();
$el.myPlugin("destroy");
assert.notOk($el.data("myPlugin"), "Instance removed");
});
Use #qunit-fixture for DOM tests — QUnit resets it between tests automatically.