Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⬆️ upgrade vitest, @vitest/browser #365

Merged
merged 8 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"@vitest/browser": "^0.29.7",
"@vitest/browser": "^0.34.6",
"eslint": "^8.35.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^4.2.1",
Expand All @@ -22,7 +22,7 @@
"tsup": "^6.7.0",
"typescript": "^5.0.2",
"vite": "4.1.4",
vvmnnnkv marked this conversation as resolved.
Show resolved Hide resolved
"vitest": "^0.29.4",
"vitest": "^0.34.6",
"webdriverio": "^8.6.7"
}
}
22 changes: 13 additions & 9 deletions packages/agents/test/HfAgent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { describe, expect, it } from "vitest";
import { HfAgent, defaultTools, LLMFromHub, LLMFromEndpoint } from "../src";
import type { Data } from "../src/types";
import type { HfInference } from "@huggingface/inference";

if (!process.env.HF_ACCESS_TOKEN) {
const env = import.meta.env;
if (!env.HF_ACCESS_TOKEN) {
console.warn("Set HF_ACCESS_TOKEN in the env to run the tests for better rate limits");
}

describe("HfAgent", () => {
it("You can create an agent from the hub", async () => {
const llm = LLMFromHub(process.env.HF_ACCESS_TOKEN, "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5");
const agent = new HfAgent(process.env.HF_ACCESS_TOKEN, llm);
const llm = LLMFromHub(env.HF_ACCESS_TOKEN, "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5");
const agent = new HfAgent(env.HF_ACCESS_TOKEN, llm);
expect(agent).toBeDefined();
});

it("You can create an agent from an endpoint", async () => {
const llm = LLMFromEndpoint(process.env.HF_ACCESS_TOKEN ?? "", "endpoint");
const agent = new HfAgent(process.env.HF_ACCESS_TOKEN, llm);
const llm = LLMFromEndpoint(env.HF_ACCESS_TOKEN ?? "", "endpoint");
const agent = new HfAgent(env.HF_ACCESS_TOKEN, llm);
expect(agent).toBeDefined();
});

Expand All @@ -29,7 +32,8 @@ describe("HfAgent", () => {
tools: ["uppercase"],
},
],
call: async (input) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
call: async (input: Promise<Data>, inference: HfInference): Promise<Data> => {
const data = await input;
if (typeof data !== "string") {
throw new Error("Input must be a string");
Expand All @@ -38,7 +42,7 @@ describe("HfAgent", () => {
},
};

const agent = new HfAgent(process.env.HF_ACCESS_TOKEN, undefined, [uppercaseTool, ...defaultTools]);
const agent = new HfAgent(env.HF_ACCESS_TOKEN, undefined, [uppercaseTool, ...defaultTools]);
const code = `
async function generate() {
const output = uppercase("hello friends");
Expand All @@ -57,7 +61,7 @@ async function generate() {
message(output);
}`;

const agent = new HfAgent(process.env.HF_ACCESS_TOKEN);
const agent = new HfAgent(env.HF_ACCESS_TOKEN);

await agent.evaluateCode(code).then((output) => {
expect(output.length).toBeGreaterThan(0);
Expand All @@ -71,7 +75,7 @@ async function generate() {
toolThatDoesntExist(aaa);
}`;

const hf = new HfAgent(process.env.HF_ACCESS_TOKEN);
const hf = new HfAgent(env.HF_ACCESS_TOKEN);

await hf.evaluateCode(code).then((output) => {
expect(output.length).toBeGreaterThan(0);
Expand Down
1 change: 1 addition & 0 deletions packages/agents/test/vitest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
4 changes: 2 additions & 2 deletions packages/agents/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"lib": ["ES2022", "DOM"],
"module": "CommonJS",
"module": "ESNext",
"moduleResolution": "node",
"target": "ES2022",
"forceConsistentCasingInFileNames": true,
Expand All @@ -14,6 +14,6 @@
"outDir": "./dist",
"declaration": true
},
"include": ["src", "index.ts", "../shared/src"],
"include": ["src", "test", "index.ts", "../shared/src"],
"exclude": ["dist"]
}
1 change: 1 addition & 0 deletions packages/agents/vitest-browser.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { configDefaults, defineConfig } from "vitest/config";

export default defineConfig({
envPrefix: ["HF_"],
test: {
exclude: [...configDefaults.exclude],
},
Expand Down
31 changes: 3 additions & 28 deletions packages/inference/test/HfInference.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import "./vcr";
import { readTestFile } from "./test-files";

const TIMEOUT = 60000 * 3;
const env = import.meta.env;

if (!process.env.HF_ACCESS_TOKEN) {
if (!env.HF_ACCESS_TOKEN) {
console.warn("Set HF_ACCESS_TOKEN in the env to run the tests for better rate limits");
}

describe.concurrent(
"HfInference",
() => {
// Individual tests can be ran without providing an api key, however running all tests without an api key will result in rate limiting error.
const hf = new HfInference(process.env.HF_ACCESS_TOKEN);
const hf = new HfInference(env.HF_ACCESS_TOKEN);

it("throws error if model does not exist", () => {
expect(
Expand Down Expand Up @@ -309,32 +310,6 @@ describe.concurrent(
]);
});
it("zeroShotClassification", async () => {
expect.extend({
closeTo(received, expected, precision) {
const { isNot } = this;
let pass = false;
let expectedDiff = 0;
let receivedDiff = 0;

if (received === Infinity && expected === Infinity) {
pass = true;
} else if (received === -Infinity && expected === -Infinity) {
pass = true;
} else {
expectedDiff = 10 ** -precision / 2;
receivedDiff = Math.abs(expected - received);
pass = receivedDiff < expectedDiff;
}

return {
pass,
message: () =>
isNot
? `expected ${received} to not be close to ${expected}, received difference is ${receivedDiff}, but expected ${expectedDiff}`
: `expected ${received} to be close to ${expected}, received difference is ${receivedDiff}, but expected ${expectedDiff}`,
};
},
});
expect(
await hf.zeroShotClassification({
model: "facebook/bart-large-mnli",
Expand Down
31 changes: 31 additions & 0 deletions packages/inference/test/expect-closeto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// https://github.com/vitest-dev/vitest/pull/4260
// this file can be removed after vitest update to 1.0

import { expect } from "vitest";

expect.extend({
closeTo(received: number, expected: number, precision: number) {
const { isNot } = this;
let pass = false;
let expectedDiff = 0;
let receivedDiff = 0;

if (received === Infinity && expected === Infinity) {
pass = true;
} else if (received === -Infinity && expected === -Infinity) {
pass = true;
} else {
expectedDiff = 10 ** -precision / 2;
receivedDiff = Math.abs(expected - received);
pass = receivedDiff < expectedDiff;
}

return {
pass,
message: () =>
isNot
? `expected ${received} to not be close to ${expected}, received difference is ${receivedDiff}, but expected ${expectedDiff}`
: `expected ${received} to be close to ${expected}, received difference is ${receivedDiff}, but expected ${expectedDiff}`,
};
},
});
9 changes: 0 additions & 9 deletions packages/inference/test/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
declare global {
namespace Vi {
interface Assertion {
closeTo(expected: number, precision: number): Assertion;
}
interface AsymmetricMatchersContaining {
closeTo(expected: number, precision: number): Assertion;
}
}

const __TEST_FILES__: Record<string, string>;
}

Expand Down
9 changes: 5 additions & 4 deletions packages/inference/test/vcr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ let VCR_MODE: MODE;
*
* If VCR_MODE is not set and a token is present then disable it.
*/
if (process.env.VCR_MODE) {
if ((process.env.VCR_MODE === MODE.RECORD || process.env.VCR_MODE === MODE.CACHE) && isFrontend) {
const env = import.meta.env;
if (env.VCR_MODE) {
if ((env.VCR_MODE === MODE.RECORD || env.VCR_MODE === MODE.CACHE) && isFrontend) {
throw new Error("VCR_MODE=record is not supported in the browser");
}

VCR_MODE = process.env.VCR_MODE as MODE;
VCR_MODE = env.VCR_MODE as MODE;
} else {
VCR_MODE = process.env.HF_ACCESS_TOKEN ? MODE.DISABLED : MODE.PLAYBACK;
VCR_MODE = env.HF_ACCESS_TOKEN ? MODE.DISABLED : MODE.PLAYBACK;
}

const originalFetch = globalThis.fetch;
Expand Down
23 changes: 23 additions & 0 deletions packages/inference/test/vitest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference types="vite/client" />
// https://vitest.dev/guide/extending-matchers
import "vitest";

interface MatcherResult {
pass: boolean;
message: () => string;
// If you pass these, they will automatically appear inside a diff when
// the matcher does not pass, so you don't need to print the diff yourself
actual?: unknown;
expected?: unknown;
}

interface CustomMatchers {
closeTo(expected: number, precision: number): MatcherResult;
}

declare module "vitest" {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Assertion extends CustomMatchers {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
2 changes: 1 addition & 1 deletion packages/inference/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"module": "commonjs"
}
},
"include": ["src", "index.ts", "../shared/src"],
"include": ["src", "test", "index.ts", "../shared/src"],
"exclude": ["dist"]
}
6 changes: 4 additions & 2 deletions packages/inference/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ for (const filename of testFilesToPreload) {
}

export default defineConfig({
test: {
setupFiles: ["./test/expect-closeto.ts"],
},
envPrefix: ["HF_", "VCR_"],
define: {
__TEST_FILES__: JSON.stringify(testFilesContents),
// make sure browser receives env vars
"process.env": JSON.stringify(process.env || {}),
},
});
Loading