diff --git a/.github/workflows/reusable-run-linting-check-and-unit-tests.yml b/.github/workflows/reusable-run-linting-check-and-unit-tests.yml index 0dba209acf..93d0f5ddd4 100644 --- a/.github/workflows/reusable-run-linting-check-and-unit-tests.yml +++ b/.github/workflows/reusable-run-linting-check-and-unit-tests.yml @@ -40,7 +40,7 @@ jobs: strategy: matrix: version: [18, 20] - workspace: ["packages/batch"] + workspace: ["packages/batch", "packages/commons"] fail-fast: false steps: - name: Checkout code @@ -57,7 +57,9 @@ jobs: - name: Linting run: npm run lint -w ${{ matrix.workspace }} - name: Unit tests - run: npm run test:unit:coverage -w ${{ matrix.workspace }} + run: | + npm run test:unit:coverage -w ${{ matrix.workspace }} + npm run test:unit:types -w ${{ matrix.workspace }} run-linting-check-and-unit-tests-on-utilities: runs-on: ubuntu-latest env: @@ -92,8 +94,7 @@ jobs: -w packages/event-handler - name: Run unit tests run: | - npm t -w packages/commons \ - -w packages/logger \ + npm t -w packages/logger \ -w packages/tracer \ -w packages/metrics \ -w packages/parameters \ diff --git a/.husky/pre-push b/.husky/pre-push index 745483f08e..a011532b30 100755 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -1,5 +1,4 @@ npm t \ - -w packages/commons \ -w packages/jmespath \ -w packages/logger \ -w packages/metrics \ diff --git a/packages/batch/package.json b/packages/batch/package.json index 655f18c0c2..d99b7c91b2 100644 --- a/packages/batch/package.json +++ b/packages/batch/package.json @@ -12,11 +12,11 @@ "scripts": { "test": "vitest --run", "test:unit": "vitest --run", - "test:unit:coverage": "vitest --run --coverage.enabled --coverage.thresholds.100 --coverage.include='src/**'", + "test:unit:coverage": "vitest --run tests/unit --coverage.enabled --coverage.thresholds.100 --coverage.include='src/**'", + "test:unit:types": "echo 'Not Implemented'", "test:e2e:nodejs18x": "echo 'Not Implemented'", "test:e2e:nodejs20x": "echo 'Not Implemented'", "test:e2e": "echo 'Not Implemented'", - "watch": "jest --watch", "build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json", "build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json", "build": "npm run build:esm & npm run build:cjs", diff --git a/packages/commons/jest.config.cjs b/packages/commons/jest.config.cjs deleted file mode 100644 index 957bbe96d7..0000000000 --- a/packages/commons/jest.config.cjs +++ /dev/null @@ -1,29 +0,0 @@ -module.exports = { - displayName: { - name: 'Powertools for AWS Lambda (TypeScript) utility: COMMONS', - color: 'red', - }, - preset: 'ts-jest', - moduleNameMapper: { - '^(\\.{1,2}/.*)\\.js$': '$1', - }, - transform: { - '^.+\\.ts?$': 'ts-jest', - }, - moduleFileExtensions: ['js', 'ts'], - collectCoverageFrom: ['**/src/**/*.ts', '!**/node_modules/**'], - testMatch: ['**/?(*.)+(spec|test).ts'], - roots: ['/src', '/tests'], - testPathIgnorePatterns: ['/node_modules/'], - testEnvironment: 'node', - coveragePathIgnorePatterns: ['/node_modules/', 'src/types/index.ts'], - coverageThreshold: { - global: { - statements: 100, - branches: 100, - functions: 100, - lines: 100, - }, - }, - coverageReporters: ['json-summary', 'text', 'lcov'], -}; diff --git a/packages/commons/package.json b/packages/commons/package.json index 1be24df095..cbe6cf6b8e 100644 --- a/packages/commons/package.json +++ b/packages/commons/package.json @@ -10,11 +10,11 @@ "access": "public" }, "scripts": { - "test": "npm run test:unit", - "test:unit": "jest --group=unit --detectOpenHandles --coverage --verbose", - "jest": "jest --detectOpenHandles --verbose", + "test": "vitest --run", + "test:unit": "vitest --run", + "test:unit:coverage": "vitest --run tests/unit --coverage.enabled --coverage.thresholds.100 --coverage.include='src/**'", + "test:unit:types": "vitest --run tests/types --typecheck", "test:e2e": "echo 'Not Applicable'", - "watch": "jest --watch", "generateVersionFile": "echo \"// this file is auto generated, do not modify\nexport const PT_VERSION = '$(jq -r '.version' package.json)';\" > src/version.ts", "build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json", "build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json", diff --git a/packages/commons/tests/types/LambdaInterface.test.ts b/packages/commons/tests/types/LambdaInterface.test.ts new file mode 100644 index 0000000000..5b50d083fe --- /dev/null +++ b/packages/commons/tests/types/LambdaInterface.test.ts @@ -0,0 +1,41 @@ +import type { Callback, Context } from 'aws-lambda'; +import { describe, expectTypeOf, it } from 'vitest'; +import type { LambdaInterface } from '../../src/types/index.js'; + +describe('Type: LambdaInterface', () => { + it('works with a sync handler', () => { + // Prepare + class Lambda implements LambdaInterface { + public handler(_event: unknown, context: Context, _callback: Callback) { + context.getRemainingTimeInMillis(); + _callback(null, 'Hello World'); + } + } + + // Act + const lambda = new Lambda(); + + // Assess + expectTypeOf(lambda).toBeObject(); + expectTypeOf(lambda).toHaveProperty('handler'); + expectTypeOf(lambda.handler).toBeFunction(); + }); + + it('works with an async handler', async () => { + // Prepare + class Lambda implements LambdaInterface { + public async handler(_event: unknown, context: Context) { + context.getRemainingTimeInMillis(); + return 'Hello World'; + } + } + + // Act + const lambda = new Lambda(); + + // Assess + expectTypeOf(lambda).toBeObject(); + expectTypeOf(lambda).toHaveProperty('handler'); + expectTypeOf(lambda.handler).toBeFunction(); + }); +}); diff --git a/packages/commons/tests/unit/EnvironmentVariablesService.test.ts b/packages/commons/tests/unit/EnvironmentVariablesService.test.ts index 2c7b7266f3..f941dbf6cb 100644 --- a/packages/commons/tests/unit/EnvironmentVariablesService.test.ts +++ b/packages/commons/tests/unit/EnvironmentVariablesService.test.ts @@ -1,15 +1,11 @@ -/** - * Test EnvironmentVariablesService class - * - * @group unit/commons/environmentService - */ +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'; import { EnvironmentVariablesService } from '../../src/index.js'; describe('Class: EnvironmentVariablesService', () => { const ENVIRONMENT_VARIABLES = process.env; beforeEach(() => { - jest.resetModules(); + vi.resetModules(); process.env = { ...ENVIRONMENT_VARIABLES }; }); @@ -18,7 +14,7 @@ describe('Class: EnvironmentVariablesService', () => { }); describe('Method: get', () => { - test('When the variable IS present, it returns the value of a runtime variable', () => { + it('returns the value of a runtime variable', () => { // Prepare process.env.CUSTOM_VARIABLE = 'my custom value'; const service = new EnvironmentVariablesService(); @@ -30,7 +26,7 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual('my custom value'); }); - test('When the variable IS NOT present, it returns an empty string', () => { + it('returns an empty string when the env variable is not present', () => { // Prepare process.env.CUSTOM_VARIABLE = undefined; const service = new EnvironmentVariablesService(); @@ -44,7 +40,7 @@ describe('Class: EnvironmentVariablesService', () => { }); describe('Method: getServiceName', () => { - test('It returns the value of the environment variable POWERTOOLS_SERVICE_NAME', () => { + it('returns the value of the environment variable POWERTOOLS_SERVICE_NAME', () => { // Prepare process.env.POWERTOOLS_SERVICE_NAME = 'shopping-cart-api'; const service = new EnvironmentVariablesService(); @@ -58,7 +54,7 @@ describe('Class: EnvironmentVariablesService', () => { }); describe('Method: getXrayTraceId', () => { - test('It returns the value of the environment variable _X_AMZN_TRACE_ID', () => { + it('returns the value of the environment variable _X_AMZN_TRACE_ID', () => { // Prepare process.env._X_AMZN_TRACE_ID = 'abcd123456789'; const service = new EnvironmentVariablesService(); @@ -69,7 +65,7 @@ describe('Class: EnvironmentVariablesService', () => { // Assess expect(value).toEqual('abcd123456789'); }); - test('It returns the value of the Root X-Ray segment ID properly formatted', () => { + it('returns the value of the Root X-Ray segment ID properly formatted', () => { // Prepare process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1'; @@ -82,7 +78,7 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual('1-5759e988-bd862e3fe1be46a994272793'); }); - test('It returns the value of the Root X-Ray segment ID properly formatted', () => { + it('returns the value of the Root X-Ray segment ID properly formatted', () => { // Prepare process.env._X_AMZN_TRACE_ID = undefined; const service = new EnvironmentVariablesService(); @@ -96,7 +92,7 @@ describe('Class: EnvironmentVariablesService', () => { }); describe('Method: getXrayTraceSampled', () => { - test('It returns true if the Sampled flag is set in the _X_AMZN_TRACE_ID environment variable', () => { + it('returns true if the Sampled flag is set in the _X_AMZN_TRACE_ID environment variable', () => { // Prepare process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1'; @@ -109,7 +105,7 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(true); }); - test('It returns false if the Sampled flag is not set in the _X_AMZN_TRACE_ID environment variable', () => { + it('returns false if the Sampled flag is not set in the _X_AMZN_TRACE_ID environment variable', () => { // Prepare process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047'; @@ -122,7 +118,7 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(false); }); - it('It returns false when no _X_AMZN_TRACE_ID environment variable is present', () => { + it('returns false when no _X_AMZN_TRACE_ID environment variable is present', () => { // Prepare process.env._X_AMZN_TRACE_ID = undefined; const service = new EnvironmentVariablesService(); @@ -150,8 +146,8 @@ describe('Class: EnvironmentVariablesService', () => { ['0', false], ]; - test.each(valuesToTest)( - 'it takes string "%s" and returns %s', + it.each(valuesToTest)( + 'takes string "%s" and returns %s', (input, output) => { // Prepare const service = new EnvironmentVariablesService(); @@ -164,7 +160,7 @@ describe('Class: EnvironmentVariablesService', () => { }); describe('Method: isDevMode', () => { - test('it returns true if the environment variable POWERTOOLS_DEV is "true"', () => { + it('returns true if the environment variable POWERTOOLS_DEV is "true"', () => { // Prepare process.env.POWERTOOLS_DEV = 'true'; const service = new EnvironmentVariablesService(); @@ -176,7 +172,7 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(true); }); - test('it returns false if the environment variable POWERTOOLS_DEV is "false"', () => { + it('returns false if the environment variable POWERTOOLS_DEV is "false"', () => { // Prepare process.env.POWERTOOLS_DEV = 'false'; const service = new EnvironmentVariablesService(); @@ -188,7 +184,7 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(false); }); - test('it returns false if the environment variable POWERTOOLS_DEV is NOT set', () => { + it('returns false if the environment variable POWERTOOLS_DEV is NOT set', () => { // Prepare process.env.POWERTOOLS_DEV = 'somethingsilly'; const service = new EnvironmentVariablesService(); @@ -200,7 +196,7 @@ describe('Class: EnvironmentVariablesService', () => { expect(value).toEqual(false); }); - test('it returns false if the environment variable POWERTOOLS_DEV is "somethingsilly"', () => { + it('returns false if the environment variable POWERTOOLS_DEV is "somethingsilly"', () => { // Prepare process.env.POWERTOOLS_DEV = 'somethingsilly'; const service = new EnvironmentVariablesService(); diff --git a/packages/commons/tests/unit/LRUCache.test.ts b/packages/commons/tests/unit/LRUCache.test.ts index cec778ac4c..089f38c218 100644 --- a/packages/commons/tests/unit/LRUCache.test.ts +++ b/packages/commons/tests/unit/LRUCache.test.ts @@ -1,13 +1,9 @@ -/** - * Test LRUCache class - * - * @group unit/commons/lru-cache - */ +import { describe, expect, it } from 'vitest'; import { LRUCache } from '../../src/LRUCache.js'; describe('Class: LRUMap', () => { describe('Method: add', () => { - test('when called it adds items to the cache', () => { + it('adds items to the cache', () => { // Prepare const cache = new LRUCache(); @@ -21,7 +17,7 @@ describe('Class: LRUMap', () => { expect(cache.get('b')).toBe(2); }); - test('when called it updates the value of an existing key', () => { + it('updates the value of an existing key', () => { // Prepare const cache = new LRUCache(); cache.add('a', 1); @@ -34,7 +30,7 @@ describe('Class: LRUMap', () => { expect(cache.get('a')).toBe(2); }); - test('when called it removes the oldest item when the cache is full', () => { + it('removes the oldest item when the cache is full', () => { // Prepare const cache = new LRUCache({ maxSize: 2 }); cache.add('a', 1); @@ -50,7 +46,7 @@ describe('Class: LRUMap', () => { expect(cache.get('c')).toBe(3); }); - test('when called and maxSize is 0, it skips cache', () => { + it('it skips the cache when max size is zero', () => { // Prepare const cache = new LRUCache({ maxSize: 0 }); @@ -63,7 +59,7 @@ describe('Class: LRUMap', () => { }); describe('Method: get', () => { - test('when called it returns the value of an existing key', () => { + it('returns the value of an existing key', () => { // Prepare const cache = new LRUCache(); cache.add('a', 1); @@ -75,7 +71,7 @@ describe('Class: LRUMap', () => { expect(value).toBe(1); }); - test('when called it returns undefined for a non-existing key', () => { + it('returns undefined for a non-existing key', () => { // Prepare const cache = new LRUCache(); @@ -86,7 +82,7 @@ describe('Class: LRUMap', () => { expect(value).toBeUndefined(); }); - test('when called it marks the item as the most recently used', () => { + it('marks the item as the most recently used', () => { // Prepare const cache = new LRUCache(); cache.add('a', 1); @@ -104,7 +100,7 @@ describe('Class: LRUMap', () => { }); describe('Method: has', () => { - test('when called it returns true for an existing key', () => { + it('returns true for an existing key', () => { // Prepare const cache = new LRUCache(); cache.add('a', 1); @@ -116,7 +112,7 @@ describe('Class: LRUMap', () => { expect(hasKey).toBe(true); }); - test('when called it returns false for a non-existing key', () => { + it('returns false for a non-existing key', () => { // Prepare const cache = new LRUCache(); @@ -129,7 +125,7 @@ describe('Class: LRUMap', () => { }); describe('Method: remove', () => { - test('when called it removes the item from the cache', () => { + it('removes the item from the cache', () => { // Prepare const cache = new LRUCache(); cache.add('a', 1); @@ -146,7 +142,7 @@ describe('Class: LRUMap', () => { expect(cache.get('a')).toBeUndefined(); }); - test('when called on an empty cache it does nothing', () => { + it('it does nothing when called on a non-existing key', () => { // Prepare const cache = new LRUCache(); cache.add('a', 1); diff --git a/packages/commons/tests/unit/LambdaInterface.test.ts b/packages/commons/tests/unit/LambdaInterface.test.ts deleted file mode 100644 index 4afc6e40f9..0000000000 --- a/packages/commons/tests/unit/LambdaInterface.test.ts +++ /dev/null @@ -1,155 +0,0 @@ -/** - * Test LambdaInterface interface - * - * @group unit/commons/lambdaInterface - */ -import context from '@aws-lambda-powertools/testing-utils/context'; -import type { Callback, Context, Handler } from 'aws-lambda'; -import type { - AsyncHandler, - LambdaInterface, - SyncHandler, -} from '../../src/types/index.js'; - -describe('LambdaInterface with arrow function', () => { - jest.spyOn(console, 'log').mockImplementation(); - test('it compiles when given a callback', async () => { - class LambdaFunction implements LambdaInterface { - public handler: SyncHandler = async ( - _event: unknown, - context: Context, - _callback: Callback - ) => { - context.done(); - context.fail(new Error('test Error')); - context.succeed('test succeed'); - context.getRemainingTimeInMillis(); - _callback(null, 'Hello World'); - }; - } - - new LambdaFunction().handler({}, context, () => - console.log('Lambda invoked!') - ); - }); - - test('it compiles when not given a callback', async () => { - class LambdaFunction implements LambdaInterface { - public handler: AsyncHandler = async ( - _event: unknown, - context: Context - ) => { - context.getRemainingTimeInMillis(); - }; - } - - await new LambdaFunction().handler({}, context); - }); -}); - -describe('LambdaInterface with standard function', () => { - test('it compiles when given a callback', async () => { - class LambdaFunction implements LambdaInterface { - public handler( - _event: unknown, - context: Context, - _callback: Callback - ): void { - context.getRemainingTimeInMillis(); - _callback(null, 'Hello World'); - } - } - - new LambdaFunction().handler({}, context, () => - console.log('Lambda invoked!') - ); - }); - - test('it compiles when not given a callback', async () => { - class LambdaFunction implements LambdaInterface { - public async handler(_event: unknown, context: Context): Promise { - context.getRemainingTimeInMillis(); - - return new Promise((resolve) => { - resolve('test promise'); - }); - } - } - - await new LambdaFunction().handler({}, context); - }); -}); - -describe('LambdaInterface with decorator', () => { - type HandlerMethodDecorator = ( - target: LambdaInterface, - propertyKey: string | symbol, - descriptor: - | TypedPropertyDescriptor> - | TypedPropertyDescriptor> - ) => void; - - class DummyModule { - public dummyDecorator(): HandlerMethodDecorator { - return (target, _propertyKey, descriptor) => { - const originalMethod = descriptor.value; - - descriptor.value = async (event, context, callback) => { - let result: unknown; - try { - console.log(`Invoking ${String(_propertyKey)}`); - result = await originalMethod?.apply(this, [ - event, - context, - callback, - ]); - console.log(`Invoked ${String(_propertyKey)}`); - } finally { - console.log('Finally from decorator'); - } - - return result; - }; - - return descriptor; - }; - } - } - - const dummyModule = new DummyModule(); - - test('decorator without callback compile', async () => { - // WHEN - class LambdaFunction implements LambdaInterface { - @dummyModule.dummyDecorator() - public async handler( - _event: unknown, - context: Context - ): Promise { - context.getRemainingTimeInMillis(); - - return 'test'; - } - } - - await new LambdaFunction().handler({}, context); - }); - - test('decorator with callback compile', async () => { - // WHEN - class LambdaFunction implements LambdaInterface { - @dummyModule.dummyDecorator() - public handler( - _event: unknown, - context: Context, - _callback: Callback - ): void { - context.getRemainingTimeInMillis(); - } - } - - new LambdaFunction().handler({}, context, () => - console.log('Lambda invoked!') - ); - }); -}); diff --git a/packages/commons/tests/unit/Utility.test.ts b/packages/commons/tests/unit/Utility.test.ts index f60e7edeea..e655273fc6 100644 --- a/packages/commons/tests/unit/Utility.test.ts +++ b/packages/commons/tests/unit/Utility.test.ts @@ -1,18 +1,14 @@ -/** - * Test Utility class - * - * @group unit/commons/utility - */ +import { beforeEach, describe, expect, it, vi } from 'vitest'; import { Utility } from '../../src/index.js'; describe('Class: Utility', () => { beforeEach(() => { - jest.clearAllMocks(); - jest.resetModules(); + vi.clearAllMocks(); + vi.resetModules(); }); describe('Method: getDefaultServiceName', () => { - test('it should return the default service name', () => { + it('returns the default service name', () => { class PowerTool extends Utility { public dummyMethod(): string { return this.getDefaultServiceName(); @@ -26,10 +22,10 @@ describe('Class: Utility', () => { }); describe('Method: getColdStart', () => { - test('when called multiple times on the parent class, it returns true the first time, then false afterwards', () => { + it('it returns true the first time, then false afterwards, when called multiple times', () => { // Prepare const utility = new Utility(); - const getColdStartSpy = jest.spyOn(utility, 'getColdStart'); + const getColdStartSpy = vi.spyOn(utility, 'getColdStart'); // Act utility.getColdStart(); @@ -49,7 +45,7 @@ describe('Class: Utility', () => { ]); }); - test('when called multiple times on a child class, it returns true the first time, then false afterwards', () => { + it('returns the correct values when subclassed', () => { // Prepare class PowerTool extends Utility { public dummyMethod(): boolean { @@ -57,8 +53,8 @@ describe('Class: Utility', () => { } } const powertool = new PowerTool(); - const dummyMethodSpy = jest.spyOn(powertool, 'dummyMethod'); - const getColdStartSpy = jest.spyOn(powertool, 'getColdStart'); + const dummyMethodSpy = vi.spyOn(powertool, 'dummyMethod'); + const getColdStartSpy = vi.spyOn(powertool, 'getColdStart'); // Act powertool.dummyMethod(); @@ -81,10 +77,10 @@ describe('Class: Utility', () => { }); describe('Method: isColdStart', () => { - test('when called multiple times on the parent class, it returns true the first time, then false afterwards', () => { + it('returns true the first time, then false afterwards when called multiple times', () => { // Prepare const utility = new Utility(); - const isColdStartSpy = jest.spyOn(utility, 'isColdStart'); + const isColdStartSpy = vi.spyOn(utility, 'isColdStart'); // Act utility.isColdStart(); @@ -104,7 +100,7 @@ describe('Class: Utility', () => { ]); }); - test('when called multiple times on a child class, it returns true the first time, then false afterwards', () => { + it('returns the correct values when subclassed', () => { // Prepare class PowerTool extends Utility { public dummyMethod(): boolean { @@ -112,8 +108,8 @@ describe('Class: Utility', () => { } } const powertool = new PowerTool(); - const dummyMethodSpy = jest.spyOn(powertool, 'dummyMethod'); - const isColdStartSpy = jest.spyOn(powertool, 'isColdStart'); + const dummyMethodSpy = vi.spyOn(powertool, 'dummyMethod'); + const isColdStartSpy = vi.spyOn(powertool, 'isColdStart'); // Act powertool.dummyMethod(); @@ -141,7 +137,7 @@ describe('Class: Utility', () => { return this.isValidServiceName(name); } } - test('it should allow valid strings', () => { + it('allows valid strings', () => { const powertool = new PowerTool(); const goodName = 'serverlessAirline'; @@ -150,7 +146,7 @@ describe('Class: Utility', () => { expect(result).toBe(true); }); - test('it should not allow empty strings', () => { + it("doesn't allow empty strings", () => { const tooShort = ''; const powertool = new PowerTool(); const result = powertool.dummyMethod(tooShort); diff --git a/packages/commons/tests/unit/awsSdkUtils.test.ts b/packages/commons/tests/unit/awsSdkUtils.test.ts index da06ea7729..907409b25d 100644 --- a/packages/commons/tests/unit/awsSdkUtils.test.ts +++ b/packages/commons/tests/unit/awsSdkUtils.test.ts @@ -1,8 +1,4 @@ -/** - * Test AWS SDK utilities - * - * @group unit/commons/awsSdkUtils - */ +import { beforeAll, describe, expect, it, vi } from 'vitest'; import { customUserAgentMiddleware } from '../../src/awsSdkUtils.js'; import { addUserAgentMiddleware, @@ -13,7 +9,7 @@ import { describe('Helpers: awsSdk', () => { describe('Function: userAgentMiddleware', () => { beforeAll(() => { - jest.spyOn(console, 'warn').mockImplementation(() => ({})); + vi.spyOn(console, 'warn').mockImplementation(() => ({})); }); it('handles gracefully failures in adding a middleware and only log a warning', () => { @@ -25,7 +21,7 @@ describe('Helpers: awsSdk', () => { }, }, }; - const warningSpy = jest + const warningSpy = vi .spyOn(console, 'warn') .mockImplementation(() => ({})); @@ -41,9 +37,9 @@ describe('Helpers: awsSdk', () => { identify: () => [ 'addPowertoolsToUserAgent: after getUserAgentMiddleware', ], - addRelativeTo: jest.fn(), + addRelativeTo: vi.fn(), }, - send: jest.fn(), + send: vi.fn(), config: { defaultSigningName: 'bar', }, @@ -62,9 +58,9 @@ describe('Helpers: awsSdk', () => { const client = { middlewareStack: { identify: () => '', - addRelativeTo: jest.fn(), + addRelativeTo: vi.fn(), }, - send: jest.fn(), + send: vi.fn(), config: { defaultSigningName: 'bar', }, @@ -104,7 +100,7 @@ describe('Helpers: awsSdk', () => { // Prepare const feature = 'my-feature'; const middleware = customUserAgentMiddleware(feature); - const next = jest.fn(); + const next = vi.fn(); const args = { request: { headers: { @@ -127,13 +123,13 @@ describe('Helpers: awsSdk', () => { it('returns true if the client is a valid AWS SDK v3 client', () => { // Prepare const client = { - send: jest.fn(), + send: vi.fn(), config: { defaultSigningName: 'bar', }, middlewareStack: { identify: () => '', - addRelativeTo: jest.fn(), + addRelativeTo: vi.fn(), }, }; diff --git a/packages/commons/tests/unit/cleanupMiddlewares.test.ts b/packages/commons/tests/unit/cleanupMiddlewares.test.ts index 8b178f2e9f..2046455de4 100644 --- a/packages/commons/tests/unit/cleanupMiddlewares.test.ts +++ b/packages/commons/tests/unit/cleanupMiddlewares.test.ts @@ -1,9 +1,5 @@ -/** - * Test Middy cleanupMiddlewares function - * - * @group unit/commons/cleanupMiddlewares - */ import context from '@aws-lambda-powertools/testing-utils/context'; +import { describe, expect, it, vi } from 'vitest'; import { IDEMPOTENCY_KEY, LOGGER_KEY, @@ -15,10 +11,10 @@ import { describe('Function: cleanupMiddlewares', () => { it('calls the cleanup function that are present', async () => { // Prepare - const mockCleanupFunction1 = jest.fn(); - const mockCleanupFunction2 = jest.fn(); - const mockCleanupFunction3 = jest.fn(); - const mockCleanupFunction4 = jest.fn(); + const mockCleanupFunction1 = vi.fn(); + const mockCleanupFunction2 = vi.fn(); + const mockCleanupFunction3 = vi.fn(); + const mockCleanupFunction4 = vi.fn(); const mockRequest = { event: {}, context: context, diff --git a/packages/commons/tests/unit/fromBase64.test.ts b/packages/commons/tests/unit/fromBase64.test.ts index 6d2a2a965b..8edc84a77b 100644 --- a/packages/commons/tests/unit/fromBase64.test.ts +++ b/packages/commons/tests/unit/fromBase64.test.ts @@ -1,14 +1,10 @@ -/** - * Test fromBase64 function - * - * @group unit/commons/fromBase64 - */ +import { beforeEach, describe, expect, it, vi } from 'vitest'; import { fromBase64 } from '../../src/fromBase64.js'; describe('Function: fromBase64', () => { beforeEach(() => { - jest.clearAllMocks(); - jest.resetModules(); + vi.clearAllMocks(); + vi.resetModules(); }); it('returns the Uint8Array from a base64 string', () => { diff --git a/packages/commons/tests/unit/typeUtils.test.ts b/packages/commons/tests/unit/typeUtils.test.ts index 6b96d61882..c2ccbcb61a 100644 --- a/packages/commons/tests/unit/typeUtils.test.ts +++ b/packages/commons/tests/unit/typeUtils.test.ts @@ -1,8 +1,4 @@ -/** - * Test type utils functions - * - * @group unit/commons/typeUtils - */ +import { beforeEach, describe, expect, it, vi } from 'vitest'; import { getType, isIntegerNumber, @@ -17,8 +13,8 @@ import { describe('Functions: typeUtils', () => { beforeEach(() => { - jest.clearAllMocks(); - jest.resetModules(); + vi.clearAllMocks(); + vi.resetModules(); }); describe('Function: isRecord', () => { diff --git a/packages/commons/vitest.config.ts b/packages/commons/vitest.config.ts new file mode 100644 index 0000000000..d5aa737c68 --- /dev/null +++ b/packages/commons/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineProject } from 'vitest/config'; + +export default defineProject({ + test: { + environment: 'node', + }, +}); diff --git a/vitest.config.ts b/vitest.config.ts index eca83dc65c..241b69b7be 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -13,8 +13,8 @@ export default defineConfig({ include: ['packages/*/src/**'], exclude: [ ...coverageConfigDefaults.exclude, - 'packages/commons/**', 'packages/batch/src/types.ts', + 'packages/commons/src/types/**', 'packages/event-handler/**', 'packages/idempotency/**', 'packages/jmespath/**',