Medusa Test Utilities (v1.x)
medusa-test-utils is a utility package designed to facilitate integration testing for applications built with Medusa, specifically targeting Medusa v1.x installations. It provides essential tools for setting up and tearing down isolated test environments, managing database interactions (primarily PostgreSQL with `pg-god`), and interacting with the Medusa container and API layer. This package, at version 1.1.45, is part of the older Medusa ecosystem, which relied on `TypeORM` v0.2.x. While still functional for legacy projects, the Medusa core has since transitioned to v2.x, which utilizes the `@medusajs/test-utils` package for its testing framework. Therefore, `medusa-test-utils` (v1.x) is considered to be in maintenance mode, receiving fewer updates compared to its successor. Its key differentiators include deep integration with the Medusa v1.x architecture, providing direct access to services and database operations within a controlled test scope.
Common errors
-
Error: Cannot find module 'typeorm' from 'medusa-test-utils'
cause The `typeorm` package is a peer dependency but is not installed or the installed version conflicts with what `medusa-test-utils` expects.fixEnsure `typeorm` is installed as a direct dependency in your project (`npm install typeorm@^0.2.43` or `yarn add typeorm@^0.2.43`). If other packages require a newer `typeorm` version, consider using `resolutions` or `overrides`. -
Timeout - Async callback was not invoked within the 60000 ms timeout specified by jest.setTimeout.
cause Medusa integration tests involve spinning up a server and database, which can take longer than the default Jest timeout, or the test logic itself is too slow.fixIncrease the Jest timeout for your test suite or individual tests, for example, by adding `jest.setTimeout(60 * 1000);` (for 60 seconds) at the top of your test file or within a `beforeAll` hook.
Warnings
- breaking The `medusa-test-utils` package is primarily for Medusa v1.x. For Medusa v2.x and newer, the testing framework has been repackaged and renamed to `@medusajs/test-utils` with potentially breaking API changes and different internal implementations. Projects upgrading to Medusa v2.x must migrate their test setup to use the new package.
- gotcha Version 1.1.45 of `medusa-test-utils` lists `typeorm: ^0.2.43` as a peer dependency, which can lead to conflicts with Medusa core versions (e.g., v1.20.0+) that might require `typeorm: ^0.3.x`. This mismatch can cause dependency resolution issues or runtime errors related to TypeORM.
- gotcha When running integration tests with `medusa-test-utils`, especially for HTTP routes, the test runner can produce verbose `console.log` output from `module-test-runner.js`. This noise can make debugging difficult and clutter CI logs.
Install
-
npm install medusa-test-utils -
yarn add medusa-test-utils -
pnpm add medusa-test-utils
Imports
- medusaIntegrationTestRunner
const { medusaIntegrationTestRunner } = require('medusa-test-utils')import { medusaIntegrationTestRunner } from 'medusa-test-utils' - moduleIntegrationTestRunner
import moduleIntegrationTestRunner from 'medusa-test-utils'
import { moduleIntegrationTestRunner } from 'medusa-test-utils' - initDb
import { initDb } from 'medusa-test-utils' - dropDb
import { dropDb } from 'medusa-test-utils'
Quickstart
import { medusaIntegrationTestRunner } from 'medusa-test-utils';
import { jest } from '@jest/globals';
// Set a longer timeout for integration tests due to database operations and server startup
jest.setTimeout(60 * 1000);
medusaIntegrationTestRunner({
testSuite: ({ db, api, getContainer }) => {
describe('Medusa API Endpoints', () => {
let productService;
beforeAll(async () => {
// Resolve a service from the Medusa container
productService = getContainer().resolve('productService');
});
it('should retrieve a product by ID', async () => {
// Example: Create a product and then retrieve it
const productData = {
title: 'Test Product',
description: 'A product for testing',
is_giftcard: false,
discountable: true,
status: 'published',
thumbnail: 'https://example.com/thumbnail.jpg',
options: [{ title: 'Size' }],
variants: [
{
title: 'Small',
prices: [{ currency_code: 'usd', amount: 1000 }],
options: { size: 'S' }
}
]
};
const createdProduct = await productService.create(productData);
const retrievedProduct = await api.get(`/store/products/${createdProduct.id}`);
expect(retrievedProduct.status).toBe(200);
expect(retrievedProduct.data.product.id).toBe(createdProduct.id);
expect(retrievedProduct.data.product.title).toBe('Test Product');
});
it('should create a new order via API', async () => {
// This is a simplified example, a real test would involve more setup (customer, region, cart)
const payload = {
email: 'test@example.com',
items: [], // Assuming items would be added to a cart first
shipping_address: { address_1: '123 Main St', city: 'Anytown', province: 'Anystate', country_code: 'us', postal_code: '12345' },
billing_address: { address_1: '123 Main St', city: 'Anytown', province: 'Anystate', country_code: 'us', postal_code: '12345' },
region_id: 'reg_test_region' // Placeholder, would need to be a valid region ID
};
// Note: Direct order creation via /store/orders is usually more complex (cart flow expected)
// This test primarily demonstrates API interaction, not full commerce flow validity.
const response = await api.post('/store/orders', payload);
// A successful order creation typically returns 200 or 201 status
// Depending on Medusa configuration, direct order creation might be restricted.
// For robust testing, a cart-to-checkout flow is usually preferred.
expect(response.status).toBeGreaterThanOrEqual(200);
expect(response.status).toBeLessThan(300);
expect(response.data.order).toBeDefined();
});
});
},
});