setup.ts•2.47 kB
import { jest } from '@jest/globals';
// Extend Jest matchers
declare global {
namespace jest {
interface Matchers<R> {
toBeValidBarcode(): R;
toBeValidProductResponse(): R;
toBeValidSearchResponse(): R;
}
}
}
// Custom matchers
expect.extend({
toBeValidBarcode(received: string) {
const pass = typeof received === 'string' &&
received.length >= 8 &&
received.length <= 13 &&
/^\d+$/.test(received);
if (pass) {
return {
message: () => `expected ${received} not to be a valid barcode`,
pass: true,
};
} else {
return {
message: () => `expected ${received} to be a valid barcode (8-13 digits)`,
pass: false,
};
}
},
toBeValidProductResponse(received: any) {
const hasStatus = typeof received.status === 'number';
const hasStatusVerbose = typeof received.status_verbose === 'string';
const hasValidProduct = !received.product || (
typeof received.product === 'object' &&
typeof received.product.code === 'string'
);
const pass = hasStatus && hasStatusVerbose && hasValidProduct;
if (pass) {
return {
message: () => `expected ${JSON.stringify(received)} not to be a valid product response`,
pass: true,
};
} else {
return {
message: () => `expected ${JSON.stringify(received)} to be a valid product response`,
pass: false,
};
}
},
toBeValidSearchResponse(received: any) {
const hasCount = typeof received.count === 'number';
const hasPage = typeof received.page === 'number';
const hasPageCount = typeof received.page_count === 'number';
const hasPageSize = typeof received.page_size === 'number';
const hasProducts = Array.isArray(received.products);
const pass = hasCount && hasPage && hasPageCount && hasPageSize && hasProducts;
if (pass) {
return {
message: () => `expected ${JSON.stringify(received)} not to be a valid search response`,
pass: true,
};
} else {
return {
message: () => `expected ${JSON.stringify(received)} to be a valid search response`,
pass: false,
};
}
},
});
// Global test configuration
process.env.NODE_ENV = 'test';
process.env.OPEN_FOOD_FACTS_BASE_URL = 'https://test.openfoodfacts.net';
process.env.OPEN_FOOD_FACTS_USER_AGENT = 'OpenFoodFactsMCP/1.0 (test)';