Skip to content

Commit 79f339d

Browse files
committed
test(common): Restoring isEmpty method with test coverage
I got an error when running npm run test:coverage. The @nestjs/graphql package depends on the isEmpty function from @nestjs/common. After renaming isEmpty to isEmptyArray, the @nestjs/graphql package can no longer find the isEmpty function, leading to the error. To fix this issue, I restored the isEmpty function for the @nestjs/graphql package.
1 parent 902cb14 commit 79f339d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/common/test/utils/shared.utils.spec.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect } from 'chai';
22
import {
33
addLeadingSlash,
44
isConstructor,
5+
isEmpty,
56
isEmptyArray,
67
isFunction,
78
isNil,
@@ -48,9 +49,7 @@ describe('Shared utils', () => {
4849
it('should return true when obj is object', () => {
4950
expect(isObject({})).to.be.true;
5051
});
51-
it('should return true for arrays', () => {
52-
expect(isObject([1, 2, 3])).to.be.true; // Arrays are objects
53-
});
52+
5453
it('should return false when object is not object', () => {
5554
expect(isObject(3)).to.be.false;
5655
expect(isObject(null)).to.be.false;
@@ -199,6 +198,24 @@ describe('Shared utils', () => {
199198
});
200199
});
201200

201+
describe('isEmpty', () => {
202+
it('should return true when array is empty or not exists', () => {
203+
expect(isEmpty([])).to.be.true;
204+
});
205+
206+
it('should return false when array is not empty', () => {
207+
expect(isEmpty([1, 2])).to.be.false;
208+
});
209+
it('should return false for non-array values', () => {
210+
expect(isEmpty({})).to.be.false;
211+
expect(isEmpty('')).to.be.false;
212+
expect(isEmpty(0)).to.be.false;
213+
expect(isEmpty(false)).to.be.false;
214+
expect(isEmpty(Symbol())).to.be.false;
215+
expect(isEmpty(() => {})).to.be.false;
216+
});
217+
});
218+
202219
describe('isEmptyArray', () => {
203220
it('should return true when array is empty or not exists', () => {
204221
expect(isEmptyArray([])).to.be.true;

packages/common/utils/shared.utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ export const isConstructor = (val: unknown): boolean => val === 'constructor';
5454
export const isNil = (val: unknown): val is null | undefined =>
5555
isUndefined(val) || val === null;
5656

57+
export const isEmpty = (value: unknown): boolean => {
58+
if (isNil(value)) {
59+
return true;
60+
}
61+
if (Array.isArray(value)) {
62+
return value.length === 0;
63+
}
64+
return false;
65+
};
66+
5767
export const isEmptyArray = (array: unknown): boolean => {
5868
if (isNil(array)) {
5969
return true;

0 commit comments

Comments
 (0)