mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 23:33:31 +00:00
build: ensure new libraries are added to the root jest.config (#16166)
* Add missing libs to jest.config.js Added 15 missing libraries to the jest projects array: - libs/assets - libs/client-type - libs/core-test-utils - libs/dirt/card - libs/guid - libs/logging - libs/messaging-internal - libs/messaging - libs/serialization - libs/state-test-utils - libs/state - libs/storage-core - libs/storage-test-utils - libs/tools/export/vault-export/vault-export-ui - libs/user-core This ensures all existing libraries with jest.config.js files are included in CI test runs. * Update basic-lib generator to add new libs to jest.config.js - Added updateJestConfig function that automatically adds new libraries to jest.config.js - Function finds the appropriate alphabetical position for the new library - Added comprehensive tests for the new functionality - Ensures new libraries are included in CI test runs from creation This prevents the issue where new libraries are created but their tests are not run in CI because they are missing from the jest configuration. * Fix import statements in state-definitions and deserialization-helpers tests - Fixed ClientLocations import in state-definitions.spec.ts to use @bitwarden/storage-core instead of relative import - Simplified deserialization-helpers.spec.ts import to use library root @bitwarden/serialization
This commit is contained in:
@@ -83,3 +83,44 @@ describe("basic-lib generator", () => {
|
||||
expect(tree.exists(`libs/test/src/test.spec.ts`)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
it("should update jest.config.js with new library", async () => {
|
||||
// Create a mock jest.config.js with existing libs
|
||||
const existingJestConfig = `module.exports = {
|
||||
projects: [
|
||||
"<rootDir>/apps/browser/jest.config.js",
|
||||
"<rootDir>/libs/admin-console/jest.config.js",
|
||||
"<rootDir>/libs/auth/jest.config.js",
|
||||
"<rootDir>/libs/vault/jest.config.js",
|
||||
],
|
||||
};`;
|
||||
tree.write("jest.config.js", existingJestConfig);
|
||||
|
||||
await basicLibGenerator(tree, options);
|
||||
|
||||
const jestConfigContent = tree.read("jest.config.js");
|
||||
expect(jestConfigContent).not.toBeNull();
|
||||
const jestConfig = jestConfigContent?.toString();
|
||||
|
||||
// Should contain the new library in alphabetical order
|
||||
expect(jestConfig).toContain('"<rootDir>/libs/test/jest.config.js",');
|
||||
|
||||
// Should be in the right alphabetical position (after auth, before vault)
|
||||
const authIndex = jestConfig?.indexOf('"<rootDir>/libs/auth/jest.config.js"');
|
||||
const testIndex = jestConfig?.indexOf('"<rootDir>/libs/test/jest.config.js"');
|
||||
const vaultIndex = jestConfig?.indexOf('"<rootDir>/libs/vault/jest.config.js"');
|
||||
|
||||
expect(authIndex).toBeDefined();
|
||||
expect(testIndex).toBeDefined();
|
||||
expect(vaultIndex).toBeDefined();
|
||||
expect(authIndex! < testIndex!).toBeTruthy();
|
||||
expect(testIndex! < vaultIndex!).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should handle missing jest.config.js file gracefully", async () => {
|
||||
const consoleSpy = jest.spyOn(console, "warn").mockImplementation();
|
||||
// Don't create jest.config.js file
|
||||
await basicLibGenerator(tree, options);
|
||||
expect(consoleSpy).toHaveBeenCalledWith("jest.config.js file not found at root");
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
|
||||
@@ -53,6 +53,9 @@ export async function basicLibGenerator(
|
||||
// Update CODEOWNERS with the new lib
|
||||
updateCodeowners(tree, options.directory, options.name, options.team);
|
||||
|
||||
// Update jest.config.js with the new lib
|
||||
updateJestConfig(tree, options.directory, options.name);
|
||||
|
||||
// Format all new files with prettier
|
||||
await formatFiles(tree);
|
||||
|
||||
@@ -124,4 +127,66 @@ function updateCodeowners(tree: Tree, directory: string, name: string, team: str
|
||||
tree.write(codeownersPath, content + newLine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the jest.config.js file to include the new library
|
||||
* This ensures the library's tests are included in CI runs
|
||||
*
|
||||
* @param {Tree} tree - The virtual file system tree
|
||||
* @param {string} directory - Directory where the library is created
|
||||
* @param {string} name - The library name
|
||||
*/
|
||||
function updateJestConfig(tree: Tree, directory: string, name: string) {
|
||||
const jestConfigPath = "jest.config.js";
|
||||
|
||||
if (!tree.exists(jestConfigPath)) {
|
||||
console.warn("jest.config.js file not found at root");
|
||||
return;
|
||||
}
|
||||
|
||||
const content = tree.read(jestConfigPath)?.toString() || "";
|
||||
const libJestPath = `"<rootDir>/${directory}/${name}/jest.config.js",`;
|
||||
|
||||
// Find the libs section and insert the new library in alphabetical order
|
||||
const lines = content.split("\n");
|
||||
let insertIndex = -1;
|
||||
let foundLibsSection = false;
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
|
||||
// Check if we're in the libs section
|
||||
if (line.includes('"<rootDir>/libs/')) {
|
||||
foundLibsSection = true;
|
||||
|
||||
// Extract the lib name for comparison
|
||||
const match = line.match(/"<rootDir>libs([^"]+)/);
|
||||
if (match) {
|
||||
const existingLibName = match[1];
|
||||
|
||||
// If the new lib should come before this existing lib alphabetically
|
||||
if (name < existingLibName) {
|
||||
insertIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we were in libs section but hit a non-libs line, insert at end of libs
|
||||
else if (foundLibsSection && !line.includes('"<rootDir>/libs/')) {
|
||||
insertIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (insertIndex === -1) {
|
||||
console.warn(`Could not find appropriate location to insert ${name} in jest.config.js`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Insert the new library line
|
||||
lines.splice(insertIndex, 0, ` ${libJestPath}`);
|
||||
|
||||
// Write back the updated content
|
||||
tree.write(jestConfigPath, lines.join("\n"));
|
||||
}
|
||||
|
||||
export default basicLibGenerator;
|
||||
|
||||
Reference in New Issue
Block a user