1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 17:53:39 +00:00
Files
browser/libs/state/src/core/state-definitions.spec.ts
Addison Beck 5f7c0ae999 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
2025-08-27 11:56:42 -04:00

63 lines
2.7 KiB
TypeScript

import { ClientLocations } from "@bitwarden/storage-core";
import { StateDefinition } from "./state-definition";
import * as stateDefinitionsRecord from "./state-definitions";
describe.each(["web", "cli", "desktop", "browser"])(
"state definitions follow rules for client %s",
(clientType: keyof ClientLocations) => {
const trackedNames: [string, string][] = [];
test.each(Object.entries(stateDefinitionsRecord))(
"that export %s follows all rules",
(exportName, stateDefinition) => {
// All exports from state-definitions are expected to be StateDefinition's
if (!(stateDefinition instanceof StateDefinition)) {
throw new Error(`export ${exportName} is expected to be a StateDefinition`);
}
const storageLocation =
stateDefinition.storageLocationOverrides[clientType] ??
stateDefinition.defaultStorageLocation;
const fullName = `${stateDefinition.name}_${storageLocation}`;
const exactConflictingExport = trackedNames.find(
([_, trackedName]) => trackedName === fullName,
);
if (exactConflictingExport !== undefined) {
const [conflictingExportName] = exactConflictingExport;
throw new Error(
`The export '${exportName}' has a conflicting state name and storage location with export ` +
`'${conflictingExportName}' please ensure that you choose a unique name and location for all clients.`,
);
}
const roughConflictingExport = trackedNames.find(
([_, trackedName]) => trackedName.toLowerCase() === fullName.toLowerCase(),
);
if (roughConflictingExport !== undefined) {
const [conflictingExportName] = roughConflictingExport;
throw new Error(
`The export '${exportName}' differs its state name and storage location ` +
`only by casing with export '${conflictingExportName}' please ensure it differs by more than casing.`,
);
}
const name = stateDefinition.name;
expect(name).not.toBeUndefined(); // undefined in an invalid name
expect(name).not.toBeNull(); // null is in invalid name
expect(name.length).toBeGreaterThan(3); // A 3 characters or less name is not descriptive enough
expect(name[0]).toEqual(name[0].toLowerCase()); // First character should be lower case since camelCase is required
expect(name).not.toContain(" "); // There should be no spaces in a state name
expect(name).not.toContain("_"); // We should not be doing snake_case for state name
// NOTE: We could expect some details about the export name as well
trackedNames.push([exportName, fullName]);
},
);
},
);