Skip to content

Commit

Permalink
Upgrade to vitest2
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson committed Jul 31, 2024
1 parent 0d250fe commit 4a5b12f
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 266 deletions.
2 changes: 2 additions & 0 deletions .changeset/lovely-kangaroos-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"turbo": "^2.0.9",
"typescript": "^5.5.3",
"typescript-eslint": "^7.17.0",
"vitest": "^1.6.0"
"vitest": "^2.0.4"
},
"pnpm": {
"overrides": {
Expand Down
28 changes: 11 additions & 17 deletions packages/client/src/object/Cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { promiseStateAsync as pStateAsync } from "p-state";
import type { Mock, MockInstance } from "vitest";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { MinimalClient } from "../MinimalClientContext.js";
import type { AsyncClientCache } from "./Cache.js";
import type { AsyncClientCache, AsyncFactory } from "./Cache.js";
import { createAsyncClientCache, createClientCache } from "./Cache.js";

declare module "vitest" {
Expand Down Expand Up @@ -89,20 +89,14 @@ describe("AsyncCache", () => {
});

describe("race checks", () => {
let factoryFn: Mock<[MinimalClient, string], Promise<any>>;
let factoryFn: Mock<AsyncFactory<any, any>>;
let cache: ReturnType<typeof createSpys>;
let inProgress: ReturnType<typeof createSpys>;
let asyncCache: AsyncClientCache<string, string>;
let asyncCacheSpies: {
[K in keyof typeof asyncCache]: MockInstance<
Parameters<typeof asyncCache[K]>,
ReturnType<typeof asyncCache[K]>
>;
[K in keyof typeof asyncCache]: MockInstance<typeof asyncCache[K]>;
};
let asyncSetSpy: MockInstance<
[client: MinimalClient, key: string, value: string | Promise<string>],
Promise<any>
>;
let asyncSetSpy: MockInstance<typeof asyncCache.set>;
let factoryDefers: DeferredPromise<string>[];
let getPromises: Promise<string>[];

Expand All @@ -114,7 +108,7 @@ describe("AsyncCache", () => {
let expectedPending: Record<string, number> = {};

beforeEach(async () => {
factoryFn = vi.fn<any, Promise<any>>();
factoryFn = vi.fn();
factoryFn.mockImplementation(() => {
const defer = pDefer<string>();
factoryDefers.push(defer);
Expand Down Expand Up @@ -274,10 +268,10 @@ describe("AsyncCache", () => {
i++
) {
if (asyncCacheSpies.get.mock.calls[i][1] === key) {
expect(asyncCacheSpies.get.mock.results[i].type).toBe(
"throw",
expect(asyncCacheSpies.get.mock.settledResults[i].type).toBe(
"rejected",
);
expect(asyncCacheSpies.get.mock.results[i].value)
expect(asyncCacheSpies.get.mock.settledResults[i].value)
.toMatchInlineSnapshot(`[Error: aError]`);
}
}
Expand Down Expand Up @@ -352,7 +346,7 @@ describe("AsyncCache", () => {

function itReturnsForAsyncGet(results: any[]) {
it("returns for async get", () => {
expect(asyncCacheSpies.get.mock.results.map(a => a.value))
expect(asyncCacheSpies.get.mock.settledResults.map(a => a.value))
.toEqual(results);
});
}
Expand Down Expand Up @@ -396,8 +390,8 @@ describe("AsyncCache", () => {
]);

itReturnsForAsyncGet([
expect.any(Promise),
expect.any(Promise),
undefined,
undefined,
"bResult",
]);
});
Expand Down
15 changes: 13 additions & 2 deletions packages/client/src/object/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface ClientCache<K, V> {
}

/**
* @internal
* A simple async cache that can be used to store values for a given client.
*/
export interface AsyncClientCache<K, V> {
Expand All @@ -46,16 +47,19 @@ export interface AsyncClientCache<K, V> {
) => Promise<V>;
}

type Factory<K, V> = (client: MinimalClient, key: K) => V;
/** @internal */
export type Factory<K, V> = (client: MinimalClient, key: K) => V;

/**
* @internal
* Create a new cache without a factory function.
*/
export function createClientCache<K, V extends {}>(): ClientCache<
K,
V | undefined
>;
/**
* @internal
* Create a new cache with a factory function.
* @param fn A factory function that will be used to create the value if it does not exist in the cache.
*/
Expand Down Expand Up @@ -101,13 +105,20 @@ export function createClientCache<K, V extends {}>(
return { get, set, remove } as ClientCache<K, V>;
}

/** @internal */
export type AsyncFactory<K, V extends {}> = (
client: MinimalClient,
key: K,
) => Promise<V>;

/**
* @internal
* Create a new cache with an async factory function.
* @param fn A factory function that will be used to create the value if it does not exist in the cache.
* @returns
*/
export function createAsyncClientCache<K, V extends {}>(
fn: (client: MinimalClient, key: K) => Promise<V>,
fn: AsyncFactory<K, V>,
createCacheLocal: typeof createClientCache = createClientCache,
): AsyncClientCache<K, V> {
const cache = createCacheLocal<K, V>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ function createMockWebSocketConstructor(
eventEmitter.removeEventListener.bind(eventEmitter),
) as any,

send: vi.fn((a, _b) => {
send: vi.fn((a, _b: any) => {
logger.debug(
{ message: JSON.parse(a.toString()), webSocketInst },
"send() called",
Expand Down
2 changes: 1 addition & 1 deletion packages/generator-converters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@osdk/monorepo.tsup": "workspace:~",
"ts-expect": "^1.3.0",
"typescript": "^5.5.4",
"vitest": "^1.6.0"
"vitest": "^2.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@types/node": "^18.0.0",
"ts-expect": "^1.3.0",
"typescript": "^5.5.4",
"vitest": "^1.6.0"
"vitest": "^2.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
9 changes: 4 additions & 5 deletions packages/generator/src/util/test/createMockMinimalFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import { vi } from "vitest";
import type { ReaddirFn, WriteFileFn } from "../../MinimalFs.js";

export function createMockMinimalFiles() {
const writeFile = vi.fn<Parameters<WriteFileFn>, ReturnType<WriteFileFn>>(
const writeFile = vi.fn<WriteFileFn>(
() => Promise.resolve(),
);
const getFiles = () => Object.fromEntries(writeFile.mock.calls);

const readdir = vi.fn<
Parameters<ReaddirFn>,
ReturnType<ReaddirFn>
>(() => Promise.resolve([]));
const readdir = vi.fn<ReaddirFn>(
() => Promise.resolve([]),
);

return {
minimalFiles: {
Expand Down
2 changes: 1 addition & 1 deletion packages/maker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@osdk/monorepo.tsup": "workspace:~",
"@types/yargs": "^17.0.32",
"typescript": "^5.5.4",
"vitest": "^1.6.0"
"vitest": "^2.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/tool.release/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@types/semver": "^7.5.8",
"@types/yargs": "^17.0.32",
"typescript": "^5.5.4",
"vitest": "^1.6.0"
"vitest": "^2.0.4"
},
"publishConfig": {
"access": "public"
Expand Down
Loading

0 comments on commit 4a5b12f

Please sign in to comment.