Loading...
Loading...
Compare original and translation side by side
src/foo.tssrc/foo.test.tssrc/routes/bar.tssrc/tests/routes/bar.test.tsdescribe()src/foo.tssrc/foo.test.tssrc/routes/bar.tssrc/tests/routes/bar.test.tsdescribe()mockResetmockClearrestoreMocksgrep -rn 'mockReset\|mockClear\|restoreMocks' vitest.config.* jest.config.*mockReset: truebeforeEachimport { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({
myDep: vi.fn(),
}));
vi.mock("../path/to/dependency", () => ({
myDep: (...args: unknown[]) => mocks.myDep(...args),
}));
describe("bootstrap", () => {
beforeEach(() => {
mocks.myDep.mockResolvedValue({ ok: true });
});
it("mock resolves correctly", async () => {
const { myDep } = await import("../path/to/dependency");
expect(await myDep()).toEqual({ ok: true });
});
});npx vitest run <test-file> --reporter=verbosevi.hoisted()vi.mock()mockResetmockClearrestoreMocksgrep -rn 'mockReset\|mockClear\|restoreMocks' vitest.config.* jest.config.*mockReset: truebeforeEachimport { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({
myDep: vi.fn(),
}));
vi.mock("../path/to/dependency", () => ({
myDep: (...args: unknown[]) => mocks.myDep(...args),
}));
describe("bootstrap", () => {
beforeEach(() => {
mocks.myDep.mockResolvedValue({ ok: true });
});
it("mock resolves correctly", async () => {
const { myDep } = await import("../path/to/dependency");
expect(await myDep()).toEqual({ ok: true });
});
});npx vitest run <test-file> --reporter=verbosevi.hoisted()vi.mock()Remove the bootstrap test once you've confirmed the mock pattern works.
npx vitest run <test-file> --reporter=verbose确认模拟模式可行后,移除初始化测试用例。
npx vitest run <test-file> --reporter=verbosenpx vitest run <test-file> --reporter=verbosenpx vitest run <test-file> --reporter=verbosenpx vitest run --reporter=verbosenpx vitest run --reporter=verbosenpx eslint <changed-files>
npx tsc --noEmitnpx eslint <changed-files>
npx tsc --noEmittype(scope): description
Co-Authored-By: Claude <noreply@anthropic.com>type(scope): description
Co-Authored-By: Claude <noreply@anthropic.com>$ARGUMENTS/tdd add rate limiting to the search endpoint$ARGUMENTS/tdd add rate limiting to the search endpointconst mockFn = vi.fn();
vi.mock("./dep", () => ({
dep: (...args: unknown[]) => mockFn(...args),
}));
beforeEach(() => {
mockFn.mockResolvedValue(defaultResult);
});const mockFn = vi.fn();
vi.mock("./dep", () => ({
dep: (...args: unknown[]) => mockFn(...args),
}));
beforeEach(() => {
mockFn.mockResolvedValue(defaultResult);
});let callCount = 0;
mockExecute.mockImplementation(async () => {
callCount++;
if (callCount === 1) return insertResult;
if (callCount === 2) return selectResult;
});let callCount = 0;
mockExecute.mockImplementation(async () => {
callCount++;
if (callCount === 1) return insertResult;
if (callCount === 2) return selectResult;
});vi.mock("./dep", () => {
if (!(globalThis as any).__mocks) (globalThis as any).__mocks = {};
const m = { dep: vi.fn() };
(globalThis as any).__mocks.dep = m;
return { dep: (...a: unknown[]) => m.dep(...a) };
});
// In tests: const mocks = (globalThis as any).__mocks;vi.mock("./dep", () => {
if (!(globalThis as any).__mocks) (globalThis as any).__mocks = {};
const m = { dep: vi.fn() };
(globalThis as any).__mocks.dep = m;
return { dep: (...a: unknown[]) => m.dep(...a) };
});
// In tests: const mocks = (globalThis as any).__mocks;