Skip to content

Commit aa62967

Browse files
feat: migrate to ESM and vitest (#4461)
## Description This PR migrates the TypeScript testing framework from Jest to Vitest and transitions the codebase from CommonJS (CJS) to ECMAScript Modules (ESM). **Motivation: ** * **Moving from Jest to Vitest:** * **Performance:** Vitest leverages Vite's build pipeline, resulting in significantly faster test execution, especially in larger projects. This speed improvement enhances developer productivity by providing quicker feedback loops. * **Vite Integration:** For projects already using Vite, Vitest offers seamless integration, reducing configuration overhead and providing a unified development experience. * **Modern tooling:** Vitest is a modern test framework that is actively being developed and provides a better developer experience. * **Migrating from CJS to ESM:** * **Modern JavaScript:** ESM is the official standard for module systems in modern JavaScript, offering better static analysis, tree-shaking, and overall performance. * **Improved Compatibility:** ESM is the future of JavaScript modules and ensures better compatibility with modern tooling and libraries. * **Simplified Dependency Management:** ESM's static imports allow for more efficient dependency management and better optimization by bundlers. **Changes:** * Replaced Jest with Vitest for unit testing. * Updated test files to adhere to Vitest syntax. * Updated configuration files to reflect the changes (e.g., `package.json`, `tsconfig.json`). **Testing:** * The changes should not introduce any functional regressions. Thoroughly verify existing test suites using Vitest. * It is advisable to clear any local cache, such as node\_modules and dist folders, before building the lambda functions yourself. * Chages are tested with * Default example * Multi runner example **Related Issues:** * Fixes #3964 * Fixes #4366 **Potential Risks:** * While the migration aims for seamless transition, there's a possibility of encountering minor compatibility issues during the initial build or test runs. Thorough testing and cache clearing should mitigate these risks. **Additional Notes:** * This migration modernizes the project's tooling and module system, laying a foundation for future improvements and maintainability. --------- Co-authored-by: Nadav Sinai <[email protected]>
1 parent ce458fe commit aa62967

File tree

82 files changed

+2778
-3330
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2778
-3330
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
"dbaeumer.vscode-eslint",
1717
"editorconfig.editorconfig",
1818
"esbenp.prettier-vscode",
19-
"firsttris.vscode-jest-runner",
2019
"hashicorp.hcl",
2120
"hashicorp.terraform",
2221
"hashicorp.terraform",
23-
"orta.vscode-jest",
22+
"vitest.explorer",
2423
"yzhang.markdown-all-in-one"
2524
]
2625
}

.vscode/extensions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"editorconfig.editorconfig",
77
"yzhang.markdown-all-in-one",
88
"hashicorp.terraform",
9-
"firsttris.vscode-jest-runner"
9+
"vitest.explorer"
1010
]
1111
}

.vscode/gh-runners.code-workspace

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,5 @@
88
"name": "🚀 lambdas",
99
"path": "../lambdas"
1010
}
11-
],
12-
"settings": {
13-
"jest.autoRun": "on",
14-
"jest.disabledWorkspaceFolders": [
15-
"✨ root"
16-
]
17-
}
11+
]
1812
}

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{}
1+
{
2+
"jest.enable": false
3+
}

lambdas/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node_modules/
22
build/
3-
dist/
3+
**/dist/
44
*.log
55

66
# Ignore all yarn.lock files except the one in the root

lambdas/.vscode/settings.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

lambdas/aws-vitest-setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Setup AWS SDK client mock matchers
2+
import 'aws-sdk-client-mock-jest/vitest';

lambdas/functions/ami-housekeeper/jest.config.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

lambdas/functions/ami-housekeeper/package.json

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,33 @@
22
"name": "@aws-github-runner/ami-housekeeper",
33
"version": "1.0.0",
44
"main": "lambda.ts",
5+
"type": "module",
56
"license": "MIT",
67
"scripts": {
78
"start": "ts-node-dev src/local.ts",
89
"test": "NODE_ENV=test nx test",
910
"test:watch": "NODE_ENV=test nx test --watch",
10-
"lint": "yarn eslint src",
11+
"lint": "eslint src",
1112
"watch": "ts-node-dev --respawn --exit-child src/local.ts",
1213
"build": "ncc build src/lambda.ts -o dist",
13-
"dist": "yarn build && cd dist && zip ../ami-housekeeper.zip index.js",
14+
"dist": "yarn build && cp package.json dist/ && cd dist && zip ../ami-housekeeper.zip *",
1415
"format": "prettier --write \"**/*.ts\"",
1516
"format-check": "prettier --check \"**/*.ts\"",
1617
"all": "yarn build && yarn format && yarn lint && yarn test"
1718
},
1819
"devDependencies": {
19-
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
20-
"@types/aws-lambda": "^8.10.146",
21-
"@types/jest": "^29.5.14",
22-
"@types/node": "^22.13.9",
23-
"@typescript-eslint/eslint-plugin": "^8.25.0",
24-
"@typescript-eslint/parser": "^8.25.0",
20+
"@aws-sdk/types": "^3.734.0",
21+
"@types/aws-lambda": "^8.10.147",
2522
"@vercel/ncc": "^0.38.3",
2623
"aws-sdk-client-mock": "^4.1.0",
27-
"aws-sdk-client-mock-jest": "^4.1.0",
28-
"eslint": "^8.57.0",
29-
"eslint-plugin-prettier": "5.2.3",
30-
"jest": "^29.7.0",
31-
"jest-mock": "^29.7.0",
32-
"jest-mock-extended": "^3.0.7",
33-
"nock": "^14.0.1",
34-
"prettier": "3.4.2",
35-
"ts-jest": "^29.2.5",
36-
"ts-node": "^10.9.2",
37-
"ts-node-dev": "^2.0.0"
24+
"aws-sdk-client-mock-jest": "^4.1.0"
3825
},
3926
"dependencies": {
4027
"@aws-github-runner/aws-powertools-util": "*",
4128
"@aws-github-runner/aws-ssm-util": "*",
42-
"@aws-sdk/client-ec2": "^3.764.0",
29+
"@aws-sdk/client-ec2": "^3.767.0",
4330
"@aws-sdk/client-ssm": "^3.759.0",
44-
"@aws-sdk/types": "^3.734.0",
45-
"cron-parser": "^4.9.0",
46-
"typescript": "^5.7.3"
31+
"cron-parser": "^4.9.0"
4732
},
4833
"nx": {
4934
"includedScripts": [

lambdas/functions/ami-housekeeper/src/ami.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import {
1414
SSMClient,
1515
} from '@aws-sdk/client-ssm';
1616
import { mockClient } from 'aws-sdk-client-mock';
17-
import 'aws-sdk-client-mock-jest';
17+
import 'aws-sdk-client-mock-jest/vitest';
1818

1919
import { AmiCleanupOptions, amiCleanup, defaultAmiCleanupOptions } from './ami';
20+
import { describe, it, expect, beforeEach, vi } from 'vitest';
2021

2122
process.env.AWS_REGION = 'eu-east-1';
2223
const deleteAmisOlderThenDays = 30;
@@ -76,7 +77,7 @@ const ssmParameters: DescribeParametersCommandOutput = {
7677

7778
describe("delete AMI's", () => {
7879
beforeEach(() => {
79-
jest.resetAllMocks();
80+
vi.resetAllMocks();
8081
mockEC2Client.reset();
8182
mockSSMClient.reset();
8283

0 commit comments

Comments
 (0)