A JavaScript library that provides helpers for testing Shopify Functions WASM (WebAssembly) modules. This library provides utilities for loading fixtures, validating test assets, building functions, and running functions.
npm install @shopify/shopify-function-test-helpersOr with pnpm:
pnpm add @shopify/shopify-function-test-helpersFor a full test suite that runs multiple fixtures using getFunctionInfo:
import path from "path";
import fs from "fs";
import { describe, beforeAll, test, expect } from "vitest";
import {
buildFunction,
getFunctionInfo,
loadSchema,
loadInputQuery,
loadFixture,
validateTestAssets,
runFunction
} from "@shopify/shopify-function-test-helpers";
describe("Default Integration Test", () => {
let schema;
let functionDir;
let schemaPath;
let targeting;
let functionRunnerPath;
let wasmPath;
beforeAll(async () => {
functionDir = path.dirname(__dirname);
await buildFunction(functionDir);
const functionInfo = await getFunctionInfo(functionDir);
({ schemaPath, functionRunnerPath, wasmPath, targeting } = functionInfo);
schema = await loadSchema(schemaPath);
}, 45000);
const fixturesDir = path.join(__dirname, "fixtures");
const fixtureFiles = fs
.readdirSync(fixturesDir)
.filter((file) => file.endsWith(".json"))
.map((file) => path.join(fixturesDir, file));
fixtureFiles.forEach((fixtureFile) => {
test(`runs ${path.relative(fixturesDir, fixtureFile)}`, async () => {
const fixture = await loadFixture(fixtureFile);
const targetInputQueryPath = targeting[fixture.target].inputQueryPath;
const inputQueryAST = await loadInputQuery(targetInputQueryPath);
const validationResult = await validateTestAssets({
schema,
fixture,
inputQueryAST
});
expect(validationResult.inputQuery.errors).toEqual([]);
expect(validationResult.inputFixture.errors).toEqual([]);
expect(validationResult.outputFixture.errors).toEqual([]);
const runResult = await runFunction(
fixture,
functionRunnerPath,
wasmPath,
targetInputQueryPath,
schemaPath
);
expect(runResult.error).toBeNull();
expect(runResult.result.output).toEqual(fixture.expectedOutput);
}, 10000);
});
});- buildFunction - Build a Shopify function using the Shopify CLI
- getFunctionInfo - Get function information from Shopify CLI (paths, targets, etc.)
- loadFixture - Load a test fixture file
- loadSchema - Load a GraphQL schema from a file
- loadInputQuery - Load and parse a GraphQL input query
- validateTestAssets - Validate test assets (input query, fixture input/output, query-fixture match)
- runFunction - Run a Shopify function
See wasm-testing-helpers.ts for all exported types.
pnpm build# Run linter
pnpm lint
# Fix linting issues
pnpm lint:fix# Run all tests
pnpm test
# Run tests in watch mode
pnpm test:watchpnpm build
pnpm packThis creates a .tgz file that can be installed in other projects:
{
"devDependencies": {
"@shopify/shopify-function-test-helpers": "file:../path/to/shopify-shopify-function-test-helpers-0.0.1.tgz"
}
}This project includes a comprehensive CI pipeline that runs on every push and pull request:
- Lint & Type-check: Ensures code quality and type safety
- Tests: Runs on multiple OS (Ubuntu, Windows, macOS) and Node versions (18.x, 20.x, 22.x)
- Build: Verifies the TypeScript compilation and creates the package
The CI configuration can be found in .github/workflows/ci.yml.
MIT
This repo uses Changesets to manage releases.
When making significant changes, generate a new changeset:
pnpm changesetFollow the prompts to:
- Select the change type (patch, minor, or major)
- Provide a description of your changes
Commit the generated changeset file (.changeset/*.md) with your PR.
- When your PR merges to
main: The release workflow automatically creates or updates a "Version Packages" pull request. - Version Packages PR: Contains all unreleased changes from merged changesets, with updated version numbers and CHANGELOG entries.
- When the Version Packages PR merges: The workflow publishes the new version to the npm registry.
To change or expand the contents of the next release:
- Close the existing "Version Packages" PR.
- The next commit to
mainwill create a new "Version Packages" PR containing all unreleased changes (including the ones from the closed PR).
This allows you to add more changes or modify changeset descriptions before releasing.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for your changes
- Run the test suite (
pnpm test) - Run the linter (
pnpm lint) - Submit a pull request
For more details, see the test examples in this repository.