Skip to content

Commit c055417

Browse files
Merge pull request #586 from contentstack/VP-1829-upgrade-uuid-and-fix-audit
chore: update vitest from v3 to v4 and its related dependencies
2 parents 0f52061 + 774c1c4 commit c055417

11 files changed

Lines changed: 417 additions & 1587 deletions

File tree

package-lock.json

Lines changed: 344 additions & 1548 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@
4747
"devDependencies": {
4848
"@commitlint/cli": "^20.2.0",
4949
"@commitlint/config-conventional": "^16.2.4",
50-
"@contentstack/advanced-post-message": "^0.0.2",
50+
"@contentstack/advanced-post-message": "^0.0.5",
5151
"@eslint/js": "^9.10.0",
5252
"@testing-library/jest-dom": "^6.5.0",
5353
"@testing-library/preact": "^3.2.4",
5454
"@testing-library/user-event": "^14.5.2",
5555
"@types/jsdom": "^21.1.6",
5656
"@types/lodash-es": "^4.17.12",
5757
"@types/mustache": "^4.2.2",
58+
"@types/node": "^25.6.0",
5859
"@types/react": "^18.2.57",
5960
"@types/react-dom": "^18.2.19",
60-
"@types/uuid": "^8.3.1",
61-
"@vitest/coverage-v8": "^3.2.4",
62-
"@vitest/ui": "^3.2.4",
61+
"@vitest/coverage-v8": "^4.1.5",
62+
"@vitest/ui": "^4.1.5",
6363
"auto-changelog": "^2.5.0",
6464
"esbuild-plugin-file-path-extensions": "^2.1.0",
6565
"eslint": "^8.57.1",
@@ -78,7 +78,7 @@
7878
"typedoc": "^0.25.13",
7979
"typescript": "^5.4.5",
8080
"typescript-eslint": "^8.5.0",
81-
"vitest": "^3.2.4"
81+
"vitest": "^4.1.5"
8282
},
8383
"repository": {
8484
"type": "git",
@@ -97,7 +97,7 @@
9797
"lodash-es": "^4.18.1",
9898
"mustache": "^4.2.0",
9999
"preact": "^10.28.2",
100-
"uuid": "^11.0.3"
100+
"uuid": "^14.0.0"
101101
},
102102
"optionalDependencies": {
103103
"@rollup/rollup-linux-x64-gnu": "4.9.5"

src/__test__/domObserverMocks.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { vi } from "vitest";
2+
3+
/**
4+
* Vitest 4 requires globals used with `new` to be constructible (class or `function`);
5+
* `vi.fn().mockImplementation(() => ({ ... }))` is not a constructor and throws.
6+
*/
7+
class MockResizeObserver {
8+
observe = vi.fn();
9+
unobserve = vi.fn();
10+
disconnect = vi.fn();
11+
12+
constructor(_: ResizeObserverCallback) {}
13+
}
14+
15+
class MockMutationObserver {
16+
observe = vi.fn();
17+
disconnect = vi.fn();
18+
takeRecords = vi.fn((): MutationRecord[] => []);
19+
20+
constructor(_: MutationCallback) {}
21+
}
22+
23+
export const constructibleResizeObserver =
24+
MockResizeObserver as unknown as typeof ResizeObserver;
25+
26+
export const constructibleMutationObserver =
27+
MockMutationObserver as unknown as typeof MutationObserver;
28+
29+
export function installGlobalObserverMocks() {
30+
global.ResizeObserver = constructibleResizeObserver;
31+
global.MutationObserver = constructibleMutationObserver;
32+
}

src/light-sdk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class LightLivePreviewHoC {
6060
}
6161

6262
static getSdkVersion(): string {
63-
return process?.env?.PACKAGE_VERSION!;
63+
return (typeof process !== "undefined" ? process?.env?.PACKAGE_VERSION : undefined) ?? "";
6464
}
6565
}
6666

src/livePreview/eventManager/__test__/livePreviewEventManager.test.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import { vi } from "vitest";
66
import { EventManager } from "@contentstack/advanced-post-message";
77
import { LIVE_PREVIEW_CHANNEL_ID } from "../livePreviewEventManager.constant";
88

9-
// Mock dependencies
9+
// Mock dependencies — must be constructible (`new EventManager()`) (Vitest 4)
1010
vi.mock("@contentstack/advanced-post-message", () => ({
11-
EventManager: vi.fn(),
11+
EventManager: vi.fn(function (this: unknown) {
12+
return { on: vi.fn(), send: vi.fn() };
13+
}),
1214
}));
1315

