Skip to content

test(batch): migrate unit tests to Vitest #2938

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 11 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
25 changes: 25 additions & 0 deletions .github/workflows/reusable-run-linting-check-and-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@ permissions:
contents: read

jobs:
code-quality:
runs-on: ubuntu-latest
env:
NODE_ENV: dev
strategy:
matrix:
version: [18, 20]
workspace: ["packages/batch"]
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Setup NodeJS
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version: ${{ matrix.version }}
cache: "npm"
- name: Setup dependencies
uses: aws-powertools/actions/.github/actions/cached-node-modules@d406bac5563f1d8c793519a3eedfe620f6a14872
with:
nodeVersion: ${{ matrix.version }}
- name: Linting
run: npm run lint -w ${{ matrix.workspace }}
- name: Unit tests
run: npm run test:unit:coverage -w ${{ matrix.workspace }}
run-linting-check-and-unit-tests-on-utilities:
runs-on: ubuntu-latest
env:
Expand Down
4 changes: 3 additions & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ npm t \
-w packages/idempotency \
-w packages/parameters \
-w packages/parser \
-w packages/event-handler
-w packages/event-handler

npx vitest --run --coverage --changed="$(git merge-base HEAD main)"
31 changes: 0 additions & 31 deletions packages/batch/jest.config.cjs

This file was deleted.

