axios-axios/test/specs/utils/forEach.spec.js
Jay fa337332b9
Update unit testing flows as part of migration to vitest (#7484)
* chore: small fixes to tests

* feat: transitional move to vitests

* feat: moving unit tests in progress

* feat: moving more unit tests over

* feat: more tests moved

* feat: updated more sections of the http test

* chore: wip http tests

* chore: wip http tests

* chore: more http tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: tests

* chore: remove un-needed docs

* chore: update package lock

* chore: update lock
2026-03-06 20:42:14 +02:00

70 lines
1.2 KiB
JavaScript

/* eslint-env mocha */
import utils from '../../../lib/utils';
const { forEach } = utils;
describe('utils::forEach', function () {
it('should loop over an array', function () {
let sum = 0;
forEach([1, 2, 3, 4, 5], function (val) {
sum += val;
});
expect(sum).toEqual(15);
});
it('should loop over object keys', function () {
let keys = '';
let vals = 0;
const obj = {
b: 1,
a: 2,
r: 3,
};
forEach(obj, function (v, k) {
keys += k;
vals += v;
});
expect(keys).toEqual('bar');
expect(vals).toEqual(6);
});
it('should handle undefined gracefully', function () {
let count = 0;
forEach(undefined, function () {
count++;
});
expect(count).toEqual(0);
});
it('should make an array out of non-array argument', function () {
let count = 0;
forEach(
function () {},
function () {
count++;
}
);
expect(count).toEqual(1);
});
it('should handle non object prototype gracefully', function () {
let count = 0;
const data = Object.create(null);
data.foo = 'bar';
forEach(data, function () {
count++;
});
expect(count).toEqual(1);
});
});