Repository navigation
testing: Make expect compare values as Jest does - #711
Merged
Merged
Conversation
nsh/test's matchers compared values through JSON, so `undefined` and `null` were equal, dates, maps, sets and typed arrays lost their contents, `toBe` was structural and a RegExp ran in Rust's syntax. Tests written against Jest's meaning passed or failed for the wrong reasons, and the shape freezes with the first recorded release. expect now keeps the values JS made and compares them in Rust over the VM: `toBe` is `Object.is`, `toEqual` is Jest's recursive equality (classes, undefined properties as missing, dates, regular expressions, maps, sets, buffers and typed arrays, cycles), `toMatchObject` uses it for its leaves, `toContain` is `===`, and a RegExp given to `toMatch` or `toThrow` runs on the JS engine. Selectors stay matched in Rust, off the JS thread, and their docs now say so.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
nsh/test's matchers compared values through JSON:undefinedequallednull, dates, maps, sets and typed arrays lost their contents,toBewas structural and aRegExpran in Rust's syntax. This makes them Jest's (decided in the API review before the first recorded release):toBeisObject.is.toEqualis Jest's recursive equality: classes,undefinedproperties as missing ones, dates by time, regular expressions by source and flags, maps and sets by entries, buffers and typed arrays by bytes, cycles.toMatchObjectuses it for its leaves;toContainis===,toContainEqualstructural.RegExpgiven totoMatch/toThrowruns on the JS engine.nsh-testingvalues.rs), never through JSON.regexsyntax.Public API
nsh_core::element::JsClass—#[non_exhaustive] enum { Array, Function, Date, RegExp, Map, Set, Error, ArrayBuffer, View(&'static str), Object, … }: the class of a JS object, whattoEqualcompares by.JsIn::class(&self) -> Option<JsClass>— a value's class.JsIn::same_value(&self, other: JsIn<'_>) -> bool—Object.is.JsIn::as_buffer(&self) -> Option<Vec<u8>>— anArrayBuffer's or a view's bytes.JsIn::iterate(&self) -> Result<Option<Vec<JsOwned<'a>>>, PropError>— the items of an iterable.JsIn::invoke(&self, name: &str, args: &[JsIn<'_>]) -> Result<JsOwned<'a>, PropError>— call a method.JsHost::{class, same_value, buffer, iterate, invoke}— the host side of the above, with defaults for hosts that have none.nsh/test:expect's matchers keep their names and signatures; their meaning is Jest's.Test
cargo test -p nsh-testing --features js,bundle,agent(newtests/testing/expect/Expect.test.ts),-p nsh-js,-p nsh-corensh testof gallery (223), gallery-rs (74), counter, embedded, host-echo, rust-exportscargo clippy --workspace --all-targets, web clippy🤖 Generated with Claude Code