1416
vi.mock("../../../common/inIframe", () => ({
@@ -31,7 +33,9 @@ describe("livePreviewEventManager", () => {
3133
on: vi.fn(),
3234
send: vi.fn(),
3335
};
34-
(EventManager as any).mockImplementation(() => mockEventManager);
36+
(EventManager as any).mockImplementation(function () {
37+
return mockEventManager;
38+
});
3539

3640
// Store original window
3741
originalWindow = global.window;
@@ -209,14 +213,21 @@ describe("livePreviewEventManager", () => {
209213
});
210214

211215
it("should handle when EventManager constructor throws", async () => {
212-
(EventManager as any).mockImplementation(() => {
216+
(EventManager as any).mockImplementation(function () {
213217
throw new Error("EventManager constructor error");
214218
});
215219

216-
// Should not crash the module initialization
217-
expect(async () => {
218-
await import("../livePreviewEventManager");
219-
}).not.toThrow();
220+
// `new EventManager` runs while the module loads, so a throwing constructor
221+
// fails module evaluation and `import()` gets a *rejected* promise.
222+
//
223+
// We use `rejects.toThrow` (and await it), not `expect(() => import).not.toThrow()`:
224+
// the latter only catches synchronous throws; async failures become promise
225+
// rejections, which the old pattern missed and could surface as unhandled
226+
// rejections (e.g. under Vitest 4). This matches actual runtime behavior, not
227+
// a reversed expectation: the module does not catch the error internally.
228+
await expect(
229+
import("../livePreviewEventManager")
230+
).rejects.toThrow("EventManager constructor error");
220231
});
221232
});
222233
});

src/visualBuilder/__test__/click/fields/all-click.test.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import { screen, waitFor } from "@testing-library/preact";
2929
import "@testing-library/jest-dom";
30+
import { constructibleMutationObserver } from "../../../../__test__/domObserverMocks";
3031
import { getFieldSchemaMap } from "../../../../__test__/data/fieldSchemaMap";
3132
import Config from "../../../../configManager/configManager";
3233
import { VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY } from "../../../utils/constants";
@@ -40,10 +41,7 @@ import { triggerAndWaitForClickAction } from "../../../../__test__/utils";
4041
import { FieldDataType } from "../../../utils/types/index.types";
4142
import { ALLOWED_MODAL_EDITABLE_FIELD } from "../../../utils/constants";
4243

43-
global.MutationObserver = vi.fn().mockImplementation(() => ({
44-
observe: vi.fn(),
45-
disconnect: vi.fn(),
46-
}));
44+
global.MutationObserver = constructibleMutationObserver;
4745

4846
vi.mock("../../../components/FieldToolbar", () => {
4947
return {

src/visualBuilder/__test__/hover/fields/file.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { screen, waitFor } from "@testing-library/preact";
2+
import { constructibleMutationObserver } from "../../../../__test__/domObserverMocks";
23
import { getFieldSchemaMap } from "../../../../__test__/data/fieldSchemaMap";
34
import {
45
waitForHoverOutline,
@@ -88,10 +89,7 @@ describe("When an element is hovered in visual builder mode", () => {
8889
getFieldSchemaMap().all_fields
8990
);
9091

91-
global.MutationObserver = vi.fn().mockImplementation(() => ({
92-
observe: vi.fn(),
93-
disconnect: vi.fn(),
94-
}));
92+
global.MutationObserver = constructibleMutationObserver;
9593
});
9694

9795
beforeEach(() => {

src/visualBuilder/__test__/hover/fields/group.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ vi.mock("../../../utils/visualBuilderPostMessage", async () => {
2222
return Promise.resolve({
2323
contentTypes,
2424
});
25-
return Promise.resolve();
25+
return Promise.resolve({});
2626
}),
2727
},
2828
};

src/visualBuilder/utils/__test__/visualBuilderPostMessage.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { VISUAL_BUILDER_CHANNEL_ID } from "../constants";
55

66
vi.mock('@contentstack/advanced-post-message', () => {
77
return {
8-
EventManager: vi.fn()
8+
EventManager: vi.fn(function () {
9+
return { on: vi.fn(), send: vi.fn() };
10+
}),
911
};
1012
});
1113

@@ -27,8 +29,10 @@ describe('visualBuilderPostMessage', () => {
2729
});
2830

2931
it('should initialize EventManager if window is defined', async () => {
30-
const mockEventManagerInstance = {};
31-
EventManager.mockImplementation(() => mockEventManagerInstance);
32+
const mockEventManagerInstance = { on: vi.fn(), send: vi.fn() };
33+
vi.mocked(EventManager).mockImplementation(function () {
34+
return mockEventManagerInstance;
35+
});
3236
const module = await import('../visualBuilderPostMessage');
3337

3438
expect(EventManager).toHaveBeenCalledWith(VISUAL_BUILDER_CHANNEL_ID, {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"declaration": true ,
77
"declarationMap": true ,
88
"lib": ["DOM", "DOM.Iterable", "ES2022"],
9-
"types": ["vitest/globals"],
9+
"types": ["vitest/globals", "node"],
1010
"sourceMap": true ,
1111
"outDir": "./dist" ,
1212
"rootDir": "." ,

0 commit comments

Comments
 (0)