-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improved matchers with more natural patterns and checks
- Loading branch information
1 parent
1cc2e47
commit 55355e5
Showing
8 changed files
with
129 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* This module exports a Jest matcher that extends the expect object with a custom matcher called "toEqual". | ||
* The "toEqual" matcher is used to compare two snarkyjs Field objects, or a Field object with a string or number. | ||
*/ | ||
import { expect } from "@jest/globals"; | ||
import { Bool, Field, State } from "snarkyjs"; | ||
import { BoolLike, FieldLike } from "../types"; | ||
|
||
function extractFromState<T extends BoolLike | FieldLike>( | ||
val: T & State<T> | ||
): T { | ||
return val.get ? val.get() : val; | ||
} | ||
|
||
export default () => { | ||
expect.extend({ | ||
toEqualBool<T extends Bool>(actual: T & State<T>, value: BoolLike) { | ||
/* Check if actual is a State object */ | ||
const toMatch: Bool = extractFromState(actual); | ||
|
||
if (!toMatch.toBoolean) { | ||
return { | ||
message: () => `Expected ${actual} to be a Bool`, | ||
pass: false, | ||
}; | ||
} | ||
const pass: boolean = toMatch.equals(Bool(value)).toBoolean(); | ||
const message = () => `Expected ${actual} to equal ${value}`; | ||
return { | ||
message, | ||
pass, | ||
}; | ||
}, | ||
toEqualField<T extends Field>(actual: T & State<T>, value: FieldLike) { | ||
/* Check if actual is a State object */ | ||
const toMatch: Field = extractFromState(actual); | ||
|
||
if (!toMatch.toFields) { | ||
return { | ||
message: () => `Expected ${actual} to be a Field`, | ||
pass: false, | ||
}; | ||
} | ||
|
||
const pass: boolean = toMatch.equals(Field(value)).toBoolean(); | ||
const message = () => `Expected ${actual} to equal ${value}`; | ||
return { | ||
message, | ||
pass, | ||
}; | ||
}, | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Bool, Field } from "snarkyjs"; | ||
import configFieldMatchers from "../../lib/matchers/common-types"; | ||
|
||
configFieldMatchers(); | ||
|
||
describe("Common Types", () => { | ||
describe("toEqualField", () => { | ||
it("should pass when fields are equal", () => { | ||
expect(Field(42)).toEqualField(Field(42)); | ||
}); | ||
|
||
it("should pass when fields are equal if using a string", () => { | ||
expect(Field(42)).toEqualField("42"); | ||
}); | ||
|
||
it("should fail when fields are not equal", () => { | ||
expect(Field(42)).not.toEqual(Field(43)); | ||
}); | ||
|
||
it("Should fail if not proper type", () => { | ||
try { | ||
expect("invalid").toEqualField(Field(1)); | ||
fail("Should have thrown"); | ||
} catch (e) { | ||
expect(e.message).toBe("Expected invalid to be a Field"); | ||
} | ||
}) | ||
}); | ||
|
||
describe("toEqualBool", () => { | ||
it("should pass when booleans are equal", () => { | ||
expect(Bool(true)).toEqualBool(Bool(true)); | ||
}); | ||
|
||
it("should fail when booleans are not equal", () => { | ||
expect(Bool(true)).not.toEqualBool(Bool(false)); | ||
}); | ||
|
||
it("should fail when booleans are not equal if using native type", () => { | ||
expect(Bool(true)).toEqualBool(true); | ||
}); | ||
|
||
it("Should fail if not proper type", () => { | ||
try { | ||
expect("invalid").toEqualBool(false); | ||
fail("Should have thrown"); | ||
} catch (e) { | ||
expect(e.message).toBe("Expected invalid to be a Bool"); | ||
} | ||
}) | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.