diff --git a/examples/app/functions/commons/powertools/index.ts b/examples/app/functions/commons/powertools/index.ts index 0ccf2b04bd..98398cfe36 100644 --- a/examples/app/functions/commons/powertools/index.ts +++ b/examples/app/functions/commons/powertools/index.ts @@ -2,9 +2,6 @@ * We have single entry point for all the powertools modules so that functions that need only one * can bundle only that one that they need and keep the bundle size small. */ -import { logger } from './logger.js'; -import { metrics } from './metrics.js'; -import { tracer } from './tracer.js'; - -// We export all three modules for those functions who need to use all of them -export { logger, metrics, tracer }; +export { logger } from './logger.js'; +export { metrics } from './metrics.js'; +export { tracer } from './tracer.js'; diff --git a/layers/src/layer-publisher-stack.ts b/layers/src/layer-publisher-stack.ts index 776f6d9dac..c61fea4882 100644 --- a/layers/src/layer-publisher-stack.ts +++ b/layers/src/layer-publisher-stack.ts @@ -122,12 +122,12 @@ export class LayerPublisherStack extends Stack { if (buildFromLocal) { for (const util of utilities) { - // Build latest version of the package - buildCommands.push(`npm run build -w packages/${util}`); - // Pack the package to a .tgz file - buildCommands.push(`npm pack -w packages/${util}`); - // Move the .tgz file to the tmp folder buildCommands.push( + // Build latest version of the package + `npm run build -w packages/${util}`, + // Pack the package to a .tgz file + `npm pack -w packages/${util}`, + // Move the .tgz file to the tmp folder `mv aws-lambda-powertools-${util}-*.tgz ${tmpBuildDir}` ); } diff --git a/layers/tests/e2e/layerPublisher.test.ts b/layers/tests/e2e/layerPublisher.test.ts index f3c7054732..70ecea0d5f 100644 --- a/layers/tests/e2e/layerPublisher.test.ts +++ b/layers/tests/e2e/layerPublisher.test.ts @@ -107,7 +107,7 @@ describe('Layers E2E tests', () => { await testStack.deploy(); // Invoke the lambda function once for each output format and collect the logs - for await (const outputFormat of cases) { + for (const outputFormat of cases) { invocationLogsMap.set( outputFormat, await invokeFunctionOnce({ diff --git a/packages/commons/src/unmarshallDynamoDB.ts b/packages/commons/src/unmarshallDynamoDB.ts index 13dff27de4..7521b07266 100644 --- a/packages/commons/src/unmarshallDynamoDB.ts +++ b/packages/commons/src/unmarshallDynamoDB.ts @@ -14,7 +14,7 @@ const typeHandlers: Record unknown> = { B: (value) => value, BS: (value) => new Set(value), SS: (value) => new Set(value), - BOOL: (value) => Boolean(value), + BOOL: Boolean, N: (value) => convertNumber(value), NS: (value) => new Set((value as Array).map(convertNumber)), L: (value) => (value as Array).map(convertAttributeValue), diff --git a/packages/event-handler/src/appsync-events/RouteHandlerRegistry.ts b/packages/event-handler/src/appsync-events/RouteHandlerRegistry.ts index 4fcd218ef5..c371bfa694 100644 --- a/packages/event-handler/src/appsync-events/RouteHandlerRegistry.ts +++ b/packages/event-handler/src/appsync-events/RouteHandlerRegistry.ts @@ -151,8 +151,11 @@ class RouteHandlerRegistry { * @param path - The path to be converted to a regex string */ static pathToRegexString(path: string): string { - const escapedPath = path.replace(/([.*+?^=!:${}()|[\]/\\])/g, '\\$1'); - return `^${escapedPath.replace(/\\\*/g, '.*')}$`; + const escapedPath = path.replace( + /([.*+?^=!:${}()|[\]/\\])/g, + String.raw`\$1` + ); + return `^${escapedPath.replaceAll('\\*', '.*')}$`; // NOSONAR - Need literal backslash to match escaped asterisks } } diff --git a/packages/event-handler/tests/helpers/factories.ts b/packages/event-handler/tests/helpers/factories.ts index 8f3d8fd328..77fcb729f3 100644 --- a/packages/event-handler/tests/helpers/factories.ts +++ b/packages/event-handler/tests/helpers/factories.ts @@ -1,28 +1,32 @@ -const onPublishEventFactory = ( - events: Array<{ payload: unknown; id: string }> = [ - { - payload: { - event_1: 'data_1', - }, - id: '5f7dfbd1-b8ff-4c20-924e-23b42db467a0', +const defaultChannel = { + path: '/request/channel', + segments: ['request', 'channel'], +}; + +const defaultEvents = [ + { + payload: { + event_1: 'data_1', }, - { - payload: { - event_2: 'data_2', - }, - id: 'ababdf65-a3e6-4c1d-acd3-87466eab433c', + id: '5f7dfbd1-b8ff-4c20-924e-23b42db467a0', + }, + { + payload: { + event_2: 'data_2', }, - { - payload: { - event_3: 'data_3', - }, - id: '8bb2983a-0967-45a0-8243-0aeb8c83d80e', + id: 'ababdf65-a3e6-4c1d-acd3-87466eab433c', + }, + { + payload: { + event_3: 'data_3', }, - ], - channel = { - path: '/request/channel', - segments: ['request', 'channel'], - } + id: '8bb2983a-0967-45a0-8243-0aeb8c83d80e', + }, +]; + +const onPublishEventFactory = ( + events: Array<{ payload: unknown; id: string }> = defaultEvents, + channel = defaultChannel ) => ({ identity: null, result: null, @@ -46,12 +50,7 @@ const onPublishEventFactory = ( events, }); -const onSubscribeEventFactory = ( - channel = { - path: '/request/channel', - segments: ['request', 'channel'], - } -) => ({ +const onSubscribeEventFactory = (channel = defaultChannel) => ({ identity: null, result: null, request: { diff --git a/packages/idempotency/src/makeIdempotent.ts b/packages/idempotency/src/makeIdempotent.ts index c5c2560c71..6d576ef34a 100644 --- a/packages/idempotency/src/makeIdempotent.ts +++ b/packages/idempotency/src/makeIdempotent.ts @@ -126,7 +126,7 @@ function makeIdempotent( options: ItempotentFunctionOptions> ): (...args: Parameters) => ReturnType { const { persistenceStore, config, keyPrefix } = options; - const idempotencyConfig = config ? config : new IdempotencyConfig({}); + const idempotencyConfig = config ?? new IdempotencyConfig({}); if (!idempotencyConfig.isEnabled()) return fn; @@ -140,12 +140,10 @@ function makeIdempotent( args[1]?.lambdaContext || args[1] ); functionPayloadToBeHashed = args[0]; + } else if (isOptionsWithDataIndexArgument(options)) { + functionPayloadToBeHashed = args[options.dataIndexArgument]; } else { - if (isOptionsWithDataIndexArgument(options)) { - functionPayloadToBeHashed = args[options.dataIndexArgument]; - } else { - functionPayloadToBeHashed = args[0]; - } + functionPayloadToBeHashed = args[0]; } const isReplay = args[1]?.durableExecutionMode === 'ReplayMode'; diff --git a/packages/idempotency/src/middleware/makeHandlerIdempotent.ts b/packages/idempotency/src/middleware/makeHandlerIdempotent.ts index cdc5403994..9c86672885 100644 --- a/packages/idempotency/src/middleware/makeHandlerIdempotent.ts +++ b/packages/idempotency/src/middleware/makeHandlerIdempotent.ts @@ -113,9 +113,7 @@ const makeHandlerIdempotent = ( * @param request - The Middy request object */ const before = (request: MiddyLikeRequest): unknown => { - const idempotencyConfig = options.config - ? options.config - : new IdempotencyConfig({}); + const idempotencyConfig = options.config ?? new IdempotencyConfig({}); const persistenceStore = options.persistenceStore; const keyPrefix = options.keyPrefix; persistenceStore.configure({ diff --git a/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts b/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts index 930d41d1db..3443f0353b 100644 --- a/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts +++ b/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts @@ -155,7 +155,7 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer { if (this.isPayloadValidationEnabled() && record.payloadHash !== undefined) { item[this.validationKeyAttr] = { - S: record.payloadHash as string, + S: record.payloadHash, }; } diff --git a/packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts b/packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts index b35b4444dc..53cda832a3 100644 --- a/packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts +++ b/packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import { Logger } from '@aws-lambda-powertools/logger'; import type { Context } from 'aws-lambda'; import { IdempotencyConfig } from '../../src/IdempotencyConfig.js'; @@ -31,7 +32,8 @@ const logger = new Logger(); */ const idempotencyConfig = new IdempotencyConfig({}); const processIdempotently = makeIdempotent( - (record: Record): string => { + async (record: Record): Promise => { + await setTimeout(0); logger.info('Got test event', { record }); return `Processing done: ${record.foo}`; @@ -62,7 +64,11 @@ const idempotencyConfigWithSelection = new IdempotencyConfig({ payloadValidationJmesPath: 'foo', }); const processIdempotentlyCustomized = makeIdempotent( - (baz: number, record: Record): Record => { + async ( + baz: number, + record: Record + ): Promise> => { + await setTimeout(0); logger.info('Got test event', { baz, record }); return record; diff --git a/packages/jmespath/src/Functions.ts b/packages/jmespath/src/Functions.ts index 5773c73b55..4801a1debf 100644 --- a/packages/jmespath/src/Functions.ts +++ b/packages/jmespath/src/Functions.ts @@ -197,8 +197,8 @@ class Functions { if (isNumber(arg[0])) { return Math.max(...(arg as number[])); } - // local compare function to handle string comparison - return arg.reduce((a, b) => (a > b ? a : b)); + // Math.max doesn't work with strings (returns NaN), so we use reduce for lexicographic comparison + return arg.reduce((a, b) => (a > b ? a : b)); // NOSONAR - Math.max only works with numbers } /** @@ -279,7 +279,8 @@ class Functions { if (isNumber(arg[0])) { return Math.min(...arg); } - return arg.reduce((a, b) => (a < b ? a : b)); + // Math.min doesn't work with strings (returns NaN), so we use reduce for lexicographic comparison + return arg.reduce((a, b) => (a < b ? a : b)); // NOSONAR - Math.min only works with numbers } /** diff --git a/packages/jmespath/tests/unit/compliance/base.test.ts b/packages/jmespath/tests/unit/compliance/base.test.ts index 0c75f6dbd1..79c4d68202 100644 --- a/packages/jmespath/tests/unit/compliance/base.test.ts +++ b/packages/jmespath/tests/unit/compliance/base.test.ts @@ -39,19 +39,19 @@ describe('Base tests', () => { expression: 'bad.morebad.morebad', expected: null, }, - ])( - 'should parse a multi-level nested object: $expression', - ({ expression, expected }) => { - // Prepare - const data = { foo: { bar: { baz: 'correct' } } }; + ])('should parse a multi-level nested object: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { foo: { bar: { baz: 'correct' } } }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -62,19 +62,19 @@ describe('Base tests', () => { expression: 'foo.bar', expected: ['one', 'two', 'three'], }, - ])( - 'should parse multi-level objects with arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { foo: { bar: ['one', 'two', 'three'] } }; + ])('should parse multi-level objects with arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { foo: { bar: ['one', 'two', 'three'] } }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -117,17 +117,17 @@ describe('Base tests', () => { expression: 'foo."-1"', expected: 'bar', }, - ])( - 'should parse an object with arrays and numeric values as keys: $expression', - ({ expression, expected }) => { - // Prepare - const data = { foo: { '1': ['one', 'two', 'three'], '-1': 'bar' } }; + ])('should parse an object with arrays and numeric values as keys: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { foo: { '1': ['one', 'two', 'three'], '-1': 'bar' } }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/boolean.test.ts b/packages/jmespath/tests/unit/compliance/boolean.test.ts index bf39dd6803..cd037ca5ec 100644 --- a/packages/jmespath/tests/unit/compliance/boolean.test.ts +++ b/packages/jmespath/tests/unit/compliance/boolean.test.ts @@ -43,25 +43,25 @@ describe('Boolean tests', () => { expression: 'outer.bad||outer.alsobad', expected: null, }, - ])( - 'should support boolean OR comparison: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - outer: { - foo: 'foo', - bar: 'bar', - baz: 'baz', - }, - }; + ])('should support boolean OR comparison: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + outer: { + foo: 'foo', + bar: 'bar', + baz: 'baz', + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -73,26 +73,26 @@ describe('Boolean tests', () => { 'outer.nokey || outer.bool || outer.empty_list || outer.empty_string || outer.foo', expected: 'foo', }, - ])( - 'should support multiple boolean OR comparisons: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - outer: { - foo: 'foo', - bool: false, - empty_list: [], - empty_string: '', - }, - }; + ])('should support multiple boolean OR comparisons: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + outer: { + foo: 'foo', + bool: false, + empty_list: [], + empty_string: '', + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -215,25 +215,25 @@ describe('Boolean tests', () => { expression: '!!Zero', expected: true, }, - ])( - 'should support boolean AND comparison: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - True: true, - False: false, - Number: 5, - EmptyList: [], - Zero: 0, - }; + ])('should support boolean AND comparison: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + True: true, + False: false, + Number: 5, + EmptyList: [], + Zero: 0, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -280,21 +280,21 @@ describe('Boolean tests', () => { expression: 'two < one || three < one', expected: false, }, - ])( - 'should support lesser and equal comparison: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - one: 1, - two: 2, - three: 3, - }; + ])('should support lesser and equal comparison: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + one: 1, + two: 2, + three: 3, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/current.test.ts b/packages/jmespath/tests/unit/compliance/current.test.ts index 3eeb565111..cd14fd502f 100644 --- a/packages/jmespath/tests/unit/compliance/current.test.ts +++ b/packages/jmespath/tests/unit/compliance/current.test.ts @@ -18,20 +18,20 @@ describe('Current operator tests', () => { expression: '@.foo[0]', expected: { name: 'a' }, }, - ])( - 'should support the current operator: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [{ name: 'a' }, { name: 'b' }], - bar: { baz: 'qux' }, - }; + ])('should support the current operator: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [{ name: 'a' }, { name: 'b' }], + bar: { baz: 'qux' }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/escape.test.ts b/packages/jmespath/tests/unit/compliance/escape.test.ts index 8e1ed35ccb..e2a514fe3b 100644 --- a/packages/jmespath/tests/unit/compliance/escape.test.ts +++ b/packages/jmespath/tests/unit/compliance/escape.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest'; import { search } from '../../../src/index.js'; +// NOSONAR - This file contains JMESPath compliance tests that intentionally use escape sequences describe('Escape characters tests', () => { it.each([ { @@ -12,15 +13,15 @@ describe('Escape characters tests', () => { expected: 'space', }, { - expression: '"foo\\nbar"', + expression: '"foo\\nbar"', // NOSONAR - Compliance test requires literal escape sequences expected: 'newline', }, { - expression: '"foo\\"bar"', + expression: '"foo\\"bar"', // NOSONAR - Compliance test requires literal escape sequences expected: 'doublequote', }, { - expression: '"c:\\\\\\\\windows\\\\path"', + expression: '"c:\\\\\\\\windows\\\\path"', // NOSONAR - Compliance test requires literal escape sequences expected: 'windows', }, { @@ -28,33 +29,33 @@ describe('Escape characters tests', () => { expected: 'unix', }, { - expression: '"\\"\\"\\""', + expression: '"\\"\\"\\""', // NOSONAR - Compliance test requires literal escape sequences expected: 'threequotes', }, { expression: '"bar"."baz"', expected: 'qux', }, - ])( - 'should support escaping characters: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - 'foo.bar': 'dot', - 'foo bar': 'space', - 'foo\nbar': 'newline', - 'foo"bar': 'doublequote', - 'c:\\\\windows\\path': 'windows', - '/unix/path': 'unix', - '"""': 'threequotes', - bar: { baz: 'qux' }, - }; + ])('should support escaping characters: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + 'foo.bar': 'dot', + 'foo bar': 'space', + 'foo\nbar': 'newline', + 'foo"bar': 'doublequote', + 'c:\\\\windows\\path': 'windows', // NOSONAR - Compliance test requires literal escape sequences + '/unix/path': 'unix', + '"""': 'threequotes', + bar: { baz: 'qux' }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/filters.test.ts b/packages/jmespath/tests/unit/compliance/filters.test.ts index 8e2d7154d1..c44a7e2d59 100644 --- a/packages/jmespath/tests/unit/compliance/filters.test.ts +++ b/packages/jmespath/tests/unit/compliance/filters.test.ts @@ -24,19 +24,19 @@ describe('Filer operator tests', () => { expression: '*[?[0] == `0`]', expected: [[], []], }, - ])( - 'should match a literal in arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { foo: [0, 1], bar: [2, 3] }; + ])('should match a literal in arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { foo: [0, 1], bar: [2, 3] }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -103,19 +103,19 @@ describe('Filer operator tests', () => { expression: 'foo[?age != `20`]', expected: [{ age: 25 }, { age: 30 }], }, - ])( - 'should match an expression with operators: $expression', - ({ expression, expected }) => { - // Prepare - const data = { foo: [{ age: 20 }, { age: 25 }, { age: 30 }] }; + ])('should match an expression with operators: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { foo: [{ age: 20 }, { age: 25 }, { age: 30 }] }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -155,40 +155,40 @@ describe('Filer operator tests', () => { expression: 'foo[?weight != `33.3`]', expected: [{ weight: 44.4 }, { weight: 55.5 }], }, - ])( - 'should match an expression with comparisons: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [{ weight: 33.3 }, { weight: 44.4 }, { weight: 55.5 }], - }; + ])('should match an expression with comparisons: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [{ weight: 33.3 }, { weight: 44.4 }, { weight: 55.5 }], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: `foo[?top.name == 'a']`, expected: [{ top: { name: 'a' } }], }, - ])( - 'should match with subexpression: $expression', - ({ expression, expected }) => { - // Prepare - const data = { foo: [{ top: { name: 'a' } }, { top: { name: 'b' } }] }; + ])('should match with subexpression: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { foo: [{ top: { name: 'a' } }, { top: { name: 'b' } }] }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -473,31 +473,31 @@ describe('Filer operator tests', () => { { key: [1] }, ], }, - ])( - 'should match with object that have mixed types as values: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { key: true }, - { key: false }, - { key: 0 }, - { key: 1 }, - { key: [0] }, - { key: { bar: [0] } }, - { key: null }, - { key: [1] }, - { key: { a: 2 } }, - ], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should match with object that have mixed types as values: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { key: true }, + { key: false }, + { key: 0 }, + { key: 1 }, + { key: [0] }, + { key: { bar: [0] } }, + { key: null }, + { key: [1] }, + { key: { a: 2 } }, + ], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -559,34 +559,34 @@ describe('Filer operator tests', () => { expression: 'foo[? `false`]', expected: [], }, - ])( - 'should match with falsy values: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { key: true }, - { key: false }, - { key: 0 }, - { key: 0.0 }, - { key: 1 }, - { key: 1.0 }, - { key: [0] }, - { key: null }, - { key: [1] }, - { key: [] }, - { key: {} }, - { key: { a: 2 } }, - ], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should match with falsy values: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { key: true }, + { key: false }, + { key: 0 }, + { key: 0.0 }, + { key: 1 }, + { key: 1.0 }, + { key: [0] }, + { key: null }, + { key: [1] }, + { key: [] }, + { key: {} }, + { key: { a: 2 } }, + ], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -601,85 +601,79 @@ describe('Filer operator tests', () => { expression: 'reservations[].instances[?bar==`1`][]', expected: [{ foo: 2, bar: 1 }], }, - ])( - 'should match with nested objects and arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - reservations: [ - { - instances: [ - { foo: 1, bar: 2 }, - { foo: 1, bar: 3 }, - { foo: 1, bar: 2 }, - { foo: 2, bar: 1 }, - ], - }, - ], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should match with nested objects and arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + reservations: [ + { + instances: [ + { foo: 1, bar: 2 }, + { foo: 1, bar: 3 }, + { foo: 1, bar: 2 }, + { foo: 2, bar: 1 }, + ], + }, + ], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: 'foo[?bar==`1`].bar[0]', expected: [], }, - ])( - 'should match with nested objects and arrays with different structures: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - baz: 'other', - foo: [ - { bar: 1 }, - { bar: 2 }, - { bar: 3 }, - { bar: 4 }, - { bar: 1, baz: 2 }, - ], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should match with nested objects and arrays with different structures: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + baz: 'other', + foo: [{ bar: 1 }, { bar: 2 }, { bar: 3 }, { bar: 4 }, { bar: 1, baz: 2 }], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: 'foo[?a==`1`].b.c', expected: ['x', 'y', 'z'], }, - ])( - 'should support filter in indexes: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { a: 1, b: { c: 'x' } }, - { a: 1, b: { c: 'y' } }, - { a: 1, b: { c: 'z' } }, - { a: 2, b: { c: 'z' } }, - { a: 1, baz: 2 }, - ], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should support filter in indexes: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { a: 1, b: { c: 'x' } }, + { a: 1, b: { c: 'y' } }, + { a: 1, b: { c: 'z' } }, + { a: 2, b: { c: 'z' } }, + { a: 1, baz: 2 }, + ], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -695,19 +689,19 @@ describe('Filer operator tests', () => { expression: `foo[?name == 'a' || name == 'b' || name == 'c']`, expected: [{ name: 'a' }, { name: 'b' }, { name: 'c' }], }, - ])( - 'should support filter with or expressions: $expression', - ({ expression, expected }) => { - // Prepare - const data = { foo: [{ name: 'a' }, { name: 'b' }, { name: 'c' }] }; + ])('should support filter with or expressions: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { foo: [{ name: 'a' }, { name: 'b' }, { name: 'c' }] }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -719,24 +713,24 @@ describe('Filer operator tests', () => { expression: 'foo[?a == `1` && b == `4`]', expected: [], }, - ])( - 'should support filter and expressions: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { a: 1, b: 2 }, - { a: 1, b: 3 }, - ], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should support filter and expressions: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { a: 1, b: 2 }, + { a: 1, b: 3 }, + ], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -780,24 +774,24 @@ describe('Filer operator tests', () => { expression: 'foo[?a == `3` && ((b == `4` || b == `2`))]', expected: [{ a: 3, b: 4 }], }, - ])( - 'should support filter with or & and expressions', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { a: 1, b: 2, c: 3 }, - { a: 3, b: 4 }, - ], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should support filter with or & and expressions', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { a: 1, b: 2, c: 3 }, + { a: 3, b: 4 }, + ], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -815,24 +809,24 @@ describe('Filer operator tests', () => { expression: 'foo[?!(a == `1` || b ==`2`)]', expected: [{ a: 3, b: 4 }], }, - ])( - 'should support filter with expressions and respect precedence: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { a: 1, b: 2, c: 3 }, - { a: 3, b: 4 }, - ], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should support filter with expressions and respect precedence: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { a: 1, b: 2, c: 3 }, + { a: 3, b: 4 }, + ], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -862,32 +856,32 @@ describe('Filer operator tests', () => { expression: 'foo[?key == `null`]', expected: [{ key: null }, { notkey: true }], }, - ])( - 'should support unary expressions: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { key: true }, - { key: false }, - { key: [] }, - { key: {} }, - { key: [0] }, - { key: { a: 'b' } }, - { key: 0 }, - { key: 1 }, - { key: null }, - { notkey: true }, - ], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should support unary expressions: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { key: true }, + { key: false }, + { key: [] }, + { key: {} }, + { key: [0] }, + { key: { a: 'b' } }, + { key: 0 }, + { key: 1 }, + { key: null }, + { notkey: true }, + ], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -905,19 +899,19 @@ describe('Filer operator tests', () => { expression: 'foo[?@ == @]', expected: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], }, - ])( - 'should support using current in a filter: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should support using current in a filter: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/functions.test.ts b/packages/jmespath/tests/unit/compliance/functions.test.ts index 33244946c4..39807c3656 100644 --- a/packages/jmespath/tests/unit/compliance/functions.test.ts +++ b/packages/jmespath/tests/unit/compliance/functions.test.ts @@ -27,35 +27,35 @@ describe('Functions tests', () => { expression: 'abs(`-24`)', expected: 24, }, - ])( - 'should support the abs() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the abs() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -137,35 +137,35 @@ describe('Functions tests', () => { expression: 'avg(numbers)', expected: 2.75, }, - ])( - 'should support the avg() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the avg() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -233,35 +233,35 @@ describe('Functions tests', () => { expression: 'ceil(decimals[2])', expected: -1, }, - ])( - 'should support the ceil() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the ceil() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -313,35 +313,35 @@ describe('Functions tests', () => { expression: 'contains(decimals, `false`)', expected: false, }, - ])( - 'should support the contains() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the contains() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -394,35 +394,35 @@ describe('Functions tests', () => { expression: `ends_with(str, 'foo')`, expected: false, }, - ])( - 'should support the ends_with() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the ends_with() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -467,35 +467,35 @@ describe('Functions tests', () => { expression: 'floor(foo)', expected: -1, }, - ])( - 'should support the floor() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the floor() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -568,35 +568,35 @@ describe('Functions tests', () => { expression: 'length(strings[0])', expected: 1, }, - ])( - 'should support the length() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the length() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -654,35 +654,35 @@ describe('Functions tests', () => { expression: 'max(empty_list)', expected: null, }, - ])( - 'should support the max() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the max() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -750,35 +750,35 @@ describe('Functions tests', () => { d: 4, }, }, - ])( - 'should support the merge() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the merge() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -801,35 +801,35 @@ describe('Functions tests', () => { expression: 'min(strings)', expected: 'a', }, - ])( - 'should support the min() function: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the min() function: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -959,35 +959,35 @@ describe('Functions tests', () => { expression: 'sort(empty_list)', expected: [], }, - ])( - 'should support the sort(), key(), and values() functions', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the sort(), key(), and values() functions', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -1025,32 +1025,32 @@ describe('Functions tests', () => { error: 'Invalid argument type for function sort(), expected one of "array-number", "array-string" but found "object" in expression: sort(@)', }, - ])( - 'sort(), keys(), and values() function errors', - ({ expression, error }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('sort(), keys(), and values() function errors', ({ + expression, + error, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act & Assess - expect(() => search(expression, data)).toThrow(error); - } - ); + // Act & Assess + expect(() => search(expression, data)).toThrow(error); + }); it.each([ { @@ -1213,35 +1213,35 @@ describe('Functions tests', () => { expression: `starts_with(str, 'String')`, expected: false, }, - ])( - 'should support the starts_with() function', - ({ expression, expected }) => { - // Prepare - const data = { - foo: -1, - zero: 0, - numbers: [-1, 3, 4, 5], - array: [-1, 3, 4, 5, 'a', '100'], - strings: ['a', 'b', 'c'], - decimals: [1.01, 1.2, -1.5], - str: 'Str', - false: false, - empty_list: [], - empty_hash: {}, - objects: { - foo: 'bar', - bar: 'baz', - }, - null_key: null, - }; + ])('should support the starts_with() function', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: -1, + zero: 0, + numbers: [-1, 3, 4, 5], + array: [-1, 3, 4, 5, 'a', '100'], + strings: ['a', 'b', 'c'], + decimals: [1.01, 1.2, -1.5], + str: 'Str', + false: false, + empty_list: [], + empty_hash: {}, + objects: { + foo: 'bar', + bar: 'baz', + }, + null_key: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -1564,42 +1564,42 @@ describe('Functions tests', () => { expression: 'foo[].not_null(f, e, d, c, b, a)', expected: ['b', 'c', 'd', 'e', 'f'], }, - ])( - 'should support function projection on variadic function', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { - b: 'b', - a: 'a', - }, - { - c: 'c', - b: 'b', - }, - { - d: 'd', - c: 'c', - }, - { - e: 'e', - d: 'd', - }, - { - f: 'f', - e: 'e', - }, - ], - }; + ])('should support function projection on variadic function', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { + b: 'b', + a: 'a', + }, + { + c: 'c', + b: 'b', + }, + { + d: 'd', + c: 'c', + }, + { + e: 'e', + d: 'd', + }, + { + f: 'f', + e: 'e', + }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -2352,37 +2352,37 @@ describe('Functions tests', () => { expression: 'map(&foo.bar.baz, array)', expected: [null, null, null], }, - ])( - 'should support map() with the `&` expression cases', - ({ expression, expected }) => { - // Prepare - const data = { - array: [ - { - foo: { - bar: 'yes1', - }, + ])('should support map() with the `&` expression cases', ({ + expression, + expected, + }) => { + // Prepare + const data = { + array: [ + { + foo: { + bar: 'yes1', }, - { - foo: { - bar: 'yes2', - }, + }, + { + foo: { + bar: 'yes2', }, - { - foo1: { - bar: 'no', - }, + }, + { + foo1: { + bar: 'no', }, - ], - }; + }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { diff --git a/packages/jmespath/tests/unit/compliance/identifiers.test.ts b/packages/jmespath/tests/unit/compliance/identifiers.test.ts index 852e310ed7..a206dcbfe5 100644 --- a/packages/jmespath/tests/unit/compliance/identifiers.test.ts +++ b/packages/jmespath/tests/unit/compliance/identifiers.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest'; import { search } from '../../../src/index.js'; +// NOSONAR - This file contains JMESPath compliance tests that intentionally use escape sequences describe('Identifiers tests', () => { it.each([ { @@ -878,14 +879,15 @@ describe('Identifiers tests', () => { expression: '"\\uD834\\uDD1E"', expected: true, }, - ])( - 'should handle different identifiers: $expression', - ({ data, expression, expected }) => { - // Act - const result = search(expression, data); + ])('should handle different identifiers: $expression', ({ + data, + expression, + expected, + }) => { + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/indices.test.ts b/packages/jmespath/tests/unit/compliance/indices.test.ts index 74f9927128..318c2f1fb9 100644 --- a/packages/jmespath/tests/unit/compliance/indices.test.ts +++ b/packages/jmespath/tests/unit/compliance/indices.test.ts @@ -35,19 +35,19 @@ describe('Indices tests', () => { expression: 'foo.bar[-4]', expected: null, }, - ])( - 'should support indices on arrays in a nested object: $expression', - ({ expression, expected }) => { - // Prepare - const data = { foo: { bar: ['zero', 'one', 'two'] } }; + ])('should support indices on arrays in a nested object: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { foo: { bar: ['zero', 'one', 'two'] } }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -94,26 +94,26 @@ describe('Indices tests', () => { expression: 'foo[4]', expected: null, }, - ])( - 'should support indices in an array with objects inside: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { bar: 'one' }, - { bar: 'two' }, - { bar: 'three' }, - { notbar: 'four' }, - ], - }; + ])('should support indices in an array with objects inside: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { bar: 'one' }, + { bar: 'two' }, + { bar: 'three' }, + { notbar: 'four' }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -140,19 +140,19 @@ describe('Indices tests', () => { expression: '[-3]', expected: 'one', }, - ])( - 'should support indices in an array: $expression', - ({ expression, expected }) => { - // Prepare - const data = ['one', 'two', 'three']; + ])('should support indices in an array: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = ['one', 'two', 'three']; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -171,19 +171,19 @@ describe('Indices tests', () => { expression: 'reservations[].notinstances[].foo', expected: [], }, - ])( - 'should support indices in multi-level nested arrays & objects: $expression', - ({ expression, expected }) => { - // Prepare - const data = { reservations: [{ instances: [{ foo: 1 }, { foo: 2 }] }] }; + ])('should support indices in multi-level nested arrays & objects: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { reservations: [{ instances: [{ foo: 1 }, { foo: 2 }] }] }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -226,83 +226,58 @@ describe('Indices tests', () => { expression: 'reservations[].instances[].qux[].baz[]', expected: [1, 2, 3, 4, 1, 2, 3, 4], }, - ])( - 'should support indices in large mixed objects and arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - reservations: [ - { - instances: [ - { foo: [{ bar: 1 }, { bar: 2 }, { notbar: 3 }, { bar: 4 }] }, - { foo: [{ bar: 5 }, { bar: 6 }, { notbar: [7] }, { bar: 8 }] }, - { foo: 'bar' }, - { - notfoo: [ - { bar: 20 }, - { bar: 21 }, - { notbar: [7] }, - { bar: 22 }, - ], - }, - { bar: [{ baz: [1] }, { baz: [2] }, { baz: [3] }, { baz: [4] }] }, - { - baz: [ - { baz: [1, 2] }, - { baz: [] }, - { baz: [] }, - { baz: [3, 4] }, - ], - }, - { - qux: [ - { baz: [] }, - { baz: [1, 2, 3] }, - { baz: [4] }, - { baz: [] }, - ], - }, - ], - otherkey: { - foo: [{ bar: 1 }, { bar: 2 }, { notbar: 3 }, { bar: 4 }], + ])('should support indices in large mixed objects and arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + reservations: [ + { + instances: [ + { foo: [{ bar: 1 }, { bar: 2 }, { notbar: 3 }, { bar: 4 }] }, + { foo: [{ bar: 5 }, { bar: 6 }, { notbar: [7] }, { bar: 8 }] }, + { foo: 'bar' }, + { + notfoo: [{ bar: 20 }, { bar: 21 }, { notbar: [7] }, { bar: 22 }], + }, + { bar: [{ baz: [1] }, { baz: [2] }, { baz: [3] }, { baz: [4] }] }, + { + baz: [{ baz: [1, 2] }, { baz: [] }, { baz: [] }, { baz: [3, 4] }], + }, + { + qux: [{ baz: [] }, { baz: [1, 2, 3] }, { baz: [4] }, { baz: [] }], }, + ], + otherkey: { + foo: [{ bar: 1 }, { bar: 2 }, { notbar: 3 }, { bar: 4 }], }, - { - instances: [ - { a: [{ bar: 1 }, { bar: 2 }, { notbar: 3 }, { bar: 4 }] }, - { b: [{ bar: 5 }, { bar: 6 }, { notbar: [7] }, { bar: 8 }] }, - { c: 'bar' }, - { - notfoo: [ - { bar: 23 }, - { bar: 24 }, - { notbar: [7] }, - { bar: 25 }, - ], - }, - { - qux: [ - { baz: [] }, - { baz: [1, 2, 3] }, - { baz: [4] }, - { baz: [] }, - ], - }, - ], - otherkey: { - foo: [{ bar: 1 }, { bar: 2 }, { notbar: 3 }, { bar: 4 }], + }, + { + instances: [ + { a: [{ bar: 1 }, { bar: 2 }, { notbar: 3 }, { bar: 4 }] }, + { b: [{ bar: 5 }, { bar: 6 }, { notbar: [7] }, { bar: 8 }] }, + { c: 'bar' }, + { + notfoo: [{ bar: 23 }, { bar: 24 }, { notbar: [7] }, { bar: 25 }], + }, + { + qux: [{ baz: [] }, { baz: [1, 2, 3] }, { baz: [4] }, { baz: [] }], }, + ], + otherkey: { + foo: [{ bar: 1 }, { bar: 2 }, { notbar: 3 }, { bar: 4 }], }, - ], - }; + }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -336,31 +311,31 @@ describe('Indices tests', () => { expression: 'foo[][0][0][100]', expected: [], }, - ])( - 'should support indices in objects containing an array of matrixes: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - [ - ['one', 'two'], - ['three', 'four'], - ], - [ - ['five', 'six'], - ['seven', 'eight'], - ], - [['nine'], ['ten']], + ])('should support indices in objects containing an array of matrixes: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + [ + ['one', 'two'], + ['three', 'four'], + ], + [ + ['five', 'six'], + ['seven', 'eight'], ], - }; + [['nine'], ['ten']], + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -423,46 +398,46 @@ describe('Indices tests', () => { expression: 'foo[].bar[].baz', expected: [1, 3, 5, 7], }, - ])( - 'should support indices with nested arrays and objects at different levels: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { - bar: [ - { - qux: 2, - baz: 1, - }, - { - qux: 4, - baz: 3, - }, - ], - }, - { - bar: [ - { - qux: 6, - baz: 5, - }, - { - qux: 8, - baz: 7, - }, - ], - }, - ], - }; + ])('should support indices with nested arrays and objects at different levels: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { + bar: [ + { + qux: 2, + baz: 1, + }, + { + qux: 4, + baz: 3, + }, + ], + }, + { + bar: [ + { + qux: 6, + baz: 5, + }, + { + qux: 8, + baz: 7, + }, + ], + }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -501,22 +476,22 @@ describe('Indices tests', () => { expression: 'nullvalue[].foo[].bar', expected: null, }, - ])( - 'should support indices in objects having special names as keys: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - string: 'string', - hash: { foo: 'bar', bar: 'baz' }, - number: 23, - nullvalue: null, - }; + ])('should support indices in objects having special names as keys: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + string: 'string', + hash: { foo: 'bar', bar: 'baz' }, + number: 23, + nullvalue: null, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/literal.test.ts b/packages/jmespath/tests/unit/compliance/literal.test.ts index b94716c3d3..c523fb44b1 100644 --- a/packages/jmespath/tests/unit/compliance/literal.test.ts +++ b/packages/jmespath/tests/unit/compliance/literal.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest'; import { search } from '../../../src/index.js'; +// NOSONAR - This file contains JMESPath compliance tests that intentionally use escape sequences describe('Literal expressions tests', () => { it.each([ { @@ -114,31 +115,31 @@ describe('Literal expressions tests', () => { expression: '`[0, 1, 2]`[1]', expected: 1, }, - ])( - 'should support literal expressions: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { - name: 'a', - }, - { - name: 'b', - }, - ], - bar: { - baz: 'qux', + ])('should support literal expressions: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { + name: 'a', }, - }; + { + name: 'b', + }, + ], + bar: { + baz: 'qux', + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -155,21 +156,21 @@ describe('Literal expressions tests', () => { foo: true, }, }, - ])( - 'should support literals with other special characters: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - type: 'object', - }; + ])('should support literals with other special characters: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + type: 'object', + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -235,17 +236,17 @@ describe('Literal expressions tests', () => { expression: `'foo\\'bar'`, expected: `foo'bar`, }, - ])( - 'should support raw string literals: $expression', - ({ expression, expected }) => { - // Prepare - const data = {}; + ])('should support raw string literals: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = {}; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/multiselect.test.ts b/packages/jmespath/tests/unit/compliance/multiselect.test.ts index 0c72cd7275..f95857ec11 100644 --- a/packages/jmespath/tests/unit/compliance/multiselect.test.ts +++ b/packages/jmespath/tests/unit/compliance/multiselect.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest'; import { search } from '../../../src/index.js'; +// NOSONAR - This file contains JMESPath compliance tests that intentionally use escape sequences describe('Multiselect expressions tests', () => { it.each([ { @@ -103,45 +104,45 @@ describe('Multiselect expressions tests', () => { expression: 'foo.[noexist,alsonoexist]', expected: [null, null], }, - ])( - 'should support expression on large nested objects: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - bar: 'bar', - baz: 'baz', - qux: 'qux', - nested: { - one: { - a: 'first', - b: 'second', - c: 'third', - }, - two: { - a: 'first', - b: 'second', - c: 'third', - }, - three: { - a: 'first', - b: 'second', - c: { inner: 'third' }, - }, + ])('should support expression on large nested objects: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + bar: 'bar', + baz: 'baz', + qux: 'qux', + nested: { + one: { + a: 'first', + b: 'second', + c: 'third', + }, + two: { + a: 'first', + b: 'second', + c: 'third', + }, + three: { + a: 'first', + b: 'second', + c: { inner: 'third' }, }, }, - bar: 1, - baz: 2, - 'qux"': 3, - }; + }, + bar: 1, + baz: 2, + 'qux"': 3, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -168,21 +169,21 @@ describe('Multiselect expressions tests', () => { expression: 'foo.[bar[0],baz[3]]', expected: [null, null], }, - ])( - 'should support the expression on objects containing arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { bar: 1, baz: [2, 3, 4] }, - }; + ])('should support the expression on objects containing arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { bar: 1, baz: [2, 3, 4] }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -193,21 +194,21 @@ describe('Multiselect expressions tests', () => { expression: 'foo.[bar,baz]', expected: [1, 2], }, - ])( - 'should support the expression using both array and object syntax: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { bar: 1, baz: 2 }, - }; + ])('should support the expression using both array and object syntax: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { bar: 1, baz: 2 }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -230,30 +231,30 @@ describe('Multiselect expressions tests', () => { expression: 'foo.[includeme, bar.baz[].common]', expected: [true, ['first', 'second']], }, - ])( - 'should support the expression using mixed array and object syntax: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - bar: { - baz: [ - { common: 'first', one: 1 }, - { common: 'second', two: 2 }, - ], - }, - ignoreme: 1, - includeme: true, + ])('should support the expression using mixed array and object syntax: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + bar: { + baz: [ + { common: 'first', one: 1 }, + { common: 'second', two: 2 }, + ], }, - }; + ignoreme: 1, + includeme: true, + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -287,34 +288,34 @@ describe('Multiselect expressions tests', () => { ['id4', 'fourth'], ], }, - ])( - 'should support the expression with wildcards: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - reservations: [ - { - instances: [ - { id: 'id1', name: 'first' }, - { id: 'id2', name: 'second' }, - ], - }, - { - instances: [ - { id: 'id3', name: 'third' }, - { id: 'id4', name: 'fourth' }, - ], - }, - ], - }; + ])('should support the expression with wildcards: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + reservations: [ + { + instances: [ + { id: 'id1', name: 'first' }, + { id: 'id2', name: 'second' }, + ], + }, + { + instances: [ + { id: 'id3', name: 'third' }, + { id: 'id4', name: 'fourth' }, + ], + }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -390,77 +391,77 @@ describe('Multiselect expressions tests', () => { expression: 'foo[].bar[].[baz, qux][]', expected: [1, 2, 3, 4, 5, 6, 7, 8], }, - ])( - 'should support expression with the flatten operator: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { - bar: [ - { - qux: 2, - baz: 1, - }, - { - qux: 4, - baz: 3, - }, - ], - }, - { - bar: [ - { - qux: 6, - baz: 5, - }, - { - qux: 8, - baz: 7, - }, - ], - }, - ], - }; + ])('should support expression with the flatten operator: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { + bar: [ + { + qux: 2, + baz: 1, + }, + { + qux: 4, + baz: 3, + }, + ], + }, + { + bar: [ + { + qux: 6, + baz: 5, + }, + { + qux: 8, + baz: 7, + }, + ], + }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: 'foo.[baz[*].bar, qux[0]]', expected: [['abc', 'def'], 'zero'], }, - ])( - 'should support the expression with slicing: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - baz: [ - { - bar: 'abc', - }, - { - bar: 'def', - }, - ], - qux: ['zero'], - }, - }; + ])('should support the expression with slicing: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + baz: [ + { + bar: 'abc', + }, + { + bar: 'def', + }, + ], + qux: ['zero'], + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -473,70 +474,70 @@ describe('Multiselect expressions tests', () => { 'zero', ], }, - ])( - 'should support the expression with wildcard slicing: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - baz: [ - { - bar: 'a', - bam: 'b', - boo: 'c', - }, - { - bar: 'd', - bam: 'e', - boo: 'f', - }, - ], - qux: ['zero'], - }, - }; + ])('should support the expression with wildcard slicing: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + baz: [ + { + bar: 'a', + bam: 'b', + boo: 'c', + }, + { + bar: 'd', + bam: 'e', + boo: 'f', + }, + ], + qux: ['zero'], + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: 'foo.[baz[*].not_there || baz[*].bar, qux[0]]', expected: [['a', 'd'], 'zero'], }, - ])( - 'should support multiselect with inexistent values: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - baz: [ - { - bar: 'a', - bam: 'b', - boo: 'c', - }, - { - bar: 'd', - bam: 'e', - boo: 'f', - }, - ], - qux: ['zero'], - }, - }; + ])('should support multiselect with inexistent values: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + baz: [ + { + bar: 'a', + bam: 'b', + boo: 'c', + }, + { + bar: 'd', + bam: 'e', + boo: 'f', + }, + ], + qux: ['zero'], + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -544,36 +545,36 @@ describe('Multiselect expressions tests', () => { expression: '[[*],*]', expected: [null, ['object']], }, - ])( - 'should support nested multiselect: $expression', - ({ expression, expected }) => { - // Prepare - const data = { type: 'object' }; + ])('should support nested multiselect: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { type: 'object' }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: '[[*]]', expected: [[]], }, - ])( - 'should handle nested multiselect with empty arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data: string[] = []; + ])('should handle nested multiselect with empty arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data: string[] = []; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/pipe.test.ts b/packages/jmespath/tests/unit/compliance/pipe.test.ts index 963bbc9264..f001b3153c 100644 --- a/packages/jmespath/tests/unit/compliance/pipe.test.ts +++ b/packages/jmespath/tests/unit/compliance/pipe.test.ts @@ -30,37 +30,37 @@ describe('Pipe expressions tests', () => { expression: '{"a": foo.bar, "b": foo.other} | *.baz', expected: ['subkey', 'subkey'], }, - ])( - 'should support piping a multi-level nested object with arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - bar: { - baz: 'subkey', - }, - other: { - baz: 'subkey', - }, - other2: { - baz: 'subkey', - }, - other3: { - notbaz: ['a', 'b', 'c'], - }, - other4: { - notbaz: ['a', 'b', 'c'], - }, + ])('should support piping a multi-level nested object with arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + bar: { + baz: 'subkey', }, - }; + other: { + baz: 'subkey', + }, + other2: { + baz: 'subkey', + }, + other3: { + notbaz: ['a', 'b', 'c'], + }, + other4: { + notbaz: ['a', 'b', 'c'], + }, + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -103,37 +103,37 @@ describe('Pipe expressions tests', () => { expression: 'foo | not_there || bar', expected: { baz: 'one' }, }, - ])( - 'should support piping with boolean conditions: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - bar: { - baz: 'one', - }, - other: { - baz: 'two', - }, - other2: { - baz: 'three', - }, - other3: { - notbaz: ['a', 'b', 'c'], - }, - other4: { - notbaz: ['d', 'e', 'f'], - }, + ])('should support piping with boolean conditions: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + bar: { + baz: 'one', + }, + other: { + baz: 'two', + }, + other2: { + baz: 'three', }, - }; + other3: { + notbaz: ['a', 'b', 'c'], + }, + other4: { + notbaz: ['d', 'e', 'f'], + }, + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -144,40 +144,40 @@ describe('Pipe expressions tests', () => { expression: '`null`|[@]', expected: null, }, - ])( - 'should support piping with wildcard and current operators: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { - bar: [ - { - baz: 'one', - }, - { - baz: 'two', - }, - ], - }, - { - bar: [ - { - baz: 'three', - }, - { - baz: 'four', - }, - ], - }, - ], - }; + ])('should support piping with wildcard and current operators: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { + bar: [ + { + baz: 'one', + }, + { + baz: 'two', + }, + ], + }, + { + bar: [ + { + baz: 'three', + }, + { + baz: 'four', + }, + ], + }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/slice.test.ts b/packages/jmespath/tests/unit/compliance/slice.test.ts index 9807fcb004..ce9a6c173b 100644 --- a/packages/jmespath/tests/unit/compliance/slice.test.ts +++ b/packages/jmespath/tests/unit/compliance/slice.test.ts @@ -111,24 +111,24 @@ describe('Slices tests', () => { expression: 'foo[:-5:-1]', expected: [9, 8, 7, 6], }, - ])( - 'should support slicing arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], - bar: { - baz: 1, - }, - }; + ])('should support slicing arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + bar: { + baz: 1, + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -150,18 +150,18 @@ describe('Slices tests', () => { error: 'Invalid jmespath expression: parse error at column 6, found unexpected token "a" (unquoted_identifier) in expression: foo[2:a:3]', }, - ])( - 'slicing objects with arrays errors: $expression', - ({ expression, error }) => { - // Prepare - const data = { - type: 'object', - }; + ])('slicing objects with arrays errors: $expression', ({ + expression, + error, + }) => { + // Prepare + const data = { + type: 'object', + }; - // Act & Assess - expect(() => search(expression, data)).toThrow(error); - } - ); + // Act & Assess + expect(() => search(expression, data)).toThrow(error); + }); it.each([ { @@ -188,23 +188,23 @@ describe('Slices tests', () => { expression: 'baz[:2].a', expected: null, }, - ])( - 'should support slicing an object with nested arrays with objects in them: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [{ a: 1 }, { a: 2 }, { a: 3 }], - bar: [{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 3 } }], - baz: 50, - }; + ])('should support slicing an object with nested arrays with objects in them: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [{ a: 1 }, { a: 2 }, { a: 3 }], + bar: [{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 3 } }], + baz: 50, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -223,17 +223,17 @@ describe('Slices tests', () => { expression: '[:2].b', expected: [], }, - ])( - 'should support slicing an array with objects in it: $expression', - ({ expression, expected }) => { - // Prepare - const data = [{ a: 1 }, { a: 2 }, { a: 3 }]; + ])('should support slicing an array with objects in it: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = [{ a: 1 }, { a: 2 }, { a: 3 }]; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/syntax.test.ts b/packages/jmespath/tests/unit/compliance/syntax.test.ts index b606b5f100..51b33c39b1 100644 --- a/packages/jmespath/tests/unit/compliance/syntax.test.ts +++ b/packages/jmespath/tests/unit/compliance/syntax.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest'; import { search } from '../../../src/index.js'; +// NOSONAR - This file contains JMESPath compliance tests that intentionally use escape sequences describe('Syntax tests', () => { it.each([ { @@ -184,21 +185,21 @@ describe('Syntax tests', () => { expression: '*[0]', expected: [], }, - ])( - 'should support wildcard syntax: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - type: 'object', - }; + ])('should support wildcard syntax: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + type: 'object', + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -241,21 +242,21 @@ describe('Syntax tests', () => { expression: '[]', expected: null, }, - ])( - 'should support flatten syntax: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - type: 'object', - }; + ])('should support flatten syntax: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + type: 'object', + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -331,21 +332,21 @@ describe('Syntax tests', () => { expression: 'foo.[abc, def]', expected: null, }, - ])( - 'should support multi-select list syntax: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - type: 'object', - }; + ])('should support multi-select list syntax: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + type: 'object', + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -462,19 +463,19 @@ describe('Syntax tests', () => { }, }, }, - ])( - 'should support multy-select hash syntax: $expression', - ({ expression, expected }) => { - // Prepare - const data = { type: 'object' }; + ])('should support multi-select hash syntax: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { type: 'object' }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -603,21 +604,21 @@ describe('Syntax tests', () => { expression: 'foo.[a || b]', expected: null, }, - ])( - 'should support boolean OR syntax: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - type: 'object', - }; + ])('should support boolean OR syntax: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + type: 'object', + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -696,21 +697,21 @@ describe('Syntax tests', () => { expression: '[?"\\\\" > `"foo"`]', expected: null, }, - ])( - 'should support filter syntax: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - type: 'object', - }; + ])('should support filter syntax: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + type: 'object', + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -867,17 +868,17 @@ describe('Syntax tests', () => { expression: '[*.*]', expected: [null], }, - ])( - 'should support combined syntax: $expression', - ({ expression, expected }) => { - // Prepare - const data: string[] = []; + ])('should support combined syntax: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data: string[] = []; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/unicode.test.ts b/packages/jmespath/tests/unit/compliance/unicode.test.ts index ae64493eeb..e1b7309ee0 100644 --- a/packages/jmespath/tests/unit/compliance/unicode.test.ts +++ b/packages/jmespath/tests/unit/compliance/unicode.test.ts @@ -7,19 +7,19 @@ describe('Unicode tests', () => { expression: 'foo[]."✓"', expected: ['✓', '✗'], }, - ])( - 'should parse an object with unicode chars as keys and values: $expression', - ({ expression, expected }) => { - // Prepare - const data = { foo: [{ '✓': '✓' }, { '✓': '✗' }] }; + ])('should parse an object with unicode chars as keys and values: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { foo: [{ '✓': '✓' }, { '✓': '✗' }] }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -30,36 +30,36 @@ describe('Unicode tests', () => { expression: '"☃"', expected: null, }, - ])( - 'should parse an object with unicode chars as keys: $expression', - ({ expression, expected }) => { - // Prepare - const data = { '☯': true }; + ])('should parse an object with unicode chars as keys: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { '☯': true }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: '"♪♫•*¨*•.¸¸❤¸¸.•*¨*•♫♪"', expected: true, }, - ])( - 'should parse an object with mulitple unicode chars as keys: $expression', - ({ expression, expected }) => { - // Prepare - const data = { '♪♫•*¨*•.¸¸❤¸¸.•*¨*•♫♪': true }; + ])('should parse an object with mulitple unicode chars as keys: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { '♪♫•*¨*•.¸¸❤¸¸.•*¨*•♫♪': true }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/jmespath/tests/unit/compliance/wildcard.test.ts b/packages/jmespath/tests/unit/compliance/wildcard.test.ts index 6cd31c9e84..1423f5a9a2 100644 --- a/packages/jmespath/tests/unit/compliance/wildcard.test.ts +++ b/packages/jmespath/tests/unit/compliance/wildcard.test.ts @@ -26,44 +26,44 @@ describe('Wildcard tests', () => { expression: 'foo.*.notbaz[-1]', expected: ['c', 'c'], }, - ])( - 'should parse the wildcard operator with an object containing multiple keys at different levels: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - bar: { - baz: 'val', - }, + ])('should parse the wildcard operator with an object containing multiple keys at different levels: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + bar: { + baz: 'val', + }, + other: { + baz: 'val', + }, + other2: { + baz: 'val', + }, + other3: { + notbaz: ['a', 'b', 'c'], + }, + other4: { + notbaz: ['a', 'b', 'c'], + }, + other5: { other: { - baz: 'val', - }, - other2: { - baz: 'val', - }, - other3: { - notbaz: ['a', 'b', 'c'], - }, - other4: { - notbaz: ['a', 'b', 'c'], - }, - other5: { - other: { - a: 1, - b: 1, - c: 1, - }, + a: 1, + b: 1, + c: 1, }, }, - }; + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -86,60 +86,60 @@ describe('Wildcard tests', () => { expression: 'foo.*.*.*.*', expected: [[], [], []], }, - ])( - 'should parse the wildcard operator with an object containing keys with hyphens: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - 'first-1': { - 'second-1': 'val', - }, - 'first-2': { - 'second-1': 'val', - }, - 'first-3': { - 'second-1': 'val', - }, + ])('should parse the wildcard operator with an object containing keys with hyphens: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + 'first-1': { + 'second-1': 'val', + }, + 'first-2': { + 'second-1': 'val', }, - }; + 'first-3': { + 'second-1': 'val', + }, + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: '*.bar', expected: ['one', 'one'], }, - ])( - 'should parse the wildcard operator with an object containing multiple keys: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - bar: 'one', - }, - other: { - bar: 'one', - }, - nomatch: { - notbar: 'three', - }, - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should parse the wildcard operator with an object containing multiple keys: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + bar: 'one', + }, + other: { + bar: 'one', + }, + nomatch: { + notbar: 'three', + }, + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -162,26 +162,26 @@ describe('Wildcard tests', () => { expression: '*.sub1.foo', expected: ['one', 'one'], }, - ])( - 'should parse the wildcard operator with an object containing nested objects: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - top1: { - sub1: { foo: 'one' }, - }, - top2: { - sub1: { foo: 'one' }, - }, - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should parse the wildcard operator with an object containing nested objects: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + top1: { + sub1: { foo: 'one' }, + }, + top2: { + sub1: { foo: 'one' }, + }, + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -192,26 +192,26 @@ describe('Wildcard tests', () => { expression: 'foo[*].notbar', expected: ['four'], }, - ])( - 'should parse the wildcard operator with an object containing an array of objects: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { bar: 'one' }, - { bar: 'two' }, - { bar: 'three' }, - { notbar: 'four' }, - ], - }; + ])('should parse the wildcard operator with an object containing an array of objects: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { bar: 'one' }, + { bar: 'two' }, + { bar: 'three' }, + { notbar: 'four' }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -231,24 +231,24 @@ describe('Wildcard tests', () => { expression: '[*].notbar', expected: ['four'], }, - ])( - 'should parse the wildcard operator with an array of objects: $expression', - ({ expression, expected }) => { - // Prepare - const data = [ - { bar: 'one' }, - { bar: 'two' }, - { bar: 'three' }, - { notbar: 'four' }, - ]; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should parse the wildcard operator with an array of objects: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = [ + { bar: 'one' }, + { bar: 'two' }, + { bar: 'three' }, + { notbar: 'four' }, + ]; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -275,27 +275,27 @@ describe('Wildcard tests', () => { expression: 'foo.bar[*].baz[3]', expected: [], }, - ])( - 'should parse the wildcard operator with an object with nested objects containing arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - bar: [ - { baz: ['one', 'two', 'three'] }, - { baz: ['four', 'five', 'six'] }, - { baz: ['seven', 'eight', 'nine'] }, - ], - }, - }; + ])('should parse the wildcard operator with an object with nested objects containing arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + bar: [ + { baz: ['one', 'two', 'three'] }, + { baz: ['four', 'five', 'six'] }, + { baz: ['seven', 'eight', 'nine'] }, + ], + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -325,26 +325,26 @@ describe('Wildcard tests', () => { expression: 'foo[0][0]', expected: null, }, - ])( - 'should parse the wildcard operator with an object with nested arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: { - bar: [ - ['one', 'two'], - ['three', 'four'], - ], - }, - }; + ])('should parse the wildcard operator with an object with nested arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: { + bar: [ + ['one', 'two'], + ['three', 'four'], + ], + }, + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -358,52 +358,52 @@ describe('Wildcard tests', () => { expression: 'foo[*].bar[0].kind', expected: ['basic', 'advanced'], }, - ])( - 'should parse the wildcard operator with an array of objects with nested arrays or strings: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { bar: [{ kind: 'basic' }, { kind: 'intermediate' }] }, - { bar: [{ kind: 'advanced' }, { kind: 'expert' }] }, - { bar: 'string' }, - ], - }; + ])('should parse the wildcard operator with an array of objects with nested arrays or strings: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { bar: [{ kind: 'basic' }, { kind: 'intermediate' }] }, + { bar: [{ kind: 'advanced' }, { kind: 'expert' }] }, + { bar: 'string' }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: 'foo[*].bar.kind', expected: ['basic', 'intermediate', 'advanced', 'expert'], }, - ])( - 'should parse the wildcard operator with an array of objects: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { bar: { kind: 'basic' } }, - { bar: { kind: 'intermediate' } }, - { bar: { kind: 'advanced' } }, - { bar: { kind: 'expert' } }, - { bar: 'string' }, - ], - }; + ])('should parse the wildcard operator with an array of objects: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { bar: { kind: 'basic' } }, + { bar: { kind: 'intermediate' } }, + { bar: { kind: 'advanced' } }, + { bar: { kind: 'expert' } }, + { bar: 'string' }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -418,46 +418,46 @@ describe('Wildcard tests', () => { expression: 'foo[*].bar[2]', expected: [], }, - ])( - 'should parse the wildcard operator with an array of objects with arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - { bar: ['one', 'two'] }, - { bar: ['three', 'four'] }, - { bar: ['five'] }, - ], - }; + ])('should parse the wildcard operator with an array of objects with arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + { bar: ['one', 'two'] }, + { bar: ['three', 'four'] }, + { bar: ['five'] }, + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { expression: 'foo[*].bar[0]', expected: [], }, - ])( - 'should parse the wildcard operator with an array of objects with empty arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [{ bar: [] }, { bar: [] }, { bar: [] }], - }; + ])('should parse the wildcard operator with an array of objects with empty arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [{ bar: [] }, { bar: [] }, { bar: [] }], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -468,21 +468,21 @@ describe('Wildcard tests', () => { expression: 'foo[*][1]', expected: ['two', 'four'], }, - ])( - 'should parse the wildcard operator with an array of arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [['one', 'two'], ['three', 'four'], ['five']], - }; + ])('should parse the wildcard operator with an array of arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [['one', 'two'], ['three', 'four'], ['five']], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -525,31 +525,31 @@ describe('Wildcard tests', () => { expression: 'bar[*].baz[*]', expected: null, }, - ])( - 'should parse a nested array of arrays: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - foo: [ - [ - ['one', 'two'], - ['three', 'four'], - ], - [ - ['five', 'six'], - ['seven', 'eight'], - ], - [['nine'], ['ten']], + ])('should parse a nested array of arrays: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + foo: [ + [ + ['one', 'two'], + ['three', 'four'], ], - }; + [ + ['five', 'six'], + ['seven', 'eight'], + ], + [['nine'], ['ten']], + ], + }; - // Act - const result = search(expression, data); + // Act + const result = search(expression, data); - // Assess - expect(result).toStrictEqual(expected); - } - ); + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -588,24 +588,24 @@ describe('Wildcard tests', () => { expression: 'nullvalue[*].foo[*].bar', expected: null, }, - ])( - 'should parse an object with different value types: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - string: 'string', - hash: { foo: 'bar', bar: 'baz' }, - number: 23, - nullvalue: null, - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should parse an object with different value types: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + string: 'string', + hash: { foo: 'bar', bar: 'baz' }, + number: 23, + nullvalue: null, + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); it.each([ { @@ -628,39 +628,41 @@ describe('Wildcard tests', () => { expression: 'nullvalue.*', expected: null, }, - ])( - 'should parse an object with different value types: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - string: 'string', - hash: { foo: 'val', bar: 'val' }, - number: 23, - array: [1, 2, 3], - nullvalue: null, - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); - it.each([{ expression: '*[0]', expected: [0, 0] }])( - 'should get the first element of each array: $expression', - ({ expression, expected }) => { - // Prepare - const data = { - a: [0, 1, 2], - b: [0, 1, 2], - }; - - // Act - const result = search(expression, data); - - // Assess - expect(result).toStrictEqual(expected); - } - ); + ])('should parse an object with different value types: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + string: 'string', + hash: { foo: 'val', bar: 'val' }, + number: 23, + array: [1, 2, 3], + nullvalue: null, + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); + it.each([ + { expression: '*[0]', expected: [0, 0] }, + ])('should get the first element of each array: $expression', ({ + expression, + expected, + }) => { + // Prepare + const data = { + a: [0, 1, 2], + b: [0, 1, 2], + }; + + // Act + const result = search(expression, data); + + // Assess + expect(result).toStrictEqual(expected); + }); }); diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index bef8468c12..6886c735a2 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -253,9 +253,7 @@ class Logger extends Utility implements LoggerInterface { public constructor(options: ConstructorOptions = {}) { super(); const { customConfigService, ...rest } = options; - this.customConfigService = customConfigService - ? customConfigService - : undefined; + this.customConfigService = customConfigService; // all logs are buffered until the logger is initialized this.setOptions(rest); this.#isInitialized = true; @@ -1098,13 +1096,13 @@ class Logger extends Utility implements LoggerInterface { * or as the global node console if the `POWERTOOLS_DEV' env variable is set and has truthy value. */ private setConsole(): void { - if (!isDevMode()) { + if (isDevMode()) { + this.console = console; + } else { this.console = new Console({ stdout: process.stdout, stderr: process.stderr, }); - } else { - this.console = console; } /** @@ -1159,8 +1157,7 @@ class Logger extends Utility implements LoggerInterface { defaultValue: '', }); - const logLevelValue = - logLevelVariable !== '' ? logLevelVariable : logLevelVariableAlias; + const logLevelValue = logLevelVariable || logLevelVariableAlias; if (this.isValidLogLevel(logLevelValue)) { this.logLevel = LogLevelThreshold[logLevelValue]; diff --git a/packages/logger/src/types/Logger.ts b/packages/logger/src/types/Logger.ts index b0b71a990d..a3e64ea075 100644 --- a/packages/logger/src/types/Logger.ts +++ b/packages/logger/src/types/Logger.ts @@ -11,6 +11,8 @@ import type { LogKeys, } from './logKeys.js'; +export type { Environment, LogAttributes } from './logKeys.js'; + /** * Type definition for the log level. * @@ -276,9 +278,7 @@ type LoggerInterface = { export type { ConstructorOptions, CustomJsonReplacerFn, - Environment, InjectLambdaContextOptions, - LogAttributes, LogFunction, LoggerInterface, LogItemExtraInput, diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index d6d2820069..b15c1dfce0 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -885,13 +885,13 @@ class Metrics extends Utility implements MetricsInterface { * @private */ private setConsole(): void { - if (!this.#envConfig.devMode) { + if (this.#envConfig.devMode) { + this.console = console; + } else { this.console = new Console({ stdout: process.stdout, stderr: process.stderr, }); - } else { - this.console = console; } } @@ -903,9 +903,7 @@ class Metrics extends Utility implements MetricsInterface { private setCustomConfigService( customConfigService?: ConfigServiceInterface ): void { - this.customConfigService = customConfigService - ? customConfigService - : undefined; + this.customConfigService = customConfigService; } /** @@ -976,7 +974,7 @@ class Metrics extends Utility implements MetricsInterface { * * @param options - The options to be used */ - private setOptions(options: MetricsOptions): Metrics { + private setOptions(options: MetricsOptions): this { const { customConfigService, namespace, diff --git a/packages/testing/src/helpers.ts b/packages/testing/src/helpers.ts index 7e3f417e67..37578661c0 100644 --- a/packages/testing/src/helpers.ts +++ b/packages/testing/src/helpers.ts @@ -62,7 +62,7 @@ const generateTestUniqueName = ({ [ testPrefix, getRuntimeKey().replace(/[nodejsx]/g, ''), - getArchitectureKey().replace(/_64/g, ''), + getArchitectureKey().replaceAll('_64', ''), randomUUID().toString().substring(0, 5), testName, ] diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index 4797c52e8d..a190935c11 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -851,9 +851,7 @@ class Tracer extends Utility implements TracerInterface { private setCustomConfigService( customConfigService?: ConfigServiceInterface ): void { - this.customConfigService = customConfigService - ? customConfigService - : undefined; + this.customConfigService = customConfigService; } /** diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000000..72fe9e1698 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,2 @@ +# Exclude generated proto files from Sonar analysis +sonar.exclusions=packages/kafka/tests/protos/*.generated.js,packages/kafka/tests/protos/*.generated.d.ts \ No newline at end of file