Skip to content

Commit d063ec6

Browse files
authored
test(maintenance): migrate testing utils to vitest (aws-powertools#2940)
1 parent 40a6b21 commit d063ec6

File tree

5 files changed

+25
-68
lines changed

5 files changed

+25
-68
lines changed

packages/testing/jest.config.cjs

-31
This file was deleted.

packages/testing/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
},
99
"private": true,
1010
"scripts": {
11-
"test": "npm run test:unit",
12-
"test:unit": "jest --group=unit --detectOpenHandles --verbose",
13-
"jest": "jest --detectOpenHandles --verbose",
11+
"test": "vitest --run",
12+
"test:unit": "vitest --run",
1413
"test:e2e": "echo 'Not implemented'",
1514
"watch": "jest --watch",
1615
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
@@ -97,4 +96,4 @@
9796
"aws-cdk-lib": "^2.152.0",
9897
"esbuild": "^0.23.0"
9998
}
100-
}
99+
}

packages/testing/tests/helpers/populateEnvironmentVariables.ts

-16
This file was deleted.

packages/testing/tests/unit/TestInvocationLogs.test.ts

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
/**
2-
* Test InvocationLogs class
3-
*
4-
* @group unit/commons/invocationLogs
5-
*
6-
*/
1+
import { beforeEach, describe, expect, it } from 'vitest';
72
import { TestInvocationLogs } from '../../src/TestInvocationLogs.js';
83

94
const exampleLogs = `START RequestId: c6af9ac6-7b61-11e6-9a41-93e812345678 Version: $LATEST
@@ -15,7 +10,7 @@ END RequestId: c6af9ac6-7b61-11e6-9a41-93e812345678
1510
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`;
1611

1712
describe('Constructor', () => {
18-
test('it should parse base64 text correctly', () => {
13+
it('parses base64 text correctly', () => {
1914
const invocationLogs = new TestInvocationLogs(
2015
Buffer.from(exampleLogs).toString('base64')
2116
);
@@ -33,29 +28,29 @@ describe('doesAnyFunctionLogsContains()', () => {
3328
Buffer.from(exampleLogs).toString('base64')
3429
);
3530
});
36-
test('it should return true if the text appear in any logs', () => {
31+
it('returns true if the text appear in any of the logs', () => {
3732
const phraseInMessage = 'This is';
3833
expect(invocationLogs.doesAnyFunctionLogsContains(phraseInMessage)).toBe(
3934
true
4035
);
4136
});
42-
test('it should return false if the text does not appear in any logs', () => {
37+
it('returns false if the text does not appear anywhere', () => {
4338
const phraseNotInMessage = 'A quick brown fox jumps over the lazy dog';
4439
expect(invocationLogs.doesAnyFunctionLogsContains(phraseNotInMessage)).toBe(
4540
false
4641
);
4742
});
4843

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

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

73-
test('it should apply filter log based on the given level', () => {
68+
it('filters log based on the given level', () => {
7469
const debugLogHasWordINFO = invocationLogs.doesAnyFunctionLogsContains(
7570
'INFO',
7671
'DEBUG'
@@ -100,7 +95,7 @@ describe('getFunctionLogs()', () => {
10095
);
10196
});
10297

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

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

126121
describe('parseFunctionLog()', () => {
127-
test('it should return object with the correct values based on the given log', () => {
122+
it('returns an object with the correct values based on the given log', () => {
128123
const rawLogStr =
129124
'{"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"}';
130125

@@ -146,7 +141,7 @@ describe('parseFunctionLog()', () => {
146141
});
147142
});
148143

149-
test('it should throw an error if receive incorrect formatted raw log string', () => {
144+
it('throws an error if receive incorrect formatted raw log string', () => {
150145
const notJSONstring = 'not-json-string';
151146
expect(() => {
152147
TestInvocationLogs.parseFunctionLog(notJSONstring);

packages/testing/vitest.config.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineProject } from 'vitest/config';
2+
3+
export default defineProject({
4+
resolve: {
5+
conditions: ['source'],
6+
},
7+
test: {
8+
environment: 'node',
9+
},
10+
});

0 commit comments

Comments
 (0)