Skip to content

test(maintenance): migrate testing utils to vitest #2940

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 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 0 additions & 31 deletions packages/testing/jest.config.cjs

This file was deleted.

7 changes: 3 additions & 4 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
},
"private": true,
"scripts": {
"test": "npm run test:unit",
"test:unit": "jest --group=unit --detectOpenHandles --verbose",
"jest": "jest --detectOpenHandles --verbose",
"test": "vitest --run",
"test:unit": "vitest --run",
"test:e2e": "echo 'Not implemented'",
"watch": "jest --watch",
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
Expand Down Expand Up @@ -97,4 +96,4 @@
"aws-cdk-lib": "^2.152.0",
"esbuild": "^0.23.0"
}
}
}
16 changes: 0 additions & 16 deletions packages/testing/tests/helpers/populateEnvironmentVariables.ts

This file was deleted.

29 changes: 12 additions & 17 deletions packages/testing/tests/unit/TestInvocationLogs.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/**
* Test InvocationLogs class
*
* @group unit/commons/invocationLogs
*
*/
import { beforeEach, describe, expect, it } from 'vitest';
import { TestInvocationLogs } from '../../src/TestInvocationLogs.js';

const exampleLogs = `START RequestId: c6af9ac6-7b61-11e6-9a41-93e812345678 Version: $LATEST
Expand All @@ -15,7 +10,7 @@ END RequestId: c6af9ac6-7b61-11e6-9a41-93e812345678
REPORT RequestId: c6af9ac6-7b61-11e6-9a41-93e812345678\tDuration: 2.16 ms\tBilled Duration: 3 ms\tMemory Size: 128 MB\tMax Memory Used: 57 MB\t`;

describe('Constructor', () => {
test('it should parse base64 text correctly', () => {
it('parses base64 text correctly', () => {
const invocationLogs = new TestInvocationLogs(
Buffer.from(exampleLogs).toString('base64')
);
Expand All @@ -33,29 +28,29 @@ describe('doesAnyFunctionLogsContains()', () => {
Buffer.from(exampleLogs).toString('base64')
);
});
test('it should return true if the text appear in any logs', () => {
it('returns true if the text appear in any of the logs', () => {
const phraseInMessage = 'This is';
expect(invocationLogs.doesAnyFunctionLogsContains(phraseInMessage)).toBe(
true
);
});
test('it should return false if the text does not appear in any logs', () => {
it('returns false if the text does not appear anywhere', () => {
const phraseNotInMessage = 'A quick brown fox jumps over the lazy dog';
expect(invocationLogs.doesAnyFunctionLogsContains(phraseNotInMessage)).toBe(
false
);
});

test('it should return true for key in the log', () => {
it('returns true if the provided key appears in any of the logs', () => {
const keyInLog = 'error';
expect(invocationLogs.doesAnyFunctionLogsContains(keyInLog)).toBe(true);
});

test('it should return true for a text in an error key', () => {
it('returns true it the provided text appears in an error key within the logs', () => {
const textInError = '/var/task/index.js:2778';
expect(invocationLogs.doesAnyFunctionLogsContains(textInError)).toBe(true);
});
test('it should return false for the text that appears only on the ', () => {
it('excludes the report logs from the search', () => {
const textInStartLine = 'Version: $LATEST';
const textInEndLine = 'END RequestId';
const textInReportLine = 'Billed Duration';
Expand All @@ -70,7 +65,7 @@ describe('doesAnyFunctionLogsContains()', () => {
);
});

test('it should apply filter log based on the given level', () => {
it('filters log based on the given level', () => {
const debugLogHasWordINFO = invocationLogs.doesAnyFunctionLogsContains(
'INFO',
'DEBUG'
Expand Down Expand Up @@ -100,7 +95,7 @@ describe('getFunctionLogs()', () => {
);
});

test('it should retrive logs of the given level only', () => {
it('retrives logs of the given level only', () => {
const infoLogs = invocationLogs.getFunctionLogs('INFO');
expect(infoLogs.length).toBe(2);
expect(infoLogs[0].includes('INFO')).toBe(true);
Expand All @@ -114,7 +109,7 @@ describe('getFunctionLogs()', () => {
expect(errorLogs[0].includes('ERROR')).toBe(true);
});

test('it should NOT return logs generated by Lambda service (e.g. START, END, and REPORT)', () => {
it("doesn't return logs generated by Lambda service (e.g. START, END, and REPORT)", () => {
const errorLogs = invocationLogs.getFunctionLogs('ERROR');
expect(errorLogs.length).toBe(1);
expect(errorLogs[0].includes('START')).toBe(false);
Expand All @@ -124,7 +119,7 @@ describe('getFunctionLogs()', () => {
});

describe('parseFunctionLog()', () => {
test('it should return object with the correct values based on the given log', () => {
it('returns an object with the correct values based on the given log', () => {
const rawLogStr =
'{"cold_start":true,"function_arn":"arn:aws:lambda:eu-west-1:561912387782:function:loggerMiddyStandardFeatures-c555a2ec-1121-4586-9c04-185ab36ea34c","function_memory_size":128,"function_name":"loggerMiddyStandardFeatures-c555a2ec-1121-4586-9c04-185ab36ea34c","function_request_id":"7f586697-238a-4c3b-9250-a5f057c1119c","level":"DEBUG","message":"This is a DEBUG log but contains the word INFO some context and persistent key","service":"logger-e2e-testing","timestamp":"2022-01-27T16:04:39.323Z","persistentKey":"works"}';

Expand All @@ -146,7 +141,7 @@ describe('parseFunctionLog()', () => {
});
});

test('it should throw an error if receive incorrect formatted raw log string', () => {
it('throws an error if receive incorrect formatted raw log string', () => {
const notJSONstring = 'not-json-string';
expect(() => {
TestInvocationLogs.parseFunctionLog(notJSONstring);
Expand Down
10 changes: 10 additions & 0 deletions packages/testing/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineProject } from 'vitest/config';

export default defineProject({
resolve: {
conditions: ['source'],
},
test: {
environment: 'node',
},
});