migrate generation tests
This commit is contained in:
parent
d2c99e51bc
commit
033577d99f
3 changed files with 35 additions and 27 deletions
|
|
@ -1,4 +1,12 @@
|
|||
import { jest } from "@jest/globals";
|
||||
import {
|
||||
afterEach,
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
Mock,
|
||||
test,
|
||||
vi,
|
||||
} from "vitest";
|
||||
import { GeneratorReuseManager } from "./GeneratorReuseManager";
|
||||
|
||||
function createMockGenerator(
|
||||
|
|
@ -14,7 +22,7 @@ function createMockGenerator(
|
|||
}
|
||||
}
|
||||
};
|
||||
const newGenerator = jest
|
||||
const newGenerator = vi
|
||||
.fn<() => AsyncGenerator<string>>()
|
||||
.mockReturnValue(mockGenerator());
|
||||
|
||||
|
|
@ -23,15 +31,15 @@ function createMockGenerator(
|
|||
|
||||
describe("GeneratorReuseManager", () => {
|
||||
let reuseManager: GeneratorReuseManager;
|
||||
let onErrorMock: jest.Mock;
|
||||
let onErrorMock: Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
onErrorMock = jest.fn();
|
||||
onErrorMock = vi.fn();
|
||||
reuseManager = new GeneratorReuseManager(onErrorMock);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
test("creates new generator when there is no current generator", async () => {
|
||||
|
|
@ -169,7 +177,7 @@ describe("GeneratorReuseManager", () => {
|
|||
const mockGenerator = async function* () {
|
||||
throw error;
|
||||
};
|
||||
const newGenerator = jest
|
||||
const newGenerator = vi
|
||||
.fn<() => AsyncGenerator<string>>()
|
||||
.mockReturnValue(mockGenerator());
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { jest } from "@jest/globals";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { ListenableGenerator } from "./ListenableGenerator";
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ describe("ListenableGenerator", () => {
|
|||
it("should yield values from the source generator via tee()", async () => {
|
||||
const values = [1, 2, 3];
|
||||
const source = asyncGenerator(values);
|
||||
const onError = jest.fn();
|
||||
const onError = vi.fn();
|
||||
|
||||
const lg = new ListenableGenerator<number>(
|
||||
source,
|
||||
|
|
@ -36,7 +36,7 @@ describe("ListenableGenerator", () => {
|
|||
it("should allow listeners to receive values", async () => {
|
||||
const values = [1, 2, 3];
|
||||
const source = asyncGenerator(values, 10); // Introduce delay to simulate async behavior
|
||||
const onError = jest.fn();
|
||||
const onError = vi.fn();
|
||||
|
||||
const lg = new ListenableGenerator<number>(
|
||||
source,
|
||||
|
|
@ -44,7 +44,7 @@ describe("ListenableGenerator", () => {
|
|||
new AbortController(),
|
||||
);
|
||||
|
||||
const listener = jest.fn();
|
||||
const listener = vi.fn();
|
||||
|
||||
// Add listener after some delay to simulate late subscription
|
||||
setTimeout(() => {
|
||||
|
|
@ -64,7 +64,7 @@ describe("ListenableGenerator", () => {
|
|||
it("should buffer values for listeners added after some values have been yielded", async () => {
|
||||
const values = [1, 2, 3];
|
||||
const source = asyncGenerator(values, 10);
|
||||
const onError = jest.fn();
|
||||
const onError = vi.fn();
|
||||
|
||||
const lg = new ListenableGenerator<number>(
|
||||
source,
|
||||
|
|
@ -72,7 +72,7 @@ describe("ListenableGenerator", () => {
|
|||
new AbortController(),
|
||||
);
|
||||
|
||||
const initialListener = jest.fn();
|
||||
const initialListener = vi.fn();
|
||||
|
||||
lg.listen(initialListener);
|
||||
|
||||
|
|
@ -80,7 +80,7 @@ describe("ListenableGenerator", () => {
|
|||
await new Promise((resolve) => setTimeout(resolve, 15));
|
||||
|
||||
// Add a second listener
|
||||
const newListener = jest.fn();
|
||||
const newListener = vi.fn();
|
||||
lg.listen(newListener);
|
||||
|
||||
// Wait for generator to finish
|
||||
|
|
@ -98,7 +98,7 @@ describe("ListenableGenerator", () => {
|
|||
it("should handle cancellation", async () => {
|
||||
const values = [1, 2, 3, 4, 5];
|
||||
const source = asyncGenerator(values, 10);
|
||||
const onError = jest.fn();
|
||||
const onError = vi.fn();
|
||||
|
||||
const lg = new ListenableGenerator<number>(
|
||||
source,
|
||||
|
|
@ -131,7 +131,7 @@ describe("ListenableGenerator", () => {
|
|||
}
|
||||
|
||||
const source = errorGenerator();
|
||||
const onError = jest.fn();
|
||||
const onError = vi.fn();
|
||||
|
||||
const lg = new ListenableGenerator<number>(
|
||||
source,
|
||||
|
|
@ -152,7 +152,7 @@ describe("ListenableGenerator", () => {
|
|||
it("should notify listeners when the generator ends", async () => {
|
||||
const values = [1, 2, 3];
|
||||
const source = asyncGenerator(values);
|
||||
const onError = jest.fn();
|
||||
const onError = vi.fn();
|
||||
|
||||
const lg = new ListenableGenerator<number>(
|
||||
source,
|
||||
|
|
@ -160,7 +160,7 @@ describe("ListenableGenerator", () => {
|
|||
new AbortController(),
|
||||
);
|
||||
|
||||
const listener = jest.fn();
|
||||
const listener = vi.fn();
|
||||
lg.listen(listener);
|
||||
|
||||
// Wait for the generator to finish
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
import { jest } from "@jest/globals";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { stopAfterMaxProcessingTime } from "./utils";
|
||||
|
||||
describe("stopAfterMaxProcessingTime", () => {
|
||||
beforeEach(() => {
|
||||
jest.useFakeTimers();
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.useRealTimers();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
async function* createMockStream(chunks: string[]): AsyncGenerator<string> {
|
||||
|
|
@ -28,7 +28,7 @@ describe("stopAfterMaxProcessingTime", () => {
|
|||
|
||||
it("should yield all chunks when maxTimeMs is not reached", async () => {
|
||||
const mockStream = createMockStream(["Hello", " world", "!"]);
|
||||
const fullStop = jest.fn();
|
||||
const fullStop = vi.fn();
|
||||
const result = stopAfterMaxProcessingTime(mockStream, 1000, fullStop);
|
||||
|
||||
const output = await streamToString(result);
|
||||
|
|
@ -41,7 +41,7 @@ describe("stopAfterMaxProcessingTime", () => {
|
|||
// Mock implementation of Date.now
|
||||
let currentTime = 0;
|
||||
const originalDateNow = Date.now;
|
||||
Date.now = jest.fn(() => currentTime);
|
||||
Date.now = vi.fn(() => currentTime);
|
||||
|
||||
// Create a generator that we can control
|
||||
async function* controlledGenerator(): AsyncGenerator<string> {
|
||||
|
|
@ -54,7 +54,7 @@ describe("stopAfterMaxProcessingTime", () => {
|
|||
}
|
||||
}
|
||||
|
||||
const fullStop = jest.fn();
|
||||
const fullStop = vi.fn();
|
||||
const maxTimeMs = 500;
|
||||
|
||||
const transformedGenerator = stopAfterMaxProcessingTime(
|
||||
|
|
@ -84,10 +84,10 @@ describe("stopAfterMaxProcessingTime", () => {
|
|||
it("should check time only periodically based on checkInterval", async () => {
|
||||
const chunks = Array(100).fill("x");
|
||||
const mockStream = createMockStream(chunks);
|
||||
const fullStop = jest.fn();
|
||||
const fullStop = vi.fn();
|
||||
|
||||
// Spy on Date.now to count how many times it's called
|
||||
const dateSpy = jest.spyOn(Date, "now");
|
||||
const dateSpy = vi.spyOn(Date, "now");
|
||||
|
||||
// Stream should complete normally (not hitting the timeout)
|
||||
await streamToString(
|
||||
|
|
@ -105,7 +105,7 @@ describe("stopAfterMaxProcessingTime", () => {
|
|||
|
||||
it("should handle empty stream gracefully", async () => {
|
||||
const mockStream = createMockStream([]);
|
||||
const fullStop = jest.fn();
|
||||
const fullStop = vi.fn();
|
||||
const result = stopAfterMaxProcessingTime(mockStream, 1000, fullStop);
|
||||
|
||||
const output = await streamToString(result);
|
||||
|
|
@ -117,7 +117,7 @@ describe("stopAfterMaxProcessingTime", () => {
|
|||
it("should pass through all chunks if there's no timeout", async () => {
|
||||
const chunks = Array(100).fill("test chunk");
|
||||
const mockStream = createMockStream(chunks);
|
||||
const fullStop = jest.fn();
|
||||
const fullStop = vi.fn();
|
||||
|
||||
// Use undefined as timeout to simulate no timeout
|
||||
const result = stopAfterMaxProcessingTime(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue