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-helpers
import {
buildFunction,
loadFixture,
loadSchema,
loadInputQuery,
validateTestAssets,
runFunction,
} from "@shopify/shopify-function-test-helpers";
// Load the GraphQL schema and input query
const schema = await loadSchema("/path/to/schema.graphql");
const inputQueryAST = await loadInputQuery("/path/to/src/run.graphql");
// Load a test fixture
const fixture = await loadFixture("/path/to/fixtures/my-test.json");
// Build the function
await buildFunction("/path/to/function");
// Validate the test assets
const validationResult = await validateTestAssets({
schema,
fixture,
inputQueryAST,
});
console.log(` Input Query: ${validationResult.inputQuery.valid ? '✅' : '❌'}`);
console.log(` Input Fixture: ${validationResult.inputFixture.valid ? '✅' : '❌'}`);
console.log(` Input Query-Fixture Match: ${validationResult.inputQueryFixtureMatch.valid ? '✅' : '❌'}`);
console.log(` Output Fixture: ${validationResult.outputFixture.valid ? '✅' : '❌'}`);
// Run the function
const runResult = await runFunction(
fixture.export,
fixture.input,
"/path/to/function"
);
console.log("Output:", runResult.result.output);
console.log("Expected:", fixture.expectedOutput);
For a full test suite that runs multiple fixtures:
import path from "path";
import fs from "fs";
import {
buildFunction,
loadFixture,
runFunction,
validateTestAssets,
loadSchema,
loadInputQuery,
} from "@shopify/shopify-function-test-helpers";
describe("Function Tests", () => {
let schema;
let inputQueryAST;
let functionDir;
beforeAll(async () => {
functionDir = path.dirname(__dirname);
await buildFunction(functionDir);
const schemaPath = path.join(functionDir, "schema.graphql");
const inputQueryPath = path.join(functionDir, "src/run.graphql");
schema = await loadSchema(schemaPath);
inputQueryAST = await loadInputQuery(inputQueryPath);
}, 20000);
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.basename(fixtureFile)}`, async () => {
const fixture = await loadFixture(fixtureFile);
const validationResult = await validateTestAssets({
schema,
fixture,
inputQueryAST,
});
expect(validationResult.inputQuery.valid).toBe(true);
expect(validationResult.inputFixture.valid).toBe(true);
expect(validationResult.inputQueryFixtureMatch.valid).toBe(true);
expect(validationResult.outputFixture.valid).toBe(true);
const runResult = await runFunction(
fixture.export,
fixture.input,
functionDir
);
expect(runResult.error).toBeNull();
expect(runResult.result.output).toEqual(fixture.expectedOutput);
}, 10000);
});
});
loadFixture
- Load a fixture file from the specified pathloadSchema
- Load a GraphQL schema from a fileloadInputQuery
- Load and parse a GraphQL input queryvalidateTestAssets
- Validate test assets including input query, fixture input/output, and query-fixture matchbuildFunction
- Build a Shopify function using the Shopify CLIrunFunction
- Run a Shopify function using the Shopify CLI
See wasm-testing-helpers.ts for all exported types.
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
# Run example integration tests
npm run test:examples
npm run build
# Run linter
npm run lint
# Fix linting issues
npm run lint:fix
MIT
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for your changes
- Run the test suite
- Submit a pull request