8 changes: 4 additions & 4 deletions packages/batch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"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 --coverage.enabled --coverage.thresholds.100 --coverage.include='src/**'",
"test:e2e:nodejs18x": "echo 'Not Implemented'",
"test:e2e:nodejs20x": "echo 'Not Implemented'",
"test:e2e": "echo 'Not Implemented'",
Expand Down Expand Up @@ -75,4 +75,4 @@
"devDependencies": {
"@aws-lambda-powertools/testing-utils": "file:../testing"
}
}
}
3 changes: 1 addition & 2 deletions packages/batch/src/BasePartialProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ abstract class BasePartialProcessor {
* If this is a sync processor, user should have called processSync instead,
* so we call the method early to throw the error early thus failing fast.
*/
if (this.constructor.name === 'BatchProcessorSync') {
if (this.constructor.name === 'BatchProcessorSync')
await this.processRecord(this.records[0]);
}
this.prepare();

const processingPromises: Promise<SuccessResponse | FailureResponse>[] =
Expand Down
12 changes: 0 additions & 12 deletions packages/batch/tests/helpers/populateEnvironmentVariables.ts

This file was deleted.

15 changes: 5 additions & 10 deletions packages/batch/tests/unit/BasePartialProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/**
* Test BasePartialBatchProcessor class
*
* @group unit/batch/class/basepartialbatchprocessor
*/
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { BasePartialBatchProcessor, EventType } from '../../src/index.js';
import type {
BaseRecord,
Expand All @@ -16,8 +12,7 @@ describe('Class: BasePartialBatchProcessor', () => {
const ENVIRONMENT_VARIABLES = process.env;

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

Expand Down Expand Up @@ -46,19 +41,19 @@ describe('Class: BasePartialBatchProcessor', () => {
}

describe('create custom batch partial processor', () => {
it('should create a custom batch partial processor', () => {
it('creates a custom batch partial processor', () => {
// Act
const processor = new MyPartialProcessor();

// Assess
expect(processor).toBeInstanceOf(BasePartialBatchProcessor);
});

it('should process a batch of records', () => {
it('processes a batch of records', () => {
// Prepare
const processor = new MyPartialProcessor();
const records = [sqsRecordFactory('success')];
const consoleSpy = jest.spyOn(console, 'log');
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

// Act
processor.register(records, sqsRecordHandler);
Expand Down
51 changes: 14 additions & 37 deletions packages/batch/tests/unit/BatchProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* Test BatchProcessor class
*
* @group unit/batch/class/batchprocessor
*/
import context from '@aws-lambda-powertools/testing-utils/context';
import type { Context } from 'aws-lambda';
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
import {
BatchProcessingError,
BatchProcessor,
Expand All @@ -31,8 +27,7 @@ describe('Class: AsyncBatchProcessor', () => {
};

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

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

describe('Asynchronously processing SQS Records', () => {
test('Batch processing SQS records with no failures', async () => {
it('completes processing with no failures', async () => {
// Prepare
const firstRecord = sqsRecordFactory('success');
const secondRecord = sqsRecordFactory('success');
Expand All @@ -59,7 +54,7 @@ describe('Class: AsyncBatchProcessor', () => {
]);
});

test('Batch processing SQS records with some failures', async () => {
it('completes processing with with some failures', async () => {
// Prepare
const firstRecord = sqsRecordFactory('failure');
const secondRecord = sqsRecordFactory('success');
Expand All @@ -86,7 +81,7 @@ describe('Class: AsyncBatchProcessor', () => {
});
});

test('Batch processing SQS records with all failures', async () => {
it('completes processing with all failures', async () => {
// Prepare
const firstRecord = sqsRecordFactory('failure');
const secondRecord = sqsRecordFactory('failure');
Expand All @@ -106,7 +101,7 @@ describe('Class: AsyncBatchProcessor', () => {
});

describe('Asynchronously processing Kinesis Records', () => {
test('Batch processing Kinesis records with no failures', async () => {
it('completes processing with no failures', async () => {
// Prepare
const firstRecord = kinesisRecordFactory('success');
const secondRecord = kinesisRecordFactory('success');
Expand All @@ -124,7 +119,7 @@ describe('Class: AsyncBatchProcessor', () => {
]);
});

test('Batch processing Kinesis records with some failures', async () => {
it('completes processing with some failures', async () => {
// Prepare
const firstRecord = kinesisRecordFactory('failure');
const secondRecord = kinesisRecordFactory('success');
Expand All @@ -151,7 +146,7 @@ describe('Class: AsyncBatchProcessor', () => {
});
});

test('Batch processing Kinesis records with all failures', async () => {
it('completes processing with all failures', async () => {
// Prepare
const firstRecord = kinesisRecordFactory('failure');
const secondRecord = kinesisRecordFactory('failure');
Expand All @@ -171,7 +166,7 @@ describe('Class: AsyncBatchProcessor', () => {
});

describe('Asynchronously processing DynamoDB Records', () => {
test('Batch processing DynamoDB records with no failures', async () => {
it('completes processing with no failures', async () => {
// Prepare
const firstRecord = dynamodbRecordFactory('success');
const secondRecord = dynamodbRecordFactory('success');
Expand All @@ -189,7 +184,7 @@ describe('Class: AsyncBatchProcessor', () => {
]);
});

test('Batch processing DynamoDB records with some failures', async () => {
it('completes processing with some failures', async () => {
// Prepare
const firstRecord = dynamodbRecordFactory('failure');
const secondRecord = dynamodbRecordFactory('success');
Expand All @@ -216,7 +211,7 @@ describe('Class: AsyncBatchProcessor', () => {
});
});

test('Batch processing DynamoDB records with all failures', async () => {
it('completes processing with all failures', async () => {
// Prepare
const firstRecord = dynamodbRecordFactory('failure');
const secondRecord = dynamodbRecordFactory('failure');
Expand All @@ -236,7 +231,7 @@ describe('Class: AsyncBatchProcessor', () => {
});

describe('Batch processing with Lambda context', () => {
test('Batch processing when context is provided and handler accepts', async () => {
it('passes the context to the record handler', async () => {
// Prepare
const firstRecord = sqsRecordFactory('success');
const secondRecord = sqsRecordFactory('success');
Expand All @@ -254,25 +249,7 @@ describe('Class: AsyncBatchProcessor', () => {
]);
});

test('Batch processing when context is provided and handler does not accept', async () => {
// Prepare
const firstRecord = sqsRecordFactory('success');
const secondRecord = sqsRecordFactory('success');
const records = [firstRecord, secondRecord];
const processor = new BatchProcessor(EventType.SQS);

// Act
processor.register(records, asyncSqsRecordHandler, options);
const processedMessages = await processor.process();

// Assess
expect(processedMessages).toStrictEqual([
['success', firstRecord.body, firstRecord],
['success', secondRecord.body, secondRecord],
]);
});

test('Batch processing when malformed context is provided and handler attempts to use', async () => {
it('throws an error when passing an invalid context object', async () => {
// Prepare
const firstRecord = sqsRecordFactory('success');
const secondRecord = sqsRecordFactory('success');
Expand All @@ -289,7 +266,7 @@ describe('Class: AsyncBatchProcessor', () => {
});
});

test('When calling the sync process method, it should throw an error', () => {
it('throws an error when the sync process method is called', () => {
// Prepare
const processor = new BatchProcessor(EventType.SQS);

Expand Down
Loading