Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active April 11, 2026 19:06
Show Gist options
  • Select an option

  • Save trikitrok/2f73cb17e33c0596c67ce768dafdd1be to your computer and use it in GitHub Desktop.

Select an option

Save trikitrok/2f73cb17e33c0596c67ce768dafdd1be to your computer and use it in GitHub Desktop.
New unit tests generated by Junie to kill all mutants
// some omitted imports
describe('MariaDBDiscountsRepository Unit Test', () => {
let mockConnection: Partial<Connection>;
beforeEach(() => {
mockConnection = {
query: jest.fn()
};
});
it('should throw an error with specific message when discount is not found', async () => {
const discountCode = 'NON_EXISTING';
(mockConnection.query as jest.Mock).mockResolvedValue([]);
const repository = new MariaDBDiscountsRepository(mockConnection as Connection);
await expect(repository.findDiscountWith(discountCode))
.rejects
.toThrow(new DiscountNotFoundException(`Discount not found: ${discountCode}`));
});
it('should throw an error with specific message when discount type is unknown', async () => {
const discountCode = 'UNKNOWN_TYPE';
(mockConnection.query as jest.Mock).mockResolvedValue([{
code: discountCode,
type: 'UNKNOWN',
value: 10
}]);
const repository = new MariaDBDiscountsRepository(mockConnection as Connection);
await expect(repository.findDiscountWith(discountCode))
.rejects
.toThrow(new Error(`Unknown discount type: UNKNOWN`));
});
it('should handle non-string condition_data (e.g., when driver already parses it as object) and preserve the value', async () => {
const discountCode = 'WITH_CONDITION';
const conditionData = { value: 50 };
(mockConnection.query as jest.Mock).mockResolvedValue([{
code: discountCode,
type: 'PERCENTAGE',
value: 10,
condition_type: 'MIN_REQUIRED_AMOUNT',
condition_data: conditionData
}]);
const repository = new MariaDBDiscountsRepository(mockConnection as Connection);
const discount = await repository.findDiscountWith(discountCode);
// Assert that the condition value was correctly extracted
// Since AppliedOverMinimumTotalPriceDiscount doesn't expose minimumRequiredAmount,
// we test it by applying it to values below and above the threshold.
expect(discount.applyTo(40)).toBe(40); // 40 < 50
expect(discount.applyTo(60)).toBe(54); // 60 >= 50, 10% of 60 is 6, 60-6 = 54
});
it('should handle string condition_data correctly', async () => {
const discountCode = 'WITH_CONDITION_STRING';
const conditionData = JSON.stringify({ value: 50 });
(mockConnection.query as jest.Mock).mockResolvedValue([{
code: discountCode,
type: 'PERCENTAGE',
value: 10,
condition_type: 'MIN_REQUIRED_AMOUNT',
condition_data: conditionData
}]);
const repository = new MariaDBDiscountsRepository(mockConnection as Connection);
const discount = await repository.findDiscountWith(discountCode);
expect(discount.applyTo(40)).toBe(40);
expect(discount.applyTo(60)).toBe(54);
});
it('should distinguish between PERCENTAGE and FIXED types correctly', async () => {
const repository = new MariaDBDiscountsRepository(mockConnection as Connection);
// Test PERCENTAGE
(mockConnection.query as jest.Mock).mockResolvedValueOnce([{
code: 'PERC',
type: 'PERCENTAGE',
value: 10
}]);
const percDiscount = await repository.findDiscountWith('PERC');
expect(percDiscount).toBeInstanceOf(PercentageDiscount);
// Test FIXED
(mockConnection.query as jest.Mock).mockResolvedValueOnce([{
code: 'FIX',
type: 'FIXED',
value: 10
}]);
const fixedDiscount = await repository.findDiscountWith('FIX');
expect(fixedDiscount).toBeInstanceOf(FixedDiscount);
});
it('should kill row.type === "FIXED" mutant by ensuring it throws when type is not FIXED even if it is the second condition', async () => {
// If "else if (row.type === 'FIXED')" is replaced by "else if (true)",
// then any type that is NOT 'PERCENTAGE' will be treated as 'FIXED'.
// So let's provide a type that is NOT 'PERCENTAGE' and NOT 'FIXED'.
const discountCode = 'NOT_PERCENTAGE_NOR_FIXED';
(mockConnection.query as jest.Mock).mockResolvedValue([{
code: discountCode,
type: 'SOMETHING_ELSE',
value: 10
}]);
const repository = new MariaDBDiscountsRepository(mockConnection as Connection);
// This should throw Error("Unknown discount type: SOMETHING_ELSE")
// If it was mutated to true, it would return a FixedDiscount instead.
await expect(repository.findDiscountWith(discountCode))
.rejects
.toThrow(new Error(`Unknown discount type: SOMETHING_ELSE`));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment