From bd4d3dbc5ca54983ad28745825e4767c1f291e60 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sun, 22 May 2022 13:13:09 +0200 Subject: [PATCH] test: improve luhn checks (error messages only) (#981) --- test/finance.spec.ts | 52 +++++++++++++++++++++----------------------- test/helpers.spec.ts | 8 +++---- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/test/finance.spec.ts b/test/finance.spec.ts index 7ae4a0f376d..0e19aa53542 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -372,23 +372,21 @@ describe('finance', () => { expect(number.length).toBeGreaterThanOrEqual(13); expect(number.length).toBeLessThanOrEqual(20); expect(number).toMatch(/^\d{13,20}$/); - expect(luhnCheck(number)).toBeTruthy(); + expect(number).toSatisfy(luhnCheck); }); it('should return a valid credit card number', () => { - expect(luhnCheck(faker.finance.creditCardNumber(''))).toBeTruthy(); - expect(luhnCheck(faker.finance.creditCardNumber())).toBeTruthy(); - expect( - luhnCheck(faker.finance.creditCardNumber('visa')) - ).toBeTruthy(); - expect( - luhnCheck(faker.finance.creditCardNumber('mastercard')) - ).toBeTruthy(); - expect( - luhnCheck(faker.finance.creditCardNumber('discover')) - ).toBeTruthy(); - expect(luhnCheck(faker.finance.creditCardNumber())).toBeTruthy(); - expect(luhnCheck(faker.finance.creditCardNumber())).toBeTruthy(); + expect(faker.finance.creditCardNumber('')).toSatisfy(luhnCheck); + expect(faker.finance.creditCardNumber()).toSatisfy(luhnCheck); + expect(faker.finance.creditCardNumber('visa')).toSatisfy(luhnCheck); + expect(faker.finance.creditCardNumber('mastercard')).toSatisfy( + luhnCheck + ); + expect(faker.finance.creditCardNumber('discover')).toSatisfy( + luhnCheck + ); + expect(faker.finance.creditCardNumber()).toSatisfy(luhnCheck); + expect(faker.finance.creditCardNumber()).toSatisfy(luhnCheck); }); it('should ignore case for issuer', () => { @@ -405,43 +403,43 @@ describe('finance', () => { //TODO: implement checks for each format with regexp const visa = faker.finance.creditCardNumber('visa'); expect(visa).toMatch(/^4(([0-9]){12}|([0-9]){3}(\-([0-9]){4}){3})$/); - expect(luhnCheck(visa)).toBeTruthy(); + expect(visa).toSatisfy(luhnCheck); const mastercard = faker.finance.creditCardNumber('mastercard'); expect(mastercard).toMatch(/^(5[1-5]\d{2}|6771)(\-\d{4}){3}$/); - expect(luhnCheck(mastercard)).toBeTruthy(); + expect(mastercard).toSatisfy(luhnCheck); const discover = faker.finance.creditCardNumber('discover'); - expect(luhnCheck(discover)).toBeTruthy(); + expect(discover).toSatisfy(luhnCheck); const american_express = faker.finance.creditCardNumber('american_express'); - expect(luhnCheck(american_express)).toBeTruthy(); + expect(american_express).toSatisfy(luhnCheck); const diners_club = faker.finance.creditCardNumber('diners_club'); - expect(luhnCheck(diners_club)).toBeTruthy(); + expect(diners_club).toSatisfy(luhnCheck); const jcb = faker.finance.creditCardNumber('jcb'); - expect(luhnCheck(jcb)).toBeTruthy(); + expect(jcb).toSatisfy(luhnCheck); const switchC = faker.finance.creditCardNumber('mastercard'); - expect(luhnCheck(switchC)).toBeTruthy(); + expect(switchC).toSatisfy(luhnCheck); const solo = faker.finance.creditCardNumber('solo'); - expect(luhnCheck(solo)).toBeTruthy(); + expect(solo).toSatisfy(luhnCheck); const maestro = faker.finance.creditCardNumber('maestro'); - expect(luhnCheck(maestro)).toBeTruthy(); + expect(maestro).toSatisfy(luhnCheck); const laser = faker.finance.creditCardNumber('laser'); - expect(luhnCheck(laser)).toBeTruthy(); + expect(laser).toSatisfy(luhnCheck); const instapayment = faker.finance.creditCardNumber('instapayment'); - expect(luhnCheck(instapayment)).toBeTruthy(); + expect(instapayment).toSatisfy(luhnCheck); }); it('should return custom formatted strings', () => { let number = faker.finance.creditCardNumber('###-###-##L'); expect(number).toMatch(/^\d{3}\-\d{3}\-\d{3}$/); - expect(luhnCheck(number)).toBeTruthy(); + expect(number).toSatisfy(luhnCheck); number = faker.finance.creditCardNumber('234[5-9]#{999}L'); expect(number).toMatch(/^234[5-9]\d{1000}$/); - expect(luhnCheck(number)).toBeTruthy(); + expect(number).toSatisfy(luhnCheck); }); }); diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 772e520ce9a..155d2d97efd 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -190,7 +190,7 @@ describe('helpers', () => { expect(number).toMatch( /^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/ ); - expect(luhnCheck(number)).toBeTruthy(); + expect(number).toSatisfy(luhnCheck); }); it('supports different symbols', () => { @@ -201,7 +201,7 @@ describe('helpers', () => { expect(number).toMatch( /^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/ ); - expect(luhnCheck(number)).toBeTruthy(); + expect(number).toSatisfy(luhnCheck); }); it('handles regexp style input', () => { @@ -212,14 +212,14 @@ describe('helpers', () => { expect(number).toMatch( /^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/ ); - expect(luhnCheck(number)).toBeTruthy(); + expect(number).toSatisfy(luhnCheck); number = faker.helpers.replaceCreditCardSymbols( '645[5-9]-#{4,6}-#{1,2}-#{4,6}-#{3}L' ); expect(number).toMatch( /^645[5-9]\-([0-9]){4,6}\-([0-9]){1,2}\-([0-9]){4,6}\-([0-9]){4}$/ ); - expect(luhnCheck(number)).toBeTruthy(); + expect(number).toSatisfy(luhnCheck); }); });