← Back to Blog
Developers

How to Write Automated Tests for a ShopsWired Plugin

To test a ShopsWired plugin, put test files in its test/ folder, wrap each file in sw.test.suite, and run them on a store with developer mode turned on. Tests run in the same ShopScript runtime as your plugin, against the real sw.* bridges, and can fire a platform hook exactly the way the store does. The test/ folder is never included in a published package.

That last point is the reason to bother. A suite costs nothing in your plugin's size budget, no merchant ever installs it, and what passes is what the store will actually do — not what a local mock of the platform thinks it will do.

We'll use a small, real example: the six-unit minimum order rule from our checkout validation post. We'll make the minimum a merchant setting, then test it twice — once as a plain function, once through the real checkout.before_create hook.

Split the rule out so it can be tested on its own

A hook that does everything inline can only be tested by firing the hook. Move the decision into a module and the hook becomes a thin adapter. Put this in lib/minimum.js:

// Returns null when the basket qualifies, or the message to show when it doesn't.
module.exports.shortfallMessage = function (items, minimum) {
    let units = 0;
    for (let i = 0; i < items.length; i++) {
        units += items[i].qty;
    }
    if (units >= minimum) {
        return null;
    }
    const missing = minimum - units;
    return "Orders require at least " + minimum +
        " units. Please add " + missing +
        " more " + (missing === 1 ? "unit" : "units") +
        " to your cart.";
};

The hook in hooks.js reads the setting, asks the module, and throws the documented blocking shape when there's a shortfall. Its first argument is ctx, and the order sits in ctx.data.order:

const rule = require("./lib/minimum");

module.exports = {
    "checkout.before_create": function (ctx) {
        const minimum = parseInt(ctx.settings.minimum_units, 10) || 6;
        const message = rule.shortfallMessage(ctx.data.order.items, minimum);
        if (message) {
            throw { error: message };
        }
    }
};

The manifest gains a number setting with a default. ctx.settings in a hook is the merchant's saved values laid over the manifest defaults, so an untouched setting still reads as 6. The parseInt and fallback are there because a blank or zero value should not silently switch the rule off.

{
    "id": "minimum-order-quantity",
    "name": "Minimum Order Quantity",
    "version": "1.1.0",
    "scripts": [
        { "path": "hooks.js" }
    ],
    "settings": [
        { "key": "minimum_units", "type": "number", "label": "Minimum units per order", "default": 6 }
    ]
}

Unit-test the rule

A test file's run export is the suite wrapper — it reports the results, so there is nothing to call at the end. describe, test and expect come off sw.test. Put this in test/minimum.js:

const { describe, test, expect } = sw.test;

module.exports.run = sw.test.suite(function () {
    const rule = require("./lib/minimum");

    describe("shortfallMessage", function () {
        test("one line of six passes", function () {
            expect(rule.shortfallMessage([{ qty: 6 }], 6)).toBe(null);
        });

        test("counts units, not lines", function () {
            expect(rule.shortfallMessage([{ qty: 2 }, { qty: 4 }], 6)).toBe(null);
        });

        test("five units asks for exactly one more", function () {
            expect(rule.shortfallMessage([{ qty: 5 }], 6)).toContain("add 1 more unit ");
        });

        test("pluralises a larger shortfall", function () {
            expect(rule.shortfallMessage([{ qty: 2 }, { qty: 2 }], 6)).toContain("add 2 more units");
        });
    });
});

The test asks for ./lib/minimum, not ../lib/minimum — that matches the reference's own example, where a file in test/ pulls in ./lib/mailer. A throw inside one case fails only that case; the rest of the file still runs, and a file with failures ends with one error listing all of them. You see everything that broke, not just the first thing.

These cases target the bug the original post warned about: items.length counts lines, not units. The second test fails the moment someone “simplifies” the loop.

Fire the real hook

A passing unit test proves the rule. It doesn't prove the store applies it — that the manifest registered the script, that the export name is spelled right, that the setting is read. sw.test.hook fires a platform hook the way the store fires it, and reports whether it was prevented and why. Put this in test/checkout.js:

const { describe, test, expect, cleanup } = sw.test;

module.exports.run = sw.test.suite(function () {
    sw.test.settings({ minimum_units: 6 });
    cleanup(function () { sw.test.settings(); });

    function checkout(items) {
        return sw.test.hook("checkout.before_create", { order: { items: items } });
    }

    describe("checkout.before_create", function () {
        test("blocks a five-unit order", function () {
            const out = checkout([{ product_id: 7, qty: 5, price: 1999 }]);
            expect(out.prevented).toBe(true);
            expect(out.error).toContain("add 1 more unit");
        });

        test("lets two lines totalling six through", function () {
            const out = checkout([
                { product_id: 7, qty: 2, price: 1999 },
                { product_id: 8, qty: 4, price: 499 }
            ]);
            expect(out.prevented).toBeFalsy();
        });

        test("follows the merchant's setting", function () {
            sw.test.settings({ minimum_units: 12 });
            const out = checkout([{ product_id: 7, qty: 6, price: 1999 }]);
            expect(out.error).toContain("at least 12 units");
        });
    });
});

Two details matter here. First, sw.test.settings is the only way to control the settings a hook reads — a hook is handed your installed settings, not a ctx your test built. It applies for the rest of the file, so the test that changes it goes last, and cleanup calling it with nothing puts the real settings back when the file finishes, passed or failed.

Second, the data object carries only the fields this handler reads. If your hook also looks at ctx.data.cart or ctx.data.payment_method, put them in.

The rest of the toolkit, briefly

Where this falls short

Writing your own plugins starts on Pro, which is $99/mo; Starter runs a full store on ready-made themes and plugins. The developer docs carry the full sw.test reference, including the outbox fields and the stub recording API. Start with one unit file for your rule and one hook file for its boundary — that pair catches most of what breaks between an edit and a live checkout.