Giter VIP home page Giter VIP logo

chai-bytes's People

Contributors

dependabot[bot] avatar slowli avatar

Watchers

 avatar  avatar  avatar

Forkers

achingbrain

chai-bytes's Issues

Relax input types?

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.