Skip to content

Commit

Permalink
Fix ALS issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Dec 20, 2024
1 parent 0d424e2 commit 5903486
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion js/src/jest/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getEnvironmentVariable } from "../utils/env.js";

export const jestAsyncLocalStorageInstance = new AsyncLocalStorage<{
dataset?: Dataset;
examples?: (Example & { inputHash: string })[];
examples?: (Example & { inputHash: string; outputHash: string })[];
createdAt: string;
project?: TracerSession;
currentExample?: Partial<Example>;
Expand Down
42 changes: 29 additions & 13 deletions js/src/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import type { SimpleEvaluator } from "./vendor/gradedBy.js";
declare global {
namespace jest {

Check failure on line 21 in js/src/jest/index.ts

View workflow job for this annotation

GitHub Actions / Check linting

ES2015 module syntax is preferred over namespaces
interface AsymmetricMatchers {
toBeRelativeCloseTo(expected: string, options: any): void;
toBeAbsoluteCloseTo(expected: string, options: any): void;
toBeSemanticCloseTo(expected: string, options: any): Promise<void>;
toBeRelativeCloseTo(expected: string, options?: any): void;
toBeAbsoluteCloseTo(expected: string, options?: any): void;
toBeSemanticCloseTo(expected: string, options?: any): Promise<void>;
}
interface Matchers<R> {
toBeRelativeCloseTo(expected: string, options: any): R;
toBeAbsoluteCloseTo(expected: string, options: any): R;
toBeSemanticCloseTo(expected: string, options: any): Promise<R>;
toBeRelativeCloseTo(expected: string, options?: any): R;
toBeAbsoluteCloseTo(expected: string, options?: any): R;
toBeSemanticCloseTo(expected: string, options?: any): Promise<R>;
gradedBy(evaluator: SimpleEvaluator): jest.Matchers<Promise<R>> & {
not: jest.Matchers<Promise<R>>;
resolves: jest.Matchers<Promise<R>>;
Expand Down Expand Up @@ -86,12 +86,13 @@ function wrapDescribeMethod(
config?: Partial<RunTreeConfig>
) {
const testClient = config?.client ?? RunTree.getSharedClient();
let storageValue;
return method(datasetName, () => {
beforeAll(async () => {
if (!trackingEnabled()) {
jestAsyncLocalStorageInstance.enterWith({
storageValue = {
createdAt: new Date().toISOString(),
});
};
} else {
let dataset;
try {
Expand All @@ -116,18 +117,26 @@ function wrapDescribeMethod(
.createHash("sha256")
.update(JSON.stringify(example.inputs))
.digest("hex");
examples.push({ ...example, inputHash });
const outputHash = crypto
.createHash("sha256")
.update(JSON.stringify(example.inputs))
.digest("hex");
examples.push({ ...example, inputHash, outputHash });
}
const project = await _createProject(testClient, dataset.id);
jestAsyncLocalStorageInstance.enterWith({
storageValue = {
dataset,
examples,
createdAt: new Date().toISOString(),
project,
client: testClient,
});
};
}
});
// Shoutout to https://github.com/jestjs/jest/issues/13653#issuecomment-1349970884
beforeEach(async () => {
jestAsyncLocalStorageInstance.enterWith(storageValue!);
});
fn();

Check failure on line 140 in js/src/jest/index.ts

View workflow job for this annotation

GitHub Actions / Check linting

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
});
};
Expand Down Expand Up @@ -164,6 +173,10 @@ function wrapTestMethod(method: (...args: any[]) => void) {
.createHash("sha256")
.update(JSON.stringify(testInput))
.digest("hex");
const outputHash = crypto
.createHash("sha256")
.update(JSON.stringify(testOutput))
.digest("hex");
const context = jestAsyncLocalStorageInstance.getStore();
if (context === undefined) {
throw new Error(
Expand Down Expand Up @@ -196,7 +209,10 @@ function wrapTestMethod(method: (...args: any[]) => void) {
}
const testClient = config?.client ?? client!;
let example = (examples ?? []).find((example) => {
return example.inputHash === inputHash;
return (
example.inputHash === inputHash &&
example.outputHash === outputHash
);
});
if (example === undefined) {
const newExample = await testClient.createExample(
Expand All @@ -207,7 +223,7 @@ function wrapTestMethod(method: (...args: any[]) => void) {
createdAt: new Date(createdAt ?? new Date()),
}
);
example = { ...newExample, inputHash };
example = { ...newExample, inputHash, outputHash };
}
jestAsyncLocalStorageInstance.enterWith({
...context,
Expand Down
5 changes: 0 additions & 5 deletions js/src/jest/vendor/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ export default function expectWithGradedBy(expect: any) {
expect // clone additional properties on expect
);

// expectProxy.extend = (o: any) => {
// expect.extend(o); // add new matchers to expect
// expectProxy = Object.assign(expectProxy, expect); // clone new asymmetric matchers
// };

return expectProxy;
}

Expand Down

0 comments on commit 5903486

Please sign in to comment.