Skip to content

Commit

Permalink
Fix for Bun
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Dec 22, 2024
1 parent e702a07 commit 96b0c55
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
6 changes: 4 additions & 2 deletions js/src/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
toBeSemanticCloseTo,
} from "./matchers.js";
import { jestAsyncLocalStorageInstance, trackingEnabled } from "./globals.js";
import expectWithGradedBy from "./vendor/chain.js";
import { wrapExpect } from "./vendor/chain.js";
import type { SimpleEvaluator } from "./vendor/gradedBy.js";

declare global {
Expand Down Expand Up @@ -339,11 +339,13 @@ const lsTest = Object.assign(wrapTestMethod(test), {
each: eachMethod,
});

const wrappedExpect = wrapExpect(expect);

export {
lsTest as test,
lsTest as it,
lsDescribe as describe,
expectWithGradedBy as expect,
wrappedExpect as expect,
};

export { type SimpleEvaluator };
29 changes: 23 additions & 6 deletions js/src/jest/vendor/chain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Adapted from https://github.com/mattphillips/jest-chain/blob/main/src/chain.js
*/

import { expect } from "@jest/globals";

Check failure on line 4 in js/src/jest/vendor/chain.ts

View workflow job for this annotation

GitHub Actions / Check linting

'@jest/globals' should be listed in the project's dependencies, not devDependencies
import { gradedBy, SimpleEvaluator } from "./gradedBy.js";

class JestAssertionError extends Error {
Expand Down Expand Up @@ -58,22 +58,39 @@ const addGradedBy = (
originalArgs: any[],
staticPath: string[] = []
) => {
let spreadMatchers = { ...matchers };
// Handle Bun, which uses a class
if (Object.keys(matchers).length === 0) {
const prototypeProps = Object.getOwnPropertyNames(
Object.getPrototypeOf(matchers)
);
spreadMatchers = Object.fromEntries(
prototypeProps.map((prop) => {
try {
return [prop, (matchers as any)[prop]];
} catch (e) {
// Ignore bizarre Bun bug
return [];
}
})
) as any;
}
return Object.assign({}, matchers, {
gradedBy: function (evaluator: SimpleEvaluator) {
const mappedMatchers: any = _wrapMatchers(
matchers,
spreadMatchers,
evaluator,
originalArgs
);
// .not etc.
const staticMatchers = Object.keys(matchers)
const staticMatchers = Object.keys(spreadMatchers)
.filter((name) => typeof (matchers as any)[name] !== "function")
.map((name) => {
return {
[name]: Object.assign(
{},
..._wrapMatchers(
matchers,
spreadMatchers,
evaluator,
originalArgs,
staticPath.concat(name)
Expand All @@ -86,7 +103,7 @@ const addGradedBy = (
});
};

export default function expectWithGradedBy(expect: any) {
export function wrapExpect(expect: any) {
// proxy the expect function
const expectProxy = Object.assign(
(...args: any[]) => addGradedBy(expect(...args), args), // partially apply expect to get all matchers and chain them
Expand All @@ -96,4 +113,4 @@ export default function expectWithGradedBy(expect: any) {
return expectProxy;
}

(globalThis as any).expect = expectWithGradedBy((globalThis as any).expect);
(globalThis as any).expect = wrapExpect((globalThis as any).expect);

0 comments on commit 96b0c55

Please sign in to comment.