Skip to content

Replace jest and sinon with node:test #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
run: yarn lint

- name: Run tests
run: yarn test
run: yarn test:coverage
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"scripts": {
"lint": "eslint",
"release": "release-it",
"test": "uphold-scripts test"
"test": "node --test",
"test:coverage": "NODE_V8_COVERAGE=coverage node --test --experimental-test-coverage",
"test:watch": "node --test --watch"
},
"dependencies": {
"lodash": "^4.17.21",
Expand All @@ -55,7 +57,6 @@
"moment": "^2.29.1",
"prettier": "^3.5.3",
"release-it": "^17.0.1",
"sinon": "^11.1.1",
"tin-validator": "^1.0.0",
"uk-modulus-checking": "0.0.3",
"uphold-scripts": "^0.5.0",
Expand Down
25 changes: 13 additions & 12 deletions test/asserts/aba-routing-number-assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

const { Assert: BaseAssert, Violation } = require('validator.js');
const AbaRoutingNumberAssert = require('../../src/asserts/aba-routing-number-assert');
const { describe, it } = require('node:test');
const AbaRoutingNumberAssert = require('../../src/asserts/aba-routing-number-assert.js');

/**
* Extend `Assert` with `AbaRoutingNumberAssert`.
Expand All @@ -20,37 +21,37 @@ const Assert = BaseAssert.extend({
*/

describe('AbaRoutingNumberAssert', () => {
it('should throw an error if the input value is not a string', () => {
it('should throw an error if the input value is not a string', ({ assert }) => {
[{}, []].forEach(choice => {
try {
Assert.abaRoutingNumber().validate(choice);

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.violation.value).toBe('must_be_a_string');
assert.ok(e instanceof Violation);
assert.equal(e.violation.value, 'must_be_a_string');
}
});
});

it('should throw an error if the input value is not a valid ABA routing number', () => {
it('should throw an error if the input value is not a valid ABA routing number', ({ assert }) => {
try {
Assert.abaRoutingNumber().validate('foobar');

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.show().value).toBe('foobar');
assert.ok(e instanceof Violation);
assert.equal(e.show().value, 'foobar');
}
});

it('should expose `assert` equal to `AbaRoutingNumber`', () => {
it('should expose `assert` equal to `AbaRoutingNumber`', ({ assert }) => {
try {
Assert.abaRoutingNumber().validate(123);

fail();
assert.fail();
} catch (e) {
expect(e.show().assert).toBe('AbaRoutingNumber');
assert.equal(e.show().assert, 'AbaRoutingNumber');
}
});

Expand Down
53 changes: 30 additions & 23 deletions test/asserts/bank-identifier-code-assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

const { Assert: BaseAssert, Violation } = require('validator.js');
const BankIdentifierCodeAssert = require('../../src/asserts/bank-identifier-code-assert');
const { describe, it } = require('node:test');
const BankIdentifierCodeAssert = require('../../src/asserts/bank-identifier-code-assert.js');

/**
* Extend `Assert` with `BankIdentifierCodeAssert`.
Expand All @@ -20,56 +21,62 @@ const Assert = BaseAssert.extend({
*/

describe('BankIdentifierCodeAssert', () => {
it('should throw an error if the input value is not a string', () => {
it('should throw an error if the input value is not a string', ({ assert }) => {
const choices = [[], {}];

choices.forEach(choice => {
try {
Assert.bankIdentifierCode().validate(choice);

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.violation.value).toBe('must_be_a_string');
assert.ok(e instanceof Violation);
assert.equal(e.violation.value, 'must_be_a_string');
}
});
});

it('should expose `assert` equal to `BankIdentifierCode`', () => {
it('should expose `assert` equal to `BankIdentifierCode`', ({ assert }) => {
try {
Assert.bankIdentifierCode().validate(123);

fail();
assert.fail();
} catch (e) {
expect(e.show().assert).toBe('BankIdentifierCode');
assert.equal(e.show().assert, 'BankIdentifierCode');
}
});

it('should throw an error if the input value is not a valid bic', () => {
it('should throw an error if the input value is not a valid bic', ({ assert }) => {
try {
Assert.bankIdentifierCode().validate('BICOLETO');

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.value).toBe('BICOLETO');
assert.ok(e instanceof Violation);
assert.equal(e.value, 'BICOLETO');
}
});

it('should accept a valid bic without branch code', () => {
Assert.bankIdentifierCode().validate('FOOBARBI');
it('should accept a valid bic without branch code', ({ assert }) => {
assert.doesNotThrow(() => {
Assert.bankIdentifierCode().validate('FOOBARBI');
});
});

it('should accept a valid bic with branch code', () => {
Assert.bankIdentifierCode().validate('FOOBARBIXXX');
it('should accept a valid bic with branch code', ({ assert }) => {
assert.doesNotThrow(() => {
Assert.bankIdentifierCode().validate('FOOBARBIXXX');
});
});

it('should be case-insensitive', () => {
Assert.bankIdentifierCode().validate('FOOBARBI');
Assert.bankIdentifierCode().validate('FooBarBI');
Assert.bankIdentifierCode().validate('foobarbi');
Assert.bankIdentifierCode().validate('FOOBARBIXXX');
Assert.bankIdentifierCode().validate('FooBarBIXXX');
Assert.bankIdentifierCode().validate('foobarbixxx');
it('should be case-insensitive', ({ assert }) => {
assert.doesNotThrow(() => {
Assert.bankIdentifierCode().validate('FOOBARBI');
Assert.bankIdentifierCode().validate('FooBarBI');
Assert.bankIdentifierCode().validate('foobarbi');
Assert.bankIdentifierCode().validate('FOOBARBIXXX');
Assert.bankIdentifierCode().validate('FooBarBIXXX');
Assert.bankIdentifierCode().validate('foobarbixxx');
});
});
});
26 changes: 14 additions & 12 deletions test/asserts/big-number-assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

const { Assert: BaseAssert, Violation } = require('validator.js');
const BigNumberAssert = require('../../src/asserts/big-number-assert');
const { describe, it } = require('node:test');
const BigNumberAssert = require('../../src/asserts/big-number-assert.js');

/**
* Extend `Assert` with `BigNumberAssert`.
Expand All @@ -24,27 +25,27 @@ describe('BigNumberAssert', () => {
describe(`with option '${
option ? `{ validateSignificantDigits: ${option.validateSignificantDigits} }` : undefined
}'`, () => {
it('should throw an error if the input value is not a big number', () => {
it('should throw an error if the input value is not a big number', ({ assert }) => {
const choices = [[], {}, ''];

choices.forEach(choice => {
try {
Assert.bigNumber(option).validate(choice);

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
assert.ok(e instanceof Violation);
}
});
});

it('should expose `assert` equal to `BigNumber`', () => {
it('should expose `assert` equal to `BigNumber`', ({ assert }) => {
try {
Assert.bigNumber(option).validate();

fail();
assert.fail();
} catch (e) {
expect(e.show().assert).toBe('BigNumber');
assert.equal(e.show().assert, 'BigNumber');
}
});

Expand All @@ -57,16 +58,17 @@ describe('BigNumberAssert', () => {
});

if (!option || option.validateSignificantDigits === true) {
it('should throw an error if a number has more than 15 significant digits', () => {
it('should throw an error if a number has more than 15 significant digits', ({ assert }) => {
try {
// eslint-disable-next-line no-loss-of-precision
Assert.bigNumber(option).validate(1.011111111111111111111111111111111111111111);

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.show().assert).toBe('BigNumber');
expect(e.show().violation.message).toBe(
assert.ok(e instanceof Violation);
assert.equal(e.show().assert, 'BigNumber');
assert.equal(
e.show().violation.message,
'[BigNumber Error] Number primitive has more than 15 significant digits: 1.011111111111111'
);
}
Expand Down
69 changes: 38 additions & 31 deletions test/asserts/big-number-equal-to-assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/

const { Assert: BaseAssert, Violation } = require('validator.js');
const { describe, it } = require('node:test');
const BigNumber = require('bignumber.js');
const BigNumberEqualToAssert = require('../../src/asserts/big-number-equal-to-assert');
const BigNumberEqualToAssert = require('../../src/asserts/big-number-equal-to-assert.js');

/**
* Extend `Assert` with `BigNumberEqualToAssert`.
Expand All @@ -21,102 +22,108 @@ const Assert = BaseAssert.extend({
*/

describe('BigNumberEqualToAssert', () => {
it('should throw an error if `value` is missing', () => {
it('should throw an error if `value` is missing', ({ assert }) => {
try {
Assert.bigNumberEqualTo();

fail();
assert.fail();
} catch (e) {
expect(e.message).toBe('A value is required.');
assert.equal(e.message, 'A value is required.');
}
});

[undefined, { validateSignificantDigits: true }, { validateSignificantDigits: false }].forEach(option => {
describe(`with option '${
option ? `{ validateSignificantDigits: ${option.validateSignificantDigits} }` : undefined
}'`, () => {
it('should throw an error if `value` is not a number', () => {
it('should throw an error if `value` is not a number', ({ assert }) => {
try {
Assert.bigNumberEqualTo({}, option);

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.show().violation.message).toMatch(/Not a number/);
assert.ok(e instanceof Violation);
assert.ok(e.show().violation.message.match(/Not a number/));
}
});

it('should throw an error if the input value is not a number', () => {
it('should throw an error if the input value is not a number', ({ assert }) => {
const choices = [[], {}, ''];

choices.forEach(choice => {
try {
Assert.bigNumberEqualTo(10, option).validate(choice);

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
assert.ok(e instanceof Violation);
}
});
});

it('should throw an error if the input number is greater than the value', () => {
it('should throw an error if the input number is greater than the value', ({ assert }) => {
try {
Assert.bigNumberEqualTo(10, option).validate(12);

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
assert.ok(e instanceof Violation);
}
});

it('should throw an error if the input number is less than the value', () => {
it('should throw an error if the input number is less than the value', ({ assert }) => {
try {
Assert.bigNumberEqualTo(10, option).validate(9);

fail();
assert.fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
assert.ok(e instanceof Violation);
}
});

it('should expose `assert` equal to `BigNumberEqualTo`', () => {
it('should expose `assert` equal to `BigNumberEqualTo`', ({ assert }) => {
try {
Assert.bigNumberEqualTo(1, option).validate(0.1);

fail();
assert.fail();
} catch (e) {
expect(e.show().assert).toBe('BigNumberEqualTo');
assert.equal(e.show().assert, 'BigNumberEqualTo');
}
});

it('should expose `assert` equal to `BigNumberEqualTo` and `message` on the violation if the input value is not a number', () => {
it('should expose `assert` equal to `BigNumberEqualTo` and `message` on the violation if the input value is not a number', ({
assert
}) => {
try {
Assert.bigNumberEqualTo(10, option).validate({});

fail();
assert.fail();
} catch (e) {
expect(e.show().assert).toBe('BigNumberEqualTo');
expect(e.show().violation.message).toMatch(/Not a number/);
assert.equal(e.show().assert, 'BigNumberEqualTo');
assert.ok(e.show().violation.message.match(/Not a number/));
}
});

it('should expose `value` on the violation', () => {
it('should expose `value` on the violation', ({ assert }) => {
try {
Assert.bigNumberEqualTo(10, option).validate(0.1);

fail();
assert.fail();
} catch (e) {
expect(e.show().violation.value).toBe('10');
assert.equal(e.show().violation.value, '10');
}
});

it('should accept a big number as a value', () => {
Assert.bigNumberEqualTo(new BigNumber(10), option).validate(10);
it('should accept a big number as a value', ({ assert }) => {
assert.doesNotThrow(() => {
Assert.bigNumberEqualTo(new BigNumber(10), option).validate(10);
});
});

it('should accept a number that is equal to value', () => {
Assert.bigNumberEqualTo(10, option).validate(10);
it('should accept a number that is equal to value', ({ assert }) => {
assert.doesNotThrow(() => {
Assert.bigNumberEqualTo(10, option).validate(10);
});
});
});
});
Expand Down
Loading