Skip to content

Commit

Permalink
feat(ut): added coverage tools
Browse files Browse the repository at this point in the history
  • Loading branch information
arhamj committed May 20, 2024
1 parent 04bcabb commit a11b4f5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
build/
build/
coverage/
10 changes: 5 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
collectCoverage: true,
collectCoverageFrom: ['src/**/*.ts'],
coverageReporters: ['text', 'lcov'],
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/test/'],
testMatch: [
'**/__tests__/**/*.+(ts|tsx|js)',
'**/?(*.)+(spec|test).+(ts|tsx|js)',
],
testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"release": "np --any-branch --no-cleanup --no-tests --no-yarn",
"test": "jest",
"coverage": "npm test -- --coverage",
"lint": "eslint './src/**/*.ts'",
"lint-windows": "eslint ./src/**/*.ts",
"clean": "gts clean",
Expand Down
4 changes: 2 additions & 2 deletions src/utils/functions/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ const isObject = (val: unknown): boolean => {
* @returns The string representation of the value.
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
export function safeStringify(val: any, options: stringifyOptions = defaultOptions): string | undefined {
export function safeStringify(val: any, options: stringifyOptions = defaultOptions): string {
const returnVal = stringifyHelper(val, false, options)
if (returnVal !== undefined) {
return '' + returnVal
}
return undefined
}

/**
Expand All @@ -42,6 +41,7 @@ export function safeStringify(val: any, options: stringifyOptions = defaultOptio
* @param value - The JSON string to parse.
* @returns The parsed JSON object.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function safeJsonParse(value: string): any {
return JSON.parse(value, typeReviver)
}
Expand Down
15 changes: 15 additions & 0 deletions test/src/utils/functions/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,19 @@ describe('safeJsonParse', function () {
const invalidStructureJson = '{"foo": [1, 2, {"dataType": "bi", "value": "invalid"}]}'
expect(() => safeJsonParse(invalidStructureJson)).toThrowError()
})

it('complex nested object test', () => {
const nestedObject = { a: { b: { c: [1, 2, { d: 'test' }], e: { f: 'test' } } } }
expect(safeJsonParse(safeStringify(nestedObject))).toEqual(nestedObject)
})

it('buffer compatibility test with JSON.stringify', () => {
const buffer = Buffer.from('hello')
const obj = {
buf: buffer,
}
expect(safeJsonParse(JSON.stringify(obj))).toEqual({
buf: { type: 'Buffer', data: [104, 101, 108, 108, 111] },
})
})
})

0 comments on commit a11b4f5

Please sign in to comment.