Skip to content

test(commons): migrate to vitest #3060

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 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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 \
Expand Down
1 change: 0 additions & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
npm t \
-w packages/commons \
-w packages/jmespath \
-w packages/logger \
-w packages/metrics \
Expand Down
4 changes: 2 additions & 2 deletions packages/batch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 0 additions & 29 deletions packages/commons/jest.config.cjs

This file was deleted.

8 changes: 4 additions & 4 deletions packages/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 41 additions & 0 deletions packages/commons/tests/types/LambdaInterface.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
38 changes: 17 additions & 21 deletions packages/commons/tests/unit/EnvironmentVariablesService.test.ts
Original file line number Diff line number Diff line change
@@ -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 };
});

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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';
Expand All @@ -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();
Expand All @@ -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';
Expand All @@ -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';
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
28 changes: 12 additions & 16 deletions packages/commons/tests/unit/LRUCache.test.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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 });

Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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 an empty cache', () => {
// Prepare
const cache = new LRUCache();
cache.add('a', 1);
Expand Down
Loading