slowli / chai-bytes Goto Github PK
View Code? Open in Web Editor NEWChai assertions for byte arrays equality
License: Apache License 2.0
Chai assertions for byte arrays equality
License: Apache License 2.0
The types for .equalBytes
are:
equalBytes(bytes: string | ArrayLike<number>): Assertion;
I was writing a test that ensures marshalling/unmarshalling works as expected. The object being marshalled has an optional field that's a Uint8Array
.
// for reasons the type is `{ foo?: Uint8Array }`
const input = createInput('some seemingly unrelated data')
const marshalled = marshal(input)
const output = unmarshal(marshalled)
// now do some assertions
What I'd like to do, and can do without types:
expect(input).to.have.property('foo').that.is.ok
expect(output).to.have.property('foo').that.equalBytes(input.foo)
What I have to do with type checking:
if (!input.foo) {
throw new Error('input had no `foo`')
}
expect(output).to.have.property('foo').that.equalBytes(input.foo)
The reason being that the type of input.foo
is Uint8Array | undefined
and undefined
is not compatible with string | ArrayLike<number>
, so by doing if (!input.foo)
and throwing we let tsc
know that input.foo
is a thing and therefore a Uint8Array
and compatible with ArrayLike<number>
.
This feels a bit weird because we're using chai
to test some properties and tsc
to test others, though it's really just a quirk of the type system.
Chai itself specifies values to be tested as any
. If this module did the same thing it would allow much more concise tests.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.