Skip to content

test(idempotency): migrate to vitest #3124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ jobs:
workspace: [
"packages/batch",
"packages/commons",
"packages/jmespath"
"packages/jmespath",
"packages/idempotency"
]
fail-fast: false
steps:
Expand Down Expand Up @@ -90,7 +91,6 @@ jobs:
-w packages/tracer \
-w packages/metrics \
-w packages/parameters \
-w packages/idempotency \
-w packages/parser \
-w packages/event-handler
- name: Run unit tests
Expand All @@ -99,7 +99,6 @@ jobs:
-w packages/tracer \
-w packages/metrics \
-w packages/parameters \
-w packages/idempotency \
-w packages/parser \
-w packages/event-handler
check-examples:
Expand Down
1 change: 0 additions & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ npm t \
-w packages/logger \
-w packages/metrics \
-w packages/tracer \
-w packages/idempotency \
-w packages/parameters \
-w packages/parser \
-w packages/event-handler
Expand Down
22 changes: 16 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions packages/idempotency/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
"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": "echo 'Not Implemented'",
"test:e2e:nodejs18x": "RUNTIME=nodejs18x jest --group=e2e",
"test:e2e:nodejs20x": "RUNTIME=nodejs20x jest --group=e2e",
"test:e2e": "jest --group=e2e",
"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",
Expand Down Expand Up @@ -127,7 +127,6 @@
"@aws-lambda-powertools/testing-utils": "file:../testing",
"@aws-sdk/client-dynamodb": "^3.658.1",
"@aws-sdk/lib-dynamodb": "^3.658.1",
"aws-sdk-client-mock": "^4.0.2",
"aws-sdk-client-mock-jest": "^4.0.2"
"aws-sdk-client-mock": "^4.0.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const makeHandlerIdempotent = (
});

const idempotencyHandler = new IdempotencyHandler({
functionToMakeIdempotent: /* istanbul ignore next */ () => ({}),
functionToMakeIdempotent: /* v8 ignore next */ () => ({}),
functionArguments: [],
idempotencyConfig,
persistenceStore,
Expand Down
36 changes: 31 additions & 5 deletions packages/idempotency/tests/helpers/idempotencyUtils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
import { vi } from 'vitest';
import { BasePersistenceLayer } from '../../src/persistence/BasePersistenceLayer.js';
import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer.js';
import type { IdempotencyRecord } from '../../src/persistence/IdempotencyRecord.js';

/**
* Dummy class to test the abstract class BasePersistenceLayer.
*
* This class is used in the unit tests.
*/
class PersistenceLayerTestClass extends BasePersistenceLayer {
protected _deleteRecord = jest.fn();
protected _getRecord = jest.fn();
protected _putRecord = jest.fn();
protected _updateRecord = jest.fn();
public _deleteRecord = vi.fn();
public _getRecord = vi.fn();
public _putRecord = vi.fn();
public _updateRecord = vi.fn();
}

export { PersistenceLayerTestClass };
/**
* Dummy class to test the abstract class DynamoDBPersistenceLayer.
*
* This class is used in the unit tests.
*/
class DynamoDBPersistenceLayerTestClass extends DynamoDBPersistenceLayer {
public _deleteRecord(record: IdempotencyRecord): Promise<void> {
return super._deleteRecord(record);
}

public _getRecord(idempotencyKey: string): Promise<IdempotencyRecord> {
return super._getRecord(idempotencyKey);
}

public _putRecord(_record: IdempotencyRecord): Promise<void> {
return super._putRecord(_record);
}

public _updateRecord(record: IdempotencyRecord): Promise<void> {
return super._updateRecord(record);
}
}

export { PersistenceLayerTestClass, DynamoDBPersistenceLayerTestClass };
3 changes: 2 additions & 1 deletion packages/idempotency/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../",
"rootDir": "../../",
"noEmit": true
},
"include": [
"../../testing/src/setupEnv.ts",
"../src/**/*",
"./**/*",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
/**
* Test EnvironmentVariableService class
*
* @group unit/idempotency/environment-variables-service
*/
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js';

describe('Class: EnvironmentVariableService', () => {
const ENVIRONMENT_VARIABLES = process.env;

beforeEach(() => {
jest.resetModules();
process.env = { ...ENVIRONMENT_VARIABLES };
});

Expand All @@ -18,9 +13,10 @@ describe('Class: EnvironmentVariableService', () => {
});

describe('Method: getFunctionName', () => {
test('When called, it gets the Lambda function name from the environment variable', () => {
it('gets the Lambda function name from the environment variable', () => {
// Prepare
const expectedName = process.env.AWS_LAMBDA_FUNCTION_NAME;
const expectedName = 'test-function';
process.env.AWS_LAMBDA_FUNCTION_NAME = expectedName;

// Act
const lambdaName = new EnvironmentVariablesService().getFunctionName();
Expand All @@ -29,7 +25,7 @@ describe('Class: EnvironmentVariableService', () => {
expect(lambdaName).toEqual(expectedName);
});

test('When called without the environment variable set, it returns an empty string', () => {
it('it returns an empty string when the Lambda function name is not set', () => {
// Prepare
process.env.AWS_LAMBDA_FUNCTION_NAME = undefined;

Expand Down
14 changes: 4 additions & 10 deletions packages/idempotency/tests/unit/IdempotencyConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
/**
* Test IdempotencyConfig class
*
* @group unit/idempotency/config
*/
import context from '@aws-lambda-powertools/testing-utils/context';
import { afterAll, beforeEach, describe, expect, it } from 'vitest';
import { IdempotencyConfig } from '../../src/index.js';
import type { IdempotencyConfigOptions } from '../../src/types/index.js';

describe('Class: IdempotencyConfig', () => {
const ENVIRONMENT_VARIABLES = process.env;

beforeEach(() => {
jest.clearAllMocks();
jest.resetModules();
process.env = { ...ENVIRONMENT_VARIABLES };
});

Expand All @@ -21,7 +15,7 @@ describe('Class: IdempotencyConfig', () => {
});

describe('Method: configure', () => {
test('when configured with an empty config object, it initializes the config with default values', () => {
it('initializes the config with default values', () => {
// Prepare
const configOptions = {};

Expand All @@ -42,7 +36,7 @@ describe('Class: IdempotencyConfig', () => {
);
});

test('when configured with a config object, it initializes the config with the provided configs', () => {
it('initializes the config with the provided configs', () => {
// Prepare
const configOptions: IdempotencyConfigOptions = {
eventKeyJmesPath: 'eventKeyJmesPath',
Expand Down Expand Up @@ -73,7 +67,7 @@ describe('Class: IdempotencyConfig', () => {
});

describe('Method: registerLambdaContext', () => {
test('when called, it stores the provided context', async () => {
it('stores the provided context', async () => {
// Prepare
const config = new IdempotencyConfig({});

Expand Down
Loading