diff --git a/.eslintrc.js b/.eslintrc.js index 6cda0417e03..bb925c81709 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -199,7 +199,9 @@ module.exports = { "import/no-extraneous-dependencies": [ "error", { - devDependencies: false, + // TODO This one is to make it work with unit tests. + // Perhaps a better solution could be a different config for both? + devDependencies: true, }, ], "import/no-internal-modules": "off", diff --git a/package.json b/package.json index 802b83a1828..d3df2da4137 100644 --- a/package.json +++ b/package.json @@ -175,8 +175,8 @@ "standalone": "node ./scripts/run_standalone_demo.mjs", "start": "node ./scripts/start_demo_web_server.mjs", "start:wasm": "node ./scripts/start_demo_web_server.mjs --include-wasm", + "test:unit:vitest": "vitest --config vitest.config.unit.mjs", "test:vitest": "node ./tests/integration/run_vitest.mjs", - "vitest": "vitest", "test:integration": "vitest tests/integration", "test:integration:chrome": "node tests/integration/run.mjs --bchrome", "test:integration:chrome:watch": "node tests/integration/run.mjs --bchrome --watch", diff --git a/src/compat/__tests__/add_text_track.test.ts b/src/compat/__tests__/add_text_track.test.ts index d84d84b683d..22277ad3e2a 100644 --- a/src/compat/__tests__/add_text_track.test.ts +++ b/src/compat/__tests__/add_text_track.test.ts @@ -1,50 +1,37 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Needed for calling require (which itself is needed to mock properly) because +import { describe, beforeEach, it, expect, vi } from "vitest"; + +// Needed for calling require (which itself is needed to doMock properly) because // it is not type-checked: /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-argument */ +/* eslint-disable @typescript-eslint/no-explicit-any */ describe("compat - addTextTrack", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should re-use text track on IE / EDGE", () => { + it("should re-use text track on IE / EDGE", async () => { const fakeTextTrack = { id: "textTrack1", HIDDEN: "hidden", SHOWING: "showing", } as unknown as TextTrack; - const mockAddTextTrack = jest.fn(() => null); + const mockAddTextTrack = vi.fn(() => null); const fakeMediaElement = { textTracks: [fakeTextTrack], addTextTrack: mockAddTextTrack, - }; + } as unknown as HTMLMediaElement; + - jest.mock("../browser_detection", () => ({ + vi.doMock("../browser_detection", () => ({ __esModule: true as const, isIEOrEdge: true, })); - - const addTextTrack = jest.requireActual("../add_text_track").default; + const { default: addTextTrack } = await vi.importActual("../add_text_track") as any; const { track, trackElement } = addTextTrack(fakeMediaElement); expect(trackElement).toBe(undefined); expect(track).toBe(fakeTextTrack); @@ -52,14 +39,14 @@ describe("compat - addTextTrack", () => { expect(mockAddTextTrack).not.toHaveBeenCalled(); }); - it("should add text track if no track on media element on IE / EDGE", () => { + it("should add text track if no track on media element on IE / EDGE", async () => { const fakeTextTrack = { id: "textTrack1", HIDDEN: "hidden", SHOWING: "showing", } as unknown as TextTrack; const fakeTextTracks: TextTrack[] = []; - const mockAddTextTrack = jest.fn(() => { + const mockAddTextTrack = vi.fn(() => { fakeTextTracks.push(fakeTextTrack); return fakeTextTrack; }); @@ -67,14 +54,14 @@ describe("compat - addTextTrack", () => { const fakeMediaElement = { textTracks: fakeTextTracks, addTextTrack: mockAddTextTrack, - }; + } as unknown as HTMLMediaElement; - jest.mock("../browser_detection", () => ({ + vi.doMock("../browser_detection", () => ({ __esModule: true as const, isIEOrEdge: true, })); - const addTextTrack = jest.requireActual("../add_text_track").default; + const { default: addTextTrack } = await vi.importActual("../add_text_track") as any; const { track, trackElement } = addTextTrack(fakeMediaElement); expect(trackElement).toBe(undefined); expect(track).toBe(fakeTextTrack); @@ -84,11 +71,12 @@ describe("compat - addTextTrack", () => { expect(mockAddTextTrack).toHaveBeenCalledTimes(1); }); - it("should create showing trackElement and set track on mediaElement", () => { - jest.mock("../browser_detection", () => ({ + it("should create showing trackElement and set track on mediaElement", async () => { + vi.doMock("../browser_detection", () => ({ __esModule: true as const, isIEOrEdge: false, })); + const { default: addTextTrack } = await vi.importActual("../add_text_track") as any; const fakeTextTrack = { id: "textTrack1", @@ -103,7 +91,7 @@ describe("compat - addTextTrack", () => { const fakeTextTracks: TextTrack[] = []; const fakeChildNodes: ChildNode[] = []; - const mockAppendChild = jest.fn((_trackElement) => { + const mockAppendChild = vi.fn((_trackElement) => { fakeChildNodes.push(_trackElement); fakeTextTracks.push(_trackElement.track); }); @@ -112,13 +100,12 @@ describe("compat - addTextTrack", () => { textTracks: fakeTextTracks, appendChild: mockAppendChild, childNodes: fakeChildNodes, - }; + } as unknown as HTMLMediaElement; - const spyOnCreateElement = jest + const spyOnCreateElement = vi .spyOn(document, "createElement") .mockImplementation(() => fakeTextTrackElement as unknown as HTMLElement); - const addTextTrack = jest.requireActual("../add_text_track").default; const { track, trackElement } = addTextTrack(fakeMediaElement); expect(track).toBe(fakeTextTrack); expect(track.mode).toBe("showing"); diff --git a/src/compat/__tests__/browser_compatibility_types.test.ts b/src/compat/__tests__/browser_compatibility_types.test.ts index 57f928b4403..eb3ee40406f 100644 --- a/src/compat/__tests__/browser_compatibility_types.test.ts +++ b/src/compat/__tests__/browser_compatibility_types.test.ts @@ -1,22 +1,7 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; import globalScope from "../../utils/global_scope"; -// Needed for calling require (which itself is needed to mock properly) because +// Needed for calling require (which itself is needed to doMock properly) because // it is not type-checked: /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -34,11 +19,11 @@ describe("compat - browser compatibility types", () => { } const gs = globalScope as IFakeWindow; beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should use the native MediaSource if defined", () => { - jest.mock("../../utils/is_node", () => ({ + it("should use the native MediaSource if defined", async () => { + vi.doMock("../../utils/is_node", () => ({ __esModule: true as const, default: false, })); @@ -53,7 +38,7 @@ describe("compat - browser compatibility types", () => { gs.WebKitMediaSource = { a: 3 }; gs.MSMediaSource = { a: 4 }; - const { MediaSource_ } = jest.requireActual("../browser_compatibility_types"); + const { MediaSource_ } = await vi.importActual("../browser_compatibility_types"); expect(MediaSource_).toEqual({ a: 1 }); gs.MediaSource = origMediaSource; @@ -62,8 +47,8 @@ describe("compat - browser compatibility types", () => { gs.MSMediaSource = origMSMediaSource; }); - it("should use MozMediaSource if defined and MediaSource is not", () => { - jest.mock("../../utils/is_node", () => ({ + it("should use MozMediaSource if defined and MediaSource is not", async () => { + vi.doMock("../../utils/is_node", () => ({ __esModule: true as const, default: false, })); @@ -78,7 +63,7 @@ describe("compat - browser compatibility types", () => { gs.WebKitMediaSource = undefined; gs.MSMediaSource = undefined; - const { MediaSource_ } = jest.requireActual("../browser_compatibility_types"); + const { MediaSource_ } = await vi.importActual("../browser_compatibility_types"); expect(MediaSource_).toEqual({ a: 2 }); gs.MediaSource = origMediaSource; @@ -87,8 +72,8 @@ describe("compat - browser compatibility types", () => { gs.MSMediaSource = origMSMediaSource; }); - it("should use WebKitMediaSource if defined and MediaSource is not", () => { - jest.mock("../../utils/is_node", () => ({ + it("should use WebKitMediaSource if defined and MediaSource is not", async () => { + vi.doMock("../../utils/is_node", () => ({ __esModule: true as const, default: false, })); @@ -103,7 +88,7 @@ describe("compat - browser compatibility types", () => { gs.WebKitMediaSource = { a: 3 }; gs.MSMediaSource = undefined; - const { MediaSource_ } = jest.requireActual("../browser_compatibility_types"); + const { MediaSource_ } = await vi.importActual("../browser_compatibility_types"); expect(MediaSource_).toEqual({ a: 3 }); gs.MediaSource = origMediaSource; @@ -112,8 +97,8 @@ describe("compat - browser compatibility types", () => { gs.MSMediaSource = origMSMediaSource; }); - it("should use MSMediaSource if defined and MediaSource is not", () => { - jest.mock("../../utils/is_node", () => ({ + it("should use MSMediaSource if defined and MediaSource is not", async () => { + vi.doMock("../../utils/is_node", () => ({ __esModule: true as const, default: false, })); @@ -128,7 +113,7 @@ describe("compat - browser compatibility types", () => { gs.WebKitMediaSource = undefined; gs.MSMediaSource = { a: 4 }; - const { MediaSource_ } = jest.requireActual("../browser_compatibility_types"); + const { MediaSource_ } = await vi.importActual("../browser_compatibility_types"); expect(MediaSource_).toEqual({ a: 4 }); gs.MediaSource = origMediaSource; diff --git a/src/compat/__tests__/browser_version.test.ts b/src/compat/__tests__/browser_version.test.ts index 1b7bf06a62f..93062622447 100644 --- a/src/compat/__tests__/browser_version.test.ts +++ b/src/compat/__tests__/browser_version.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -42,43 +28,43 @@ describe("Compat - Browser version", () => { afterEach(() => { nav.userAgent = origUserAgent; - jest.resetModules(); + vi.resetModules(); }); - it("Should return correct Firefox version (60)", () => { - jest.mock("../browser_detection", () => { + it("Should return correct Firefox version (60)", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isFirefox: true }; }); - const { getFirefoxVersion } = jest.requireActual("../browser_version"); + const { getFirefoxVersion } = await vi.importActual("../browser_version"); nav.userAgent = "Firefox/60.0"; const version = getFirefoxVersion(); expect(version).toBe(60); }); - it("Should return correct Firefox version (80)", () => { - jest.mock("../browser_detection", () => { + it("Should return correct Firefox version (80)", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isFirefox: true }; }); - const { getFirefoxVersion } = jest.requireActual("../browser_version"); + const { getFirefoxVersion } = await vi.importActual("../browser_version"); nav.userAgent = "Firefox/80.0"; const version = getFirefoxVersion(); expect(version).toBe(80); }); - it("Should return null when not on Firefox", () => { - jest.mock("../browser_detection", () => { + it("Should return null when not on Firefox", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isFirefox: false }; }); - const { getFirefoxVersion } = jest.requireActual("../browser_version"); + const { getFirefoxVersion } = await vi.importActual("../browser_version"); const version = getFirefoxVersion(); expect(version).toBe(null); }); - it("Should return null when obscure Firefox user agent", () => { - jest.mock("../browser_detection", () => { + it("Should return null when obscure Firefox user agent", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isFirefox: true }; }); - const { getFirefoxVersion } = jest.requireActual("../browser_version"); + const { getFirefoxVersion } = await vi.importActual("../browser_version"); nav.userAgent = "FireFennec/80.0"; const version = getFirefoxVersion(); expect(version).toBe(-1); diff --git a/src/compat/__tests__/can_patch_isobmff.test.ts b/src/compat/__tests__/can_patch_isobmff.test.ts index c06809e6490..860580b3944 100644 --- a/src/compat/__tests__/can_patch_isobmff.test.ts +++ b/src/compat/__tests__/can_patch_isobmff.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -23,28 +9,28 @@ describe("compat - canPatchISOBMFFSegment", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should return true if we are not on IE11 nor Edge", () => { - jest.mock("../browser_detection", () => { + it("should return true if we are not on IE11 nor Edge", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isIEOrEdge: false, }; }); - const canPatchISOBMFFSegment = jest.requireActual("../can_patch_isobmff"); + const canPatchISOBMFFSegment = await vi.importActual("../can_patch_isobmff"); expect(canPatchISOBMFFSegment.default()).toBe(true); }); - it("should return false if we are on IE11 or Edge", () => { - jest.mock("../browser_detection", () => { + it("should return false if we are on IE11 or Edge", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isIEOrEdge: true, }; }); - const canPatchISOBMFFSegment = jest.requireActual("../can_patch_isobmff"); + const canPatchISOBMFFSegment = await vi.importActual("../can_patch_isobmff"); expect(canPatchISOBMFFSegment.default()).toBe(false); }); }); diff --git a/src/compat/__tests__/can_rely_on_video_visibility_and_size.test.ts b/src/compat/__tests__/can_rely_on_video_visibility_and_size.test.ts index 4a2761eb35e..f2d9ef7f5d1 100644 --- a/src/compat/__tests__/can_rely_on_video_visibility_and_size.test.ts +++ b/src/compat/__tests__/can_rely_on_video_visibility_and_size.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-unsafe-call */ @@ -21,53 +7,53 @@ describe("Compat - canRelyOnVideoVisibilityAndSize", () => { afterEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should return true on any browser but Firefox", () => { - jest.mock("../browser_detection", () => { + it("should return true on any browser but Firefox", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isFirefox: false }; }); - const canRelyOnVideoVisibilityAndSize = jest.requireActual( + const canRelyOnVideoVisibilityAndSize = await vi.importActual( "../can_rely_on_video_visibility_and_size.ts", ); expect(canRelyOnVideoVisibilityAndSize.default()).toBe(true); }); - it("should return true on Firefox but the version is unknown", () => { - jest.mock("../browser_detection", () => { + it("should return true on Firefox but the version is unknown", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isFirefox: true }; }); - jest.mock("../browser_version", () => { + vi.doMock("../browser_version", () => { return { __esModule: true as const, getFirefoxVersion: () => -1 }; }); - const canRelyOnVideoVisibilityAndSize = jest.requireActual( + const canRelyOnVideoVisibilityAndSize = await vi.importActual( "../can_rely_on_video_visibility_and_size.ts", ); expect(canRelyOnVideoVisibilityAndSize.default()).toBe(true); }); - it("should return true on Firefox < 67>", () => { - jest.mock("../browser_detection", () => { + it("should return true on Firefox < 67>", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isFirefox: true }; }); - jest.mock("../browser_version", () => { + vi.doMock("../browser_version", () => { return { __esModule: true as const, getFirefoxVersion: () => 60 }; }); - const canRelyOnVideoVisibilityAndSize = jest.requireActual( + const canRelyOnVideoVisibilityAndSize = await vi.importActual( "../can_rely_on_video_visibility_and_size.ts", ); expect(canRelyOnVideoVisibilityAndSize.default()).toBe(true); }); - it("should return false on Firefox >= 67", () => { - jest.mock("../browser_detection", () => { + it("should return false on Firefox >= 67", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isFirefox: true }; }); - jest.mock("../browser_version", () => { + vi.doMock("../browser_version", () => { return { __esModule: true as const, getFirefoxVersion: () => 83 }; }); - const canRelyOnVideoVisibilityAndSize = jest.requireActual( + const canRelyOnVideoVisibilityAndSize = await vi.importActual( "../can_rely_on_video_visibility_and_size.ts", ); expect(canRelyOnVideoVisibilityAndSize.default()).toBe(false); diff --git a/src/compat/__tests__/can_reuse_media_keys.test.ts b/src/compat/__tests__/can_reuse_media_keys.test.ts index 55c3e61e58f..dbfe1f14cc6 100644 --- a/src/compat/__tests__/can_reuse_media_keys.test.ts +++ b/src/compat/__tests__/can_reuse_media_keys.test.ts @@ -1,26 +1,38 @@ +import { describe, afterEach, it, expect, vi } from "vitest"; + /* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ +/* eslint-disable @typescript-eslint/no-explicit-any */ describe("Compat - canReuseMediaKeys", () => { afterEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should return true on any browser but WebOS", () => { - jest.mock("../browser_detection", () => { - return { __esModule: true as const, isWebOs: false }; + it("should return true on any browser but WebOS", async () => { + vi.doMock("../browser_detection", () => { + return { __esModule: true as const, isWebOs: false, isPanasonic: false }; }); - const canReuseMediaKeys = jest.requireActual("../can_reuse_media_keys.ts"); + const canReuseMediaKeys = (await vi.importActual( + "../can_reuse_media_keys.ts", + )) as any; expect(canReuseMediaKeys.default()).toBe(true); }); - it("should return false on WebOs", () => { - jest.mock("../browser_detection", () => { - return { __esModule: true as const, isWebOs: true, isWebOs2022: false }; + it("should return false on WebOs", async () => { + vi.doMock("../browser_detection", () => { + return { + __esModule: true as const, + isWebOs: true, + isWebOs2022: false, + isPanasonic: false, + }; }); - const canReuseMediaKeys = jest.requireActual("../can_reuse_media_keys.ts"); + const canReuseMediaKeys = (await vi.importActual( + "../can_reuse_media_keys.ts", + )) as any; expect(canReuseMediaKeys.default()).toBe(false); }); }); diff --git a/src/compat/__tests__/change_source_buffer_type.test.ts b/src/compat/__tests__/change_source_buffer_type.test.ts index dd95fdc7a8d..9325257e29a 100644 --- a/src/compat/__tests__/change_source_buffer_type.test.ts +++ b/src/compat/__tests__/change_source_buffer_type.test.ts @@ -1,33 +1,18 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; import log from "../../log"; import tryToChangeSourceBufferType from "../change_source_buffer_type"; describe("Compat - tryToChangeSourceBufferType", () => { it("should just return false if the SourceBuffer provided does not have a changeType method", () => { - const spy = jest.spyOn(log, "warn"); + const spy = vi.spyOn(log, "warn"); const fakeSourceBuffer: SourceBuffer = {} as unknown as SourceBuffer; expect(tryToChangeSourceBufferType(fakeSourceBuffer, "toto")).toBe(false); expect(spy).not.toHaveBeenCalled(); }); it("should return true if the SourceBuffer provided does have a changeType method and the API returned normally", () => { - const spy = jest.spyOn(log, "warn"); - const changeTypeFn = jest.fn(); + const spy = vi.spyOn(log, "warn"); + const changeTypeFn = vi.fn(); const fakeSourceBuffer = { changeType: changeTypeFn, } as unknown as SourceBuffer; @@ -36,9 +21,9 @@ describe("Compat - tryToChangeSourceBufferType", () => { }); it("should return false and warn if the SourceBuffer provided does have a changeType method and the API threw", () => { - const spy = jest.spyOn(log, "warn"); + const spy = vi.spyOn(log, "warn"); const err = new Error("bar"); - const changeTypeFn = jest.fn(() => { + const changeTypeFn = vi.fn(() => { throw err; }); const fakeSourceBuffer = { diff --git a/src/compat/__tests__/clear_element_src.test.ts b/src/compat/__tests__/clear_element_src.test.ts index 191350c65c9..33b2f40bf01 100644 --- a/src/compat/__tests__/clear_element_src.test.ts +++ b/src/compat/__tests__/clear_element_src.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-var-requires */ @@ -24,43 +10,43 @@ import arrayFindIndex from "../../utils/array_find_index"; describe("Compat - clearElementSrc", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should empty the src and remove the Attribute for a given Element", () => { + it("should empty the src and remove the Attribute for a given Element", async () => { const fakeElement = { src: "foo", removeAttribute() { return null; }, }; - const clearElementSrc = jest.requireActual("../clear_element_src").default; + const clearElementSrc = await vi.importActual("../clear_element_src"); - const spyRemoveAttribute = jest.spyOn(fakeElement, "removeAttribute"); - clearElementSrc(fakeElement); + const spyRemoveAttribute = vi.spyOn(fakeElement, "removeAttribute"); + clearElementSrc.default(fakeElement); expect(fakeElement.src).toBe(""); expect(spyRemoveAttribute).toHaveBeenCalledTimes(1); expect(spyRemoveAttribute).toHaveBeenCalledWith("src"); }); - it("should throw if failed to remove the Attribute for a given Element", () => { + it("should throw if failed to remove the Attribute for a given Element", async () => { const fakeElement = { src: "foo", removeAttribute() { throw new Error("Oups, can't remove attribute."); }, }; - const clearElementSrc = jest.requireActual("../clear_element_src").default; - const spyRemoveAttribute = jest.spyOn(fakeElement, "removeAttribute"); + const clearElementSrc = await vi.importActual("../clear_element_src"); + const spyRemoveAttribute = vi.spyOn(fakeElement, "removeAttribute"); - expect(() => clearElementSrc(fakeElement)).toThrowError( + expect(() => clearElementSrc.default(fakeElement)).toThrowError( "Oups, can't remove attribute.", ); expect(fakeElement.src).toBe(""); expect(spyRemoveAttribute).toHaveBeenCalledTimes(1); }); - it("should disable text tracks and remove childs", () => { + it("should disable text tracks and remove childs", async () => { const fakeElement = { src: "foo", removeAttribute() { @@ -76,13 +62,13 @@ describe("Compat - clearElementSrc", () => { }, }; - const clearElementSrc = jest.requireActual("../clear_element_src").default; + const clearElementSrc = await vi.importActual("../clear_element_src"); - const spyRemoveAttribute = jest.spyOn(fakeElement, "removeAttribute"); - const spyHasChildNodes = jest.spyOn(fakeElement, "hasChildNodes"); - const spyRemoveChild = jest.spyOn(fakeElement, "removeChild"); + const spyRemoveAttribute = vi.spyOn(fakeElement, "removeAttribute"); + const spyHasChildNodes = vi.spyOn(fakeElement, "hasChildNodes"); + const spyRemoveChild = vi.spyOn(fakeElement, "removeChild"); - clearElementSrc(fakeElement); + clearElementSrc.default(fakeElement); expect(fakeElement.src).toBe(""); expect(fakeElement.textTracks[0].mode).toBe("disabled"); @@ -95,7 +81,7 @@ describe("Compat - clearElementSrc", () => { expect(spyRemoveChild).toHaveBeenCalledWith({ nodeName: "track" }); }); - it("should log when failed to remove text track child node", () => { + it("should log when failed to remove text track child node", async () => { const fakeElement = { src: "foo", removeAttribute() { @@ -109,18 +95,18 @@ describe("Compat - clearElementSrc", () => { }, }; - const mockLogWarn = jest.fn((message) => message); - jest.mock("../../log", () => ({ + const mockLogWarn = vi.fn((message) => message); + vi.doMock("../../log", () => ({ __esModule: true as const, default: { warn: mockLogWarn }, })); - const clearElementSrc = jest.requireActual("../clear_element_src").default; + const clearElementSrc = await vi.importActual("../clear_element_src"); - const spyRemoveAttribute = jest.spyOn(fakeElement, "removeAttribute"); - const spyHasChildNodes = jest.spyOn(fakeElement, "hasChildNodes"); - const spyRemoveChild = jest.spyOn(fakeElement, "removeChild"); + const spyRemoveAttribute = vi.spyOn(fakeElement, "removeAttribute"); + const spyHasChildNodes = vi.spyOn(fakeElement, "hasChildNodes"); + const spyRemoveChild = vi.spyOn(fakeElement, "removeChild"); - clearElementSrc(fakeElement); + clearElementSrc.default(fakeElement); expect(fakeElement.src).toBe(""); expect(fakeElement.textTracks[0].mode).toBe("disabled"); @@ -140,7 +126,7 @@ describe("Compat - clearElementSrc", () => { ); }); - it("should not remove audio child node if on firefox and no text tracks", () => { + it("should not remove audio child node if on firefox and no text tracks", async () => { const fakeElement = { src: "foo", removeAttribute() { @@ -152,13 +138,13 @@ describe("Compat - clearElementSrc", () => { removeChild: () => null, }; - const clearElementSrc = jest.requireActual("../clear_element_src").default; + const clearElementSrc = await vi.importActual("../clear_element_src"); - const spyRemoveAttribute = jest.spyOn(fakeElement, "removeAttribute"); - const spyHasChildNodes = jest.spyOn(fakeElement, "hasChildNodes"); - const spyRemoveChild = jest.spyOn(fakeElement, "removeChild"); + const spyRemoveAttribute = vi.spyOn(fakeElement, "removeAttribute"); + const spyHasChildNodes = vi.spyOn(fakeElement, "hasChildNodes"); + const spyRemoveChild = vi.spyOn(fakeElement, "removeChild"); - clearElementSrc(fakeElement); + clearElementSrc.default(fakeElement); expect(fakeElement.src).toBe(""); expect(fakeElement.childNodes).toEqual([{ nodeName: "audio" }]); @@ -168,7 +154,7 @@ describe("Compat - clearElementSrc", () => { expect(spyRemoveChild).not.toHaveBeenCalled(); }); - it("should not handle text tracks nodes is has no child nodes", () => { + it("should not handle text tracks nodes is has no child nodes", async () => { const fakeElement = { src: "foo", removeAttribute() { @@ -180,13 +166,13 @@ describe("Compat - clearElementSrc", () => { removeChild: () => null, }; - const clearElementSrc = jest.requireActual("../clear_element_src").default; + const clearElementSrc = await vi.importActual("../clear_element_src"); - const spyRemoveAttribute = jest.spyOn(fakeElement, "removeAttribute"); - const spyHasChildNodes = jest.spyOn(fakeElement, "hasChildNodes"); - const spyRemoveChild = jest.spyOn(fakeElement, "removeChild"); + const spyRemoveAttribute = vi.spyOn(fakeElement, "removeAttribute"); + const spyHasChildNodes = vi.spyOn(fakeElement, "hasChildNodes"); + const spyRemoveChild = vi.spyOn(fakeElement, "removeChild"); - clearElementSrc(fakeElement); + clearElementSrc.default(fakeElement); expect(fakeElement.src).toBe(""); expect(fakeElement.childNodes).toEqual([]); @@ -196,7 +182,7 @@ describe("Compat - clearElementSrc", () => { expect(spyRemoveChild).not.toHaveBeenCalled(); }); - it("should not throw if the textTracks attribute is `null`", () => { + it("should not throw if the textTracks attribute is `null`", async () => { const fakeElement = { src: "foo", removeAttribute() { @@ -208,13 +194,13 @@ describe("Compat - clearElementSrc", () => { removeChild: () => null, }; - const clearElementSrc = jest.requireActual("../clear_element_src").default; + const clearElementSrc = await vi.importActual("../clear_element_src"); - const spyRemoveAttribute = jest.spyOn(fakeElement, "removeAttribute"); - const spyHasChildNodes = jest.spyOn(fakeElement, "hasChildNodes"); - const spyRemoveChild = jest.spyOn(fakeElement, "removeChild"); + const spyRemoveAttribute = vi.spyOn(fakeElement, "removeAttribute"); + const spyHasChildNodes = vi.spyOn(fakeElement, "hasChildNodes"); + const spyRemoveChild = vi.spyOn(fakeElement, "removeChild"); - clearElementSrc(fakeElement); + clearElementSrc.default(fakeElement); expect(fakeElement.src).toBe(""); expect(fakeElement.childNodes).toEqual([]); @@ -224,7 +210,7 @@ describe("Compat - clearElementSrc", () => { expect(spyRemoveChild).not.toHaveBeenCalled(); }); - it("should not throw if the textTracks attribute is `undefined`", () => { + it("should not throw if the textTracks attribute is `undefined`", async () => { const fakeElement = { src: "foo", removeAttribute() { @@ -236,13 +222,13 @@ describe("Compat - clearElementSrc", () => { removeChild: () => null, }; - const clearElementSrc = jest.requireActual("../clear_element_src").default; + const clearElementSrc = await vi.importActual("../clear_element_src"); - const spyRemoveAttribute = jest.spyOn(fakeElement, "removeAttribute"); - const spyHasChildNodes = jest.spyOn(fakeElement, "hasChildNodes"); - const spyRemoveChild = jest.spyOn(fakeElement, "removeChild"); + const spyRemoveAttribute = vi.spyOn(fakeElement, "removeAttribute"); + const spyHasChildNodes = vi.spyOn(fakeElement, "hasChildNodes"); + const spyRemoveChild = vi.spyOn(fakeElement, "removeChild"); - clearElementSrc(fakeElement); + clearElementSrc.default(fakeElement); expect(fakeElement.src).toBe(""); expect(fakeElement.childNodes).toEqual([]); diff --git a/src/compat/__tests__/enable_audio_track.test.ts b/src/compat/__tests__/enable_audio_track.test.ts index d8a86ba6c84..e8861e4769a 100644 --- a/src/compat/__tests__/enable_audio_track.test.ts +++ b/src/compat/__tests__/enable_audio_track.test.ts @@ -1,3 +1,4 @@ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-var-requires */ @@ -6,11 +7,11 @@ /* eslint-disable @typescript-eslint/no-unsafe-return */ describe("compat - enableAudioTrack", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should enable the wanted audioTrack", () => { - jest.mock("../browser_detection", () => { + it("should enable the wanted audioTrack", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isTizen: false, @@ -39,7 +40,7 @@ describe("compat - enableAudioTrack", () => { enabled: false, }, ]; - const enableAudioTrack = jest.requireActual("../enable_audio_track"); + const enableAudioTrack = await vi.importActual("../enable_audio_track"); expect(enableAudioTrack.default(fakeAudioTracks, 2)).toEqual(true); expect(fakeAudioTracks[0].enabled).toBe(false); expect(fakeAudioTracks[1].enabled).toBe(false); @@ -54,8 +55,8 @@ describe("compat - enableAudioTrack", () => { expect(fakeAudioTracks[2].enabled).toBe(false); }); - it("should enable the wanted audioTrack on Tizen", () => { - jest.mock("../browser_detection", () => { + it("should enable the wanted audioTrack on Tizen", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isTizen: true, @@ -84,7 +85,7 @@ describe("compat - enableAudioTrack", () => { enabled: false, }, ]; - const enableAudioTrack = jest.requireActual("../enable_audio_track"); + const enableAudioTrack = await vi.importActual("../enable_audio_track"); expect(enableAudioTrack.default(fakeAudioTracks, 2)).toEqual(true); expect(fakeAudioTracks[0].enabled).toBe(false); expect(fakeAudioTracks[1].enabled).toBe(false); @@ -99,8 +100,8 @@ describe("compat - enableAudioTrack", () => { expect(fakeAudioTracks[2].enabled).toBe(false); }); - it("should return false if the audio track index does not exist", () => { - jest.mock("../browser_detection", () => { + it("should return false if the audio track index does not exist", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isTizen: false, @@ -129,7 +130,7 @@ describe("compat - enableAudioTrack", () => { enabled: false, }, ]; - const enableAudioTrack = jest.requireActual("../enable_audio_track"); + const enableAudioTrack = await vi.importActual("../enable_audio_track"); expect(enableAudioTrack.default(fakeAudioTracks, -1)).toEqual(false); expect(fakeAudioTracks[0].enabled).toBe(false); expect(fakeAudioTracks[1].enabled).toBe(false); @@ -144,8 +145,8 @@ describe("compat - enableAudioTrack", () => { expect(fakeAudioTracks[2].enabled).toBe(false); }); - it("should return false if the audio track index does not exist on Tizen", () => { - jest.mock("../browser_detection", () => { + it("should return false if the audio track index does not exist on Tizen", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isTizen: false, @@ -225,7 +226,7 @@ describe("compat - enableAudioTrack", () => { track3IsEnabled = enabled; }, }); - const enableAudioTrack = jest.requireActual("../enable_audio_track"); + const enableAudioTrack = await vi.importActual("../enable_audio_track"); expect(enableAudioTrack.default(fakeAudioTracks, 1)).toBe(true); expect(fakeAudioTracks[0].enabled).toBe(false); expect(fakeAudioTracks[1].enabled).toBe(true); @@ -241,8 +242,8 @@ describe("compat - enableAudioTrack", () => { expect(track3WasEnabled).toBe(0); }); - it("should first disable all audioTracks except the one wanted by default on Tizen", () => { - jest.mock("../browser_detection", () => { + it("should first disable all audioTracks except the one wanted by default on Tizen", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isTizen: true, @@ -322,7 +323,7 @@ describe("compat - enableAudioTrack", () => { track3IsEnabled = enabled; }, }); - const enableAudioTrack = jest.requireActual("../enable_audio_track"); + const enableAudioTrack = await vi.importActual("../enable_audio_track"); expect(enableAudioTrack.default(fakeAudioTracks, 1)).toBe(true); expect(fakeAudioTracks[0].enabled).toBe(false); expect(fakeAudioTracks[1].enabled).toBe(true); diff --git a/src/compat/__tests__/generate_init_data.test.ts b/src/compat/__tests__/generate_init_data.test.ts index 9616ed83aa5..c349a2bff82 100644 --- a/src/compat/__tests__/generate_init_data.test.ts +++ b/src/compat/__tests__/generate_init_data.test.ts @@ -1,3 +1,4 @@ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; import { utf16LEToStr } from "../../utils/string_parsing"; import { generatePlayReadyInitData } from "../generate_init_data"; diff --git a/src/compat/__tests__/is_codec_supported.test.ts b/src/compat/__tests__/is_codec_supported.test.ts index 34d7f0f4a73..77f23538043 100644 --- a/src/compat/__tests__/is_codec_supported.test.ts +++ b/src/compat/__tests__/is_codec_supported.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -23,35 +9,35 @@ describe("Compat - isCodecSupported", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should return false if MediaSource is not supported in the current device", () => { - jest.mock("../browser_compatibility_types", () => { + it("should return false if MediaSource is not supported in the current device", async () => { + vi.doMock("../browser_compatibility_types", () => { return { __esModule: true as const, MediaSource_: undefined, }; }); - const isCodecSupported = jest.requireActual("../is_codec_supported").default; - expect(isCodecSupported("foo")).toEqual(false); - expect(isCodecSupported("")).toEqual(false); + const isCodecSupported = await vi.importActual("../is_codec_supported"); + expect(isCodecSupported.default("foo")).toEqual(false); + expect(isCodecSupported.default("")).toEqual(false); }); - it("should return true in any case if the MediaSource does not have the right function", () => { - jest.mock("../browser_compatibility_types", () => { + it("should return true in any case if the MediaSource does not have the right function", async () => { + vi.doMock("../browser_compatibility_types", () => { return { __esModule: true as const, MediaSource_: { isTypeSupported: undefined }, }; }); - const isCodecSupported = jest.requireActual("../is_codec_supported").default; - expect(isCodecSupported("foo")).toEqual(true); - expect(isCodecSupported("")).toEqual(true); + const isCodecSupported = await vi.importActual("../is_codec_supported"); + expect(isCodecSupported.default("foo")).toEqual(true); + expect(isCodecSupported.default("")).toEqual(true); }); - it("should return true if MediaSource.isTypeSupported returns true", () => { - jest.mock("../browser_compatibility_types", () => { + it("should return true if MediaSource.isTypeSupported returns true", async () => { + vi.doMock("../browser_compatibility_types", () => { return { __esModule: true as const, MediaSource_: { @@ -61,13 +47,13 @@ describe("Compat - isCodecSupported", () => { }, }; }); - const isCodecSupported = jest.requireActual("../is_codec_supported").default; - expect(isCodecSupported("foo")).toEqual(true); - expect(isCodecSupported("")).toEqual(true); + const isCodecSupported = await vi.importActual("../is_codec_supported"); + expect(isCodecSupported.default("foo")).toEqual(true); + expect(isCodecSupported.default("")).toEqual(true); }); - it("should return false if MediaSource.isTypeSupported returns false", () => { - jest.mock("../browser_compatibility_types", () => { + it("should return false if MediaSource.isTypeSupported returns false", async () => { + vi.doMock("../browser_compatibility_types", () => { return { __esModule: true as const, MediaSource_: { @@ -77,8 +63,8 @@ describe("Compat - isCodecSupported", () => { }, }; }); - const isCodecSupported = jest.requireActual("../is_codec_supported").default; - expect(isCodecSupported("foo")).toEqual(false); - expect(isCodecSupported("")).toEqual(false); + const isCodecSupported = await vi.importActual("../is_codec_supported"); + expect(isCodecSupported.default("foo")).toEqual(false); + expect(isCodecSupported.default("")).toEqual(false); }); }); diff --git a/src/compat/__tests__/is_seeking_approximate.test.ts b/src/compat/__tests__/is_seeking_approximate.test.ts index 6f5a3c48db3..77ad7dd1a23 100644 --- a/src/compat/__tests__/is_seeking_approximate.test.ts +++ b/src/compat/__tests__/is_seeking_approximate.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -20,29 +6,30 @@ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-return */ +/* eslint-disable @typescript-eslint/no-explicit-any */ describe("isSeekingApproximate", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should be true if on Tizen", () => { - jest.mock("../browser_detection", () => { + it("should be true if on Tizen", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isTizen: true }; }); - const shouldAppendBufferAfterPadding = jest.requireActual( + const shouldAppendBufferAfterPadding = await vi.importActual( "../is_seeking_approximate", - ).default; - expect(shouldAppendBufferAfterPadding).toBe(true); + ) as any; + expect(shouldAppendBufferAfterPadding.default).toBe(true); }); - it("should be false if not on tizen", () => { - jest.mock("../browser_detection", () => { + it("should be false if not on tizen", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isTizen: false }; }); - const shouldAppendBufferAfterPadding = jest.requireActual( + const shouldAppendBufferAfterPadding = await vi.importActual( "../is_seeking_approximate", - ).default; - expect(shouldAppendBufferAfterPadding).toBe(false); + ) as any; + expect(shouldAppendBufferAfterPadding.default).toBe(false); }); }); diff --git a/src/compat/__tests__/is_vtt_cue.test.ts b/src/compat/__tests__/is_vtt_cue.test.ts index 936754f123f..acd57f6a78f 100644 --- a/src/compat/__tests__/is_vtt_cue.test.ts +++ b/src/compat/__tests__/is_vtt_cue.test.ts @@ -1,19 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; import globalScope from "../../utils/global_scope"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ @@ -39,16 +24,16 @@ describe("Compat - isVTTCue", () => { } const gs = globalScope as IFakeWindow; - it("should return true if the given cue is an instance of a vtt cue", () => { + it("should return true if the given cue is an instance of a vtt cue", async () => { const originalVTTCue = globalScope.VTTCue; gs.VTTCue = MockVTTCue; const cue = new VTTCue(0, 10, ""); - const isVTTCue = jest.requireActual("../is_vtt_cue").default; - expect(isVTTCue(cue)).toEqual(true); + const isVTTCue = await vi.importActual("../is_vtt_cue"); + expect(isVTTCue.default(cue)).toEqual(true); globalScope.VTTCue = originalVTTCue; }); - it("should return false if the given cue is not an instance of a vtt cue", () => { + it("should return false if the given cue is not an instance of a vtt cue", async () => { const originalVTTCue = globalScope.VTTCue; gs.VTTCue = MockVTTCue; const cue = { @@ -56,18 +41,18 @@ describe("Compat - isVTTCue", () => { endTime: 10, text: "toto", }; - const isVTTCue = jest.requireActual("../is_vtt_cue").default; - expect(isVTTCue(cue)).toEqual(false); + const isVTTCue = await vi.importActual("../is_vtt_cue"); + expect(isVTTCue.default(cue)).toEqual(false); globalScope.VTTCue = originalVTTCue; }); - it("should return false in any case if the global scope does not define a VTTCue", () => { + it("should return false in any case if the global scope does not define a VTTCue", async () => { const originalVTTCue = globalScope.VTTCue; gs.VTTCue = MockVTTCue; const cue = new VTTCue(0, 10, ""); delete gs.VTTCue; - const isVTTCue = jest.requireActual("../is_vtt_cue").default; - expect(isVTTCue(cue)).toEqual(false); + const isVTTCue = await vi.importActual("../is_vtt_cue"); + expect(isVTTCue.default(cue)).toEqual(false); globalScope.VTTCue = originalVTTCue; }); }); diff --git a/src/compat/__tests__/make_vtt_cue.test.ts b/src/compat/__tests__/make_vtt_cue.test.ts index 24f52a30978..7aebfec2a4c 100644 --- a/src/compat/__tests__/make_vtt_cue.test.ts +++ b/src/compat/__tests__/make_vtt_cue.test.ts @@ -1,19 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; import globalScope from "../../utils/global_scope"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ @@ -43,24 +28,24 @@ describe("Compat - makeVTTCue", () => { const ogVTTuCue = gs.VTTCue; const ogTextTrackCue = gs.TextTrackCue; beforeEach(() => { - jest.resetModules(); + vi.resetModules(); gs.VTTCue = ogVTTuCue; gs.TextTrackCue = ogTextTrackCue; }); - it("should throw if nor VTTCue nor TextTrackCue is available", () => { - const mockLog = { warn: jest.fn() }; + it("should throw if nor VTTCue nor TextTrackCue is available", async () => { + const mockLog = { warn: vi.fn() }; gs.VTTCue = undefined; gs.TextTrackCue = undefined; - jest.mock("../../log", () => ({ + vi.doMock("../../log", () => ({ __esModule: true as const, default: mockLog, })); - const makeCue = jest.requireActual("../make_vtt_cue").default; + const makeCue = await vi.importActual("../make_vtt_cue"); let result; let error; try { - result = makeCue(5, 10, "toto"); + result = makeCue.default(5, 10, "toto"); } catch (e: unknown) { error = e; } @@ -70,29 +55,29 @@ describe("Compat - makeVTTCue", () => { expect(mockLog.warn).not.toHaveBeenCalled(); }); - it("should warn and not create anything if start time is after end time", () => { - const mockLog = { warn: jest.fn() }; + it("should warn and not create anything if start time is after end time", async () => { + const mockLog = { warn: vi.fn() }; gs.VTTCue = MockVTTCue; - jest.mock("../../log", () => ({ + vi.doMock("../../log", () => ({ __esModule: true as const, default: mockLog, })); - const makeCue = jest.requireActual("../make_vtt_cue").default; - const result = makeCue(12, 10, "toto"); + const makeCue = await vi.importActual("../make_vtt_cue"); + const result = makeCue.default(12, 10, "toto"); expect(result).toBeNull(); expect(mockLog.warn).toHaveBeenCalledTimes(1); expect(mockLog.warn).toHaveBeenCalledWith("Compat: Invalid cue times: 12 - 10"); }); - it("should create a new VTT Cue in other cases", () => { - const mockLog = { warn: jest.fn() }; + it("should create a new VTT Cue in other cases", async () => { + const mockLog = { warn: vi.fn() }; gs.VTTCue = MockVTTCue; - jest.mock("../../log", () => ({ + vi.doMock("../../log", () => ({ __esModule: true as const, default: mockLog, })); - const makeCue = jest.requireActual("../make_vtt_cue").default; - const result = makeCue(10, 12, "toto"); + const makeCue = await vi.importActual("../make_vtt_cue"); + const result = makeCue.default(10, 12, "toto"); expect(result).toEqual(new MockVTTCue(10, 12, "toto")); expect(mockLog.warn).not.toHaveBeenCalled(); }); diff --git a/src/compat/__tests__/patch_webkit_source_buffer.test.ts b/src/compat/__tests__/patch_webkit_source_buffer.test.ts index a2ab8df0166..8b38a4a5201 100644 --- a/src/compat/__tests__/patch_webkit_source_buffer.test.ts +++ b/src/compat/__tests__/patch_webkit_source_buffer.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -50,40 +36,13 @@ describe("compat - parseWebkitSourceBuffer", () => { gs.WebKitSourceBuffer = origWebKitSourceBuffer; }); - it("should assign EventEmitter funcs to WebKitSourceBuffer", () => { - const origWebKitSourceBuffer = gs.WebKitSourceBuffer; - const mockWebKitSourceBuffer = { - prototype: {}, - }; - const mockEventEmitter = { - prototype: { - addEventListener: () => null, - test1: () => null, - test2: () => null, - }, - }; - const origEventEmitter = EventEmitter; - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (EventEmitter as any) = mockEventEmitter; - gs.WebKitSourceBuffer = mockWebKitSourceBuffer; - patchWebkitSourceBuffer(); - const protoWebkitSourceBuffer = gs.WebKitSourceBuffer.prototype; - expect(protoWebkitSourceBuffer.test1).not.toBeUndefined(); - expect(protoWebkitSourceBuffer.test2).not.toBeUndefined(); - expect(protoWebkitSourceBuffer.addEventListener).not.toBeUndefined(); - gs.WebKitSourceBuffer = origWebKitSourceBuffer; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (EventEmitter as any) = origEventEmitter; - }); - it("(appendBuffer) should update buffer", () => { const origWebKitSourceBuffer = gs.WebKitSourceBuffer; const mockWebKitSourceBuffer = { prototype: {} }; - const mockTrigger = jest.fn(() => ({})); - const _mockEmitUpdate = jest.fn(() => ({})); - const mockAppend = jest.fn(() => ({})); + const mockTrigger = vi.fn(() => ({})); + const _mockEmitUpdate = vi.fn(() => ({})); + const mockAppend = vi.fn(() => ({})); gs.WebKitSourceBuffer = mockWebKitSourceBuffer; patchWebkitSourceBuffer(); @@ -106,8 +65,8 @@ describe("compat - parseWebkitSourceBuffer", () => { const origWebKitSourceBuffer = gs.WebKitSourceBuffer; const mockWebKitSourceBuffer = { prototype: {} }; - const mockTrigger = jest.fn(() => ({})); - const _mockEmitUpdate = jest.fn(() => ({})); + const mockTrigger = vi.fn(() => ({})); + const _mockEmitUpdate = vi.fn(() => ({})); gs.WebKitSourceBuffer = mockWebKitSourceBuffer; patchWebkitSourceBuffer(); diff --git a/src/compat/__tests__/remove_cue.test.ts b/src/compat/__tests__/remove_cue.test.ts index 77d0c11fa35..af7ba702d5f 100644 --- a/src/compat/__tests__/remove_cue.test.ts +++ b/src/compat/__tests__/remove_cue.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -25,13 +11,13 @@ import arrayFindIndex from "../../utils/array_find_index"; describe("compat - removeCue", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should remove cue from track if not on firefox", () => { + it("should remove cue from track if not on firefox", async () => { const fakeTrackCues = [{ id: "1" }]; - const mockRemoveCue = jest.fn((cue: { id: string }) => { + const mockRemoveCue = vi.fn((cue: { id: string }) => { const idx = arrayFindIndex(fakeTrackCues, (c) => { return c.id === cue.id; }); @@ -39,8 +25,8 @@ describe("compat - removeCue", () => { fakeTrackCues.splice(idx, 1); } }); - const mockGetMode = jest.fn(() => "showing"); - const mockSetMode = jest.fn(() => null); + const mockGetMode = vi.fn(() => "showing"); + const mockSetMode = vi.fn(() => null); const fakeTrack = { get mode() { @@ -54,12 +40,12 @@ describe("compat - removeCue", () => { removeCue: mockRemoveCue, }; - jest.mock("../browser_detection", () => ({ + vi.doMock("../browser_detection", () => ({ __esModule: true as const, isFirefox: false, })); - const removeCue = jest.requireActual("../remove_cue").default; + const removeCue = (await vi.importActual("../remove_cue")).default; removeCue(fakeTrack, { id: "1" }); expect(fakeTrack.cues.length).toBe(0); @@ -70,12 +56,12 @@ describe("compat - removeCue", () => { expect(mockRemoveCue).toHaveBeenLastCalledWith({ id: "1" }); }); - it("should remove cue from track if on firefox and is active cue", () => { + it("should remove cue from track if on firefox and is active cue", async () => { const fakeCue = { id: "1" }; const fakeTrackCues = [fakeCue]; let fakeMode = "showing"; - const mockRemoveCue = jest.fn((cue: { id: string }) => { + const mockRemoveCue = vi.fn((cue: { id: string }) => { const idx = arrayFindIndex(fakeTrackCues, (c) => { return c.id === cue.id; }); @@ -83,10 +69,10 @@ describe("compat - removeCue", () => { fakeTrackCues.splice(idx, 1); } }); - const mockGetMode = jest.fn(() => { + const mockGetMode = vi.fn(() => { return fakeMode; }); - const mockSetMode = jest.fn((newMode: string) => { + const mockSetMode = vi.fn((newMode: string) => { fakeMode = newMode; }); @@ -102,12 +88,12 @@ describe("compat - removeCue", () => { removeCue: mockRemoveCue, }; - jest.mock("../browser_detection", () => ({ + vi.doMock("../browser_detection", () => ({ __esModule: true as const, isFirefox: true, })); - const removeCue = jest.requireActual("../remove_cue").default; + const removeCue = (await vi.importActual("../remove_cue")).default; removeCue(fakeTrack, fakeCue); expect(fakeTrack.cues.length).toBe(0); @@ -118,12 +104,12 @@ describe("compat - removeCue", () => { expect(mockRemoveCue).toHaveBeenLastCalledWith(fakeCue); }); - it("should remove cue from track if on firefox and is not active cue", () => { + it("should remove cue from track if on firefox and is not active cue", async () => { const fakeCue = { id: "1" }; const fakeTrackCue = [fakeCue]; let fakeMode = "showing"; - const mockRemoveCue = jest.fn((cue: { id: string }) => { + const mockRemoveCue = vi.fn((cue: { id: string }) => { const idx = arrayFindIndex(fakeTrackCue, (c) => { return c.id === cue.id; }); @@ -131,10 +117,10 @@ describe("compat - removeCue", () => { fakeTrackCue.splice(idx, 1); } }); - const mockGetMode = jest.fn(() => { + const mockGetMode = vi.fn(() => { return fakeMode; }); - const mockSetMode = jest.fn((newMode: string) => { + const mockSetMode = vi.fn((newMode: string) => { fakeMode = newMode; }); @@ -150,12 +136,12 @@ describe("compat - removeCue", () => { removeCue: mockRemoveCue, }; - jest.mock("../browser_detection", () => ({ + vi.doMock("../browser_detection", () => ({ __esModule: true as const, isFirefox: true, })); - const removeCue = jest.requireActual("../remove_cue").default; + const removeCue = (await vi.importActual("../remove_cue")).default; removeCue(fakeTrack, fakeCue); expect(fakeTrack.cues.length).toBe(0); @@ -166,19 +152,19 @@ describe("compat - removeCue", () => { expect(mockRemoveCue).toHaveBeenLastCalledWith(fakeCue); }); - it("should log if removeCue throws if on firefox and is active cue", () => { + it("should log if removeCue throws if on firefox and is active cue", async () => { const fakeCue = { id: "1" }; const fakeTrackCues = [fakeCue]; - const mockRemoveCue = jest.fn(() => { + const mockRemoveCue = vi.fn(() => { throw new Error(); }); - const mockLog = jest.fn((message) => message); + const mockLog = vi.fn((message) => message); - jest.mock("../browser_detection", () => ({ + vi.doMock("../browser_detection", () => ({ __esModule: true as const, isFirefox: true, })); - jest.mock("../../log", () => ({ + vi.doMock("../../log", () => ({ __esModule: true as const, default: { warn: mockLog, @@ -192,7 +178,7 @@ describe("compat - removeCue", () => { removeCue: mockRemoveCue, }; - const removeCue = jest.requireActual("../remove_cue").default; + const removeCue = (await vi.importActual("../remove_cue")).default; removeCue(fakeTrack, fakeCue); expect(fakeTrack.cues.length).toBe(1); @@ -203,17 +189,17 @@ describe("compat - removeCue", () => { expect(mockRemoveCue).toHaveBeenLastCalledWith(fakeCue); }); - it("should log if removeCue throws if not on firefox", () => { - const mockLog = jest.fn((message) => message); - const mockRemoveCue = jest.fn(() => { + it("should log if removeCue throws if not on firefox", async () => { + const mockLog = vi.fn((message) => message); + const mockRemoveCue = vi.fn(() => { throw new Error(); }); - jest.mock("../browser_detection", () => ({ + vi.doMock("../browser_detection", () => ({ __esModule: true as const, isFirefox: false, })); - jest.mock("../../log", () => ({ + vi.doMock("../../log", () => ({ __esModule: true as const, default: { warn: mockLog, @@ -226,7 +212,7 @@ describe("compat - removeCue", () => { removeCue: mockRemoveCue, }; - const removeCue = jest.requireActual("../remove_cue").default; + const removeCue = (await vi.importActual("../remove_cue")).default; removeCue(fakeTrack, { id: "1" }); expect(fakeTrack.cues.length).toBe(1); diff --git a/src/compat/__tests__/should_favour_custom_safari_EME.test.ts b/src/compat/__tests__/should_favour_custom_safari_EME.test.ts index c941b5bec3e..be8facd1e36 100644 --- a/src/compat/__tests__/should_favour_custom_safari_EME.test.ts +++ b/src/compat/__tests__/should_favour_custom_safari_EME.test.ts @@ -1,19 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; import globalScope from "../../utils/global_scope"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ @@ -32,58 +17,58 @@ describe("compat - shouldFavourSafariMediaKeys", () => { const originalWebKitMediaKeys = gs.WebKitMediaKeys; beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); afterEach(() => { gs.WebKitMediaKeys = originalWebKitMediaKeys; }); - it("should return false if we are not on Safari", () => { - jest.mock("../browser_detection", () => { + it("should return false if we are not on Safari", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSafariDesktop: false, isSafariMobile: false, }; }); - const shouldFavourCustomSafariEME = jest.requireActual( + const shouldFavourCustomSafariEME = await vi.importActual( "../should_favour_custom_safari_EME", ); expect(shouldFavourCustomSafariEME.default()).toBe(false); }); - it("should return false if we are on Safari Desktop but WekitMediaKeys is not available", () => { + it("should return false if we are on Safari Desktop but WekitMediaKeys is not available", async () => { gs.WebKitMediaKeys = undefined; - jest.mock("../browser_detection", () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSafariDesktop: true, isSafariMobile: false, }; }); - const shouldFavourCustomSafariEME = jest.requireActual( + const shouldFavourCustomSafariEME = await vi.importActual( "../should_favour_custom_safari_EME", ); expect(shouldFavourCustomSafariEME.default()).toBe(false); }); - it("should return false if we are on Safari Mobile but WekitMediaKeys is not available", () => { + it("should return false if we are on Safari Mobile but WekitMediaKeys is not available", async () => { gs.WebKitMediaKeys = undefined; - jest.mock("../browser_detection", () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSafariDesktop: false, isSafariMobile: true, }; }); - const shouldFavourCustomSafariEME = jest.requireActual( + const shouldFavourCustomSafariEME = await vi.importActual( "../should_favour_custom_safari_EME", ); expect(shouldFavourCustomSafariEME.default()).toBe(false); }); - it("should return true if we are on Safari Desktop and a WebKitMediaKeys implementation is available", () => { + it("should return true if we are on Safari Desktop and a WebKitMediaKeys implementation is available", async () => { gs.WebKitMediaKeys = { isTypeSupported: () => ({}), prototype: { @@ -94,20 +79,20 @@ describe("compat - shouldFavourSafariMediaKeys", () => { webkitSetMediaKeys: () => Record; }; proto.webkitSetMediaKeys = () => ({}); - jest.mock("../browser_detection", () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSafariDesktop: true, isSafariMobile: false, }; }); - const shouldFavourCustomSafariEME = jest.requireActual( + const shouldFavourCustomSafariEME = await vi.importActual( "../should_favour_custom_safari_EME", ); expect(shouldFavourCustomSafariEME.default()).toBe(true); }); - it("should return true if we are on Safari Mobile and a WebKitMediaKeys implementation is available", () => { + it("should return true if we are on Safari Mobile and a WebKitMediaKeys implementation is available", async () => { gs.WebKitMediaKeys = { isTypeSupported: () => ({}), prototype: { @@ -118,14 +103,14 @@ describe("compat - shouldFavourSafariMediaKeys", () => { webkitSetMediaKeys: () => Record; }; proto.webkitSetMediaKeys = () => ({}); - jest.mock("../browser_detection", () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSafariDesktop: false, isSafariMobile: true, }; }); - const shouldFavourCustomSafariEME = jest.requireActual( + const shouldFavourCustomSafariEME = await vi.importActual( "../should_favour_custom_safari_EME", ); expect(shouldFavourCustomSafariEME.default()).toBe(true); diff --git a/src/compat/__tests__/should_reload_media_source_on_decipherability_update.test.ts b/src/compat/__tests__/should_reload_media_source_on_decipherability_update.test.ts index d98ad6a5bdf..47237209651 100644 --- a/src/compat/__tests__/should_reload_media_source_on_decipherability_update.test.ts +++ b/src/compat/__tests__/should_reload_media_source_on_decipherability_update.test.ts @@ -1,19 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; import shouldReloadMediaSourceOnDecipherabilityUpdate from "../should_reload_media_source_on_decipherability_update"; describe("Compat - shouldReloadMediaSourceOnDecipherabilityUpdate", () => { diff --git a/src/compat/__tests__/should_renew_media_key_system_access.test.ts b/src/compat/__tests__/should_renew_media_key_system_access.test.ts index 7ad1f73858b..67a0ac89f1a 100644 --- a/src/compat/__tests__/should_renew_media_key_system_access.test.ts +++ b/src/compat/__tests__/should_renew_media_key_system_access.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -23,35 +9,35 @@ describe("compat - shouldRenewMediaKeySystemAccess", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should return false if we are not on IE11", () => { - jest.mock("../browser_detection", () => { + it("should return false if we are not on IE11", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isIE11: false, }; }); - const shouldRenewMediaKeySystemAccess = jest.requireActual( + const shouldRenewMediaKeySystemAccess = await vi.importActual( "../should_renew_media_key_system_access", ); expect(shouldRenewMediaKeySystemAccess.default()).toBe(false); }); - it("should return true if we are on IE11", () => { - jest.mock("../browser_detection", () => { + it("should return true if we are on IE11", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isIE11: true, }; }); - const shouldRenewMediaKeySystemAccess = jest.requireActual( + const shouldRenewMediaKeySystemAccess = await vi.importActual( "../should_renew_media_key_system_access", ); expect(shouldRenewMediaKeySystemAccess.default()).toBe(true); }); beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); }); diff --git a/src/compat/__tests__/should_unset_media_keys.test.ts b/src/compat/__tests__/should_unset_media_keys.test.ts index 74449163893..838b77fae4d 100644 --- a/src/compat/__tests__/should_unset_media_keys.test.ts +++ b/src/compat/__tests__/should_unset_media_keys.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -23,31 +9,28 @@ describe("compat - shouldUnsetMediaKeys", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should return false if we are not on IE11", () => { - jest.mock("../browser_detection", () => { + it("should return false if we are not on IE11", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isIE11: false, }; }); - const shouldUnsetMediaKeys = jest.requireActual("../should_unset_media_keys"); + const shouldUnsetMediaKeys = await vi.importActual("../should_unset_media_keys"); expect(shouldUnsetMediaKeys.default()).toBe(false); }); - it("should return true if we are on IE11", () => { - jest.mock("../browser_detection", () => { + it("should return true if we are on IE11", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isIE11: true, }; }); - const shouldUnsetMediaKeys = jest.requireActual("../should_unset_media_keys"); + const shouldUnsetMediaKeys = await vi.importActual("../should_unset_media_keys"); expect(shouldUnsetMediaKeys.default()).toBe(true); }); - beforeEach(() => { - jest.resetModules(); - }); }); diff --git a/src/compat/__tests__/should_validate_metadata.test.ts b/src/compat/__tests__/should_validate_metadata.test.ts index 1630e5e30b0..ffeccde1122 100644 --- a/src/compat/__tests__/should_validate_metadata.test.ts +++ b/src/compat/__tests__/should_validate_metadata.test.ts @@ -1,18 +1,4 @@ -/** - * Copyright 2015 CANAL+ Group - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ @@ -23,31 +9,31 @@ describe("compat - shouldValidateMetadata", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should return false if we are not on the Samsung browser", () => { - jest.mock("../browser_detection", () => { + it("should return false if we are not on the Samsung browser", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSamsungBrowser: false, }; }); - const shouldValidateMetadata = jest.requireActual("../should_validate_metadata"); + const shouldValidateMetadata = await vi.importActual("../should_validate_metadata"); expect(shouldValidateMetadata.default()).toBe(false); }); - it("should return true if we are on the Samsung browser", () => { - jest.mock("../browser_detection", () => { + it("should return true if we are on the Samsung browser", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSamsungBrowser: true, }; }); - const shouldValidateMetadata = jest.requireActual("../should_validate_metadata"); + const shouldValidateMetadata = await vi.importActual("../should_validate_metadata"); expect(shouldValidateMetadata.default()).toBe(true); }); beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); }); diff --git a/src/compat/__tests__/should_wait_for_data_before_loaded.test.ts b/src/compat/__tests__/should_wait_for_data_before_loaded.test.ts index 62e3187ded7..fb0a07dd1fb 100644 --- a/src/compat/__tests__/should_wait_for_data_before_loaded.test.ts +++ b/src/compat/__tests__/should_wait_for_data_before_loaded.test.ts @@ -1,3 +1,4 @@ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /** * Copyright 2015 CANAL+ Group * @@ -23,63 +24,63 @@ describe("compat - shouldWaitForDataBeforeLoaded", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should return true if we are not on Safari browser nor in directfile mode", () => { - jest.mock("../browser_detection", () => { + it("should return true if we are not on Safari browser nor in directfile mode", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSafariMobile: false, }; }); - const shouldWaitForDataBeforeLoaded = jest.requireActual( + const shouldWaitForDataBeforeLoaded = await vi.importActual( "../should_wait_for_data_before_loaded", ); expect(shouldWaitForDataBeforeLoaded.default(false)).toBe(true); }); - it("should return true if we are not on Safari browser but in directfile mode", () => { - jest.mock("../browser_detection", () => { + it("should return true if we are not on Safari browser but in directfile mode", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSafariMobile: false, }; }); - const shouldWaitForDataBeforeLoaded = jest.requireActual( + const shouldWaitForDataBeforeLoaded = await vi.importActual( "../should_wait_for_data_before_loaded", ); expect(shouldWaitForDataBeforeLoaded.default(true)).toBe(true); }); - it("should return true if we are on the Safari browser but not in directfile mode", () => { - jest.mock("../browser_detection", () => { + it("should return true if we are on the Safari browser but not in directfile mode", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSafariMobile: true, }; }); - const shouldWaitForDataBeforeLoaded = jest.requireActual( + const shouldWaitForDataBeforeLoaded = await vi.importActual( "../should_wait_for_data_before_loaded", ); expect(shouldWaitForDataBeforeLoaded.default(false)).toBe(true); }); // eslint-disable-next-line max-len - it("should return false if we are on the Safari browser and in directfile mode", () => { - jest.mock("../browser_detection", () => { + it("should return false if we are on the Safari browser and in directfile mode", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isSafariMobile: true, }; }); - const shouldWaitForDataBeforeLoaded = jest.requireActual( + const shouldWaitForDataBeforeLoaded = await vi.importActual( "../should_wait_for_data_before_loaded", ); expect(shouldWaitForDataBeforeLoaded.default(true)).toBe(false); }); beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); }); diff --git a/src/compat/__tests__/should_wait_for_have_enough_data.test.ts b/src/compat/__tests__/should_wait_for_have_enough_data.test.ts index 39b63e005bc..2cb531e01fd 100644 --- a/src/compat/__tests__/should_wait_for_have_enough_data.test.ts +++ b/src/compat/__tests__/should_wait_for_have_enough_data.test.ts @@ -1,3 +1,4 @@ +import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-var-requires */ @@ -7,30 +8,30 @@ describe("compat - shouldWaitForHaveEnoughData", () => { beforeEach(() => { - jest.resetModules(); + vi.resetModules(); }); - it("should return false if we are not on the Playstation 5", () => { - jest.mock("../browser_detection", () => { + it("should return false if we are not on the Playstation 5", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isPlayStation5: false, }; }); - const shouldWaitForHaveEnoughData = jest.requireActual( + const shouldWaitForHaveEnoughData = await vi.importActual( "../should_wait_for_have_enough_data", ); expect(shouldWaitForHaveEnoughData.default()).toBe(false); }); - it("should return true if we are on the Playstation 5", () => { - jest.mock("../browser_detection", () => { + it("should return true if we are on the Playstation 5", async () => { + vi.doMock("../browser_detection", () => { return { __esModule: true as const, isPlayStation5: true, }; }); - const shouldWaitForHaveEnoughData = jest.requireActual( + const shouldWaitForHaveEnoughData = await vi.importActual( "../should_wait_for_have_enough_data", ); expect(shouldWaitForHaveEnoughData.default()).toBe(true); diff --git a/vitest.config.mjs b/vitest.config.mjs index 0ae80952d98..37f94aaaa1e 100644 --- a/vitest.config.mjs +++ b/vitest.config.mjs @@ -20,7 +20,6 @@ function vitePluginArraybuffer() { export default defineConfig({ plugins: [vitePluginArraybuffer()], - // assetsInclude: ["**/*.bif?arraybuffer"], define: { // global variables __TEST_CONTENT_SERVER__: { @@ -37,8 +36,10 @@ export default defineConfig({ }, }, test: { + watch: false, globals: false, - include: ["**/*.test.[jt]s?(x)"], + reporters: "dot", + include: ["tests/**/*.test.[jt]s?(x)"], globalSetup: "tests/integration/globalSetup.js", browser: { enabled: true, @@ -57,5 +58,13 @@ export default defineConfig({ }, }, }, + + // Ensure parallelization is disabled, as it led to some test errors for + // some reasons + // TODO enable it + fileParallelism: false, + minWorkers: 1, + maxWorkers: 1, + maxConcurrency: 1, }, }); diff --git a/vitest.config.unit.mjs b/vitest.config.unit.mjs new file mode 100644 index 00000000000..fa9cec5d7bb --- /dev/null +++ b/vitest.config.unit.mjs @@ -0,0 +1,27 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + define: { + // global variables + __TEST_CONTENT_SERVER__: { + URL: "127.0.0.1", + PORT: 3000, + }, + __ENVIRONMENT__: { + PRODUCTION: 0, + DEV: 1, + CURRENT_ENV: 1, + }, + __LOGGER_LEVEL__: { + CURRENT_LEVEL: '"NONE"', + }, + }, + test: { + watch: false, + globals: false, + reporters: "dot", + include: ["src/compat/__tests__/*.test.ts"], + environment: "jsdom", + fileParallelism: true, + }, +});