Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166import { expect, test, vi } from "vitest";
const myEqual = vi.defineHelper((a: any, b: any) => { expect(a).toEqual(b);});
const myEqualAsync = vi.defineHelper(async (a: any, b: any) => { await new Promise((r) => setTimeout(r, 1)); expect(a).toEqual(b);});
const myEqualSoft = vi.defineHelper((a: any, b: any) => { expect.soft(a).toEqual(b);});
const myEqualSoftAsync = vi.defineHelper(async (a: any, b: any) => { await new Promise((r) => setTimeout(r, 1)); expect.soft(a).toEqual(b);});
test("sync", () => { myEqual("sync", "x");});
test("async", async () => { await myEqualAsync("async", "x");});
test("soft", () => { myEqualSoft("soft", "x");});
test("soft async", async () => { await myEqualSoftAsync("soft async", "x");});
// Nested helpers: outermost marker winsconst innerHelper = vi.defineHelper((a: any, b: any) => { expect(a).toEqual(b);});const outerHelper = vi.defineHelper((a: any, b: any) => { innerHelper(a, b);});
test("nested", () => { outerHelper("nested", "x");});
// Helper that passestest("pass sync", () => { myEqual(1, 1);});
test("pass async", async () => { await myEqualAsync(1, 1);});
// Helper returning valueconst myAdd = vi.defineHelper((a: number, b: number) => { return a + b;});const myAddAsync = vi.defineHelper(async (a: number, b: number) => { await new Promise((r) => setTimeout(r, 1)); return a + b;});
test("return sync", () => { expect(myAdd(1, 2)).toBe(3);});
test("return async", async () => { expect(await myAddAsync(1, 2)).toBe(3);});
// Multiple soft errors in one testtest("multiple soft", () => { myEqualSoft("first", "x"); myEqualSoft("second", "y");});
// Custom error in helperconst throwCustom = vi.defineHelper(() => { throw new Error("custom error from helper");});
test("custom error", () => { throwCustom();});
// non-helper wrapper calling a helper: stack should include the wrapperfunction assertEqualValues(a: any, b: any) { myEqual(a, b);}
test("non-helper wrapper", () => { assertEqualValues("wrapper", "x");});
// printConsoleTrace also hides internal stacksconst myHelperWithLogs = vi.defineHelper(() => { console.error("[test-myHelperWithLogs]");});
test("helper with logs", () => { myHelperWithLogs();});
const helperObject = { prop: 1234, assertEqualProp: vi.defineHelper(function (this: any, actual: any) { expect(actual).toEqual(this.prop); }), assertEqualPropAsync: vi.defineHelper(async function (this: any, actual: any) { await new Promise((r) => setTimeout(r, 1)); expect(actual).toEqual(this.prop); }),}
test("helper with context pass", () => { helperObject.assertEqualProp(1234);});
test("helper with context fail", () => { helperObject.assertEqualProp(4321);});
test("async helper with context pass", async () => { await helperObject.assertEqualPropAsync(1234);});
test("async helper with context fail", async () => { await helperObject.assertEqualPropAsync(4321);});
const checkWithWaitFor = vi.defineHelper(async () => { await vi.waitFor(() => { expect(1).toBe(2); }, { timeout: 20, interval: 10 });});
const checkWithWaitUntil = vi.defineHelper(async () => { await vi.waitUntil(() => { expect(1).toBe(2); }, { timeout: 20, interval: 10 });});
const throwPlainAsyncError = vi.defineHelper(async () => { await new Promise((_resolve, reject) => { setTimeout(() => { reject(new Error("async error from helper")); }, 1); });});
test("waitFor keeps helper callsite", async () => { await checkWithWaitFor();});
test("waitUntil keeps helper callsite", async () => { await checkWithWaitUntil();});
test("plain async error", async () => { await throwPlainAsyncError();});