|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// This file is required by vitest.config.ts and sets up the Angular testing environment |
| 18 | + |
| 19 | +// Import Zone.js testing utilities first |
| 20 | +import "zone.js"; |
| 21 | +import "zone.js/testing"; |
| 22 | + |
| 23 | +// Set up Zone.js testing environment |
| 24 | +import { TestBed } from "@angular/core/testing"; |
| 25 | + |
| 26 | +// Ensure Zone.js testing environment is properly configured |
| 27 | +beforeEach(() => { |
| 28 | + // Reset Zone.js state before each test |
| 29 | + if (typeof Zone !== "undefined") { |
| 30 | + Zone.current.fork({}).run(() => { |
| 31 | + // Run each test in a fresh zone |
| 32 | + }); |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +// Import Angular testing utilities |
| 37 | +import { getTestBed } from "@angular/core/testing"; |
| 38 | +import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from "@angular/platform-browser-dynamic/testing"; |
| 39 | + |
| 40 | +// Import Vitest utilities |
| 41 | +import { expect, vi, afterEach, beforeEach } from "vitest"; |
| 42 | +import * as matchers from "@testing-library/jest-dom/matchers"; |
| 43 | + |
| 44 | +// Extend Vitest's expect with jest-dom matchers |
| 45 | +expect.extend(matchers); |
| 46 | + |
| 47 | +// Initialize the testing environment with Zone.js support |
| 48 | +if (!TestBed.platform) { |
| 49 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), { |
| 50 | + teardown: { destroyAfterEach: false }, |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +// Reset TestBed after each test to prevent configuration conflicts |
| 55 | +afterEach(() => { |
| 56 | + TestBed.resetTestingModule(); |
| 57 | +}); |
| 58 | + |
| 59 | +// Make Vitest globals available |
| 60 | +declare global { |
| 61 | + const spyOn: typeof vi.spyOn; |
| 62 | + const pending: (reason?: string) => void; |
| 63 | + const jasmine: any; |
| 64 | +} |
| 65 | + |
| 66 | +// Define global test utilities |
| 67 | +globalThis.spyOn = (obj: any, method: string) => { |
| 68 | + const spy = vi.spyOn(obj, method); |
| 69 | + // Add Jasmine-compatible methods |
| 70 | + spy.and = { |
| 71 | + callFake: (fn: Function) => { |
| 72 | + spy.mockImplementation(fn); |
| 73 | + return spy; |
| 74 | + }, |
| 75 | + returnValue: (value: any) => { |
| 76 | + spy.mockReturnValue(value); |
| 77 | + return spy; |
| 78 | + }, |
| 79 | + callThrough: () => { |
| 80 | + spy.mockImplementation((...args: any[]) => obj[method](...args)); |
| 81 | + return spy; |
| 82 | + }, |
| 83 | + }; |
| 84 | + spy.calls = { |
| 85 | + reset: () => spy.mockClear(), |
| 86 | + all: () => spy.mock.calls, |
| 87 | + count: () => spy.mock.calls.length, |
| 88 | + mostRecent: () => spy.mock.calls[spy.mock.calls.length - 1], |
| 89 | + any: () => spy.mock.calls.length > 0, |
| 90 | + }; |
| 91 | + return spy; |
| 92 | +}; |
| 93 | +globalThis.pending = (reason?: string) => { |
| 94 | + throw new Error(`Test pending: ${reason || "No reason provided"}`); |
| 95 | +}; |
| 96 | + |
| 97 | +// Mock Jasmine for compatibility |
| 98 | +globalThis.jasmine = { |
| 99 | + createSpy: (name: string) => { |
| 100 | + const spy = vi.fn(); |
| 101 | + spy.and = { |
| 102 | + returnValue: (value: any) => { |
| 103 | + spy.mockReturnValue(value); |
| 104 | + return spy; |
| 105 | + }, |
| 106 | + callFake: (fn: Function) => { |
| 107 | + spy.mockImplementation(fn); |
| 108 | + return spy; |
| 109 | + }, |
| 110 | + callThrough: () => { |
| 111 | + // For createSpy, there's no original method to call through |
| 112 | + return spy; |
| 113 | + }, |
| 114 | + }; |
| 115 | + spy.calls = { |
| 116 | + reset: () => spy.mockClear(), |
| 117 | + all: () => spy.mock.calls, |
| 118 | + count: () => spy.mock.calls.length, |
| 119 | + mostRecent: () => spy.mock.calls[spy.mock.calls.length - 1], |
| 120 | + any: () => spy.mock.calls.length > 0, |
| 121 | + }; |
| 122 | + return spy; |
| 123 | + }, |
| 124 | + createSpyObj: (name: string, methods: string[], properties?: any) => { |
| 125 | + const obj: any = {}; |
| 126 | + methods.forEach((method) => { |
| 127 | + const spy = vi.fn(); |
| 128 | + // Add Jasmine-compatible methods |
| 129 | + spy.and = { |
| 130 | + returnValue: (value: any) => { |
| 131 | + spy.mockReturnValue(value); |
| 132 | + return spy; |
| 133 | + }, |
| 134 | + callFake: (fn: Function) => { |
| 135 | + spy.mockImplementation(fn); |
| 136 | + return spy; |
| 137 | + }, |
| 138 | + callThrough: () => { |
| 139 | + // For createSpyObj, there's no original method to call through |
| 140 | + return spy; |
| 141 | + }, |
| 142 | + }; |
| 143 | + spy.calls = { |
| 144 | + reset: () => spy.mockClear(), |
| 145 | + all: () => spy.mock.calls, |
| 146 | + count: () => spy.mock.calls.length, |
| 147 | + mostRecent: () => spy.mock.calls[spy.mock.calls.length - 1], |
| 148 | + any: () => spy.mock.calls.length > 0, |
| 149 | + }; |
| 150 | + obj[method] = spy; |
| 151 | + }); |
| 152 | + if (properties) { |
| 153 | + Object.assign(obj, properties); |
| 154 | + } |
| 155 | + return obj; |
| 156 | + }, |
| 157 | +}; |
0 commit comments