Medusa Test Utilities (v1.x)

1.1.45 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use `medusaIntegrationTestRunner` to set up an integration test suite for Medusa v1.x, including database access, service resolution, and making API requests. It also shows a basic Jest setup with an increased timeout.

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();
      });
    });
  },
});

view raw JSON →