mercredi 26 octobre 2016

Can I cast a half implemented mock to a type with flowtype?

I'm using flowtype to annotate types in my JavaScript. In my tests I would also like to take advantage of flowtype.

I am testing that a function is given an argument that is an object with another function and that this other function is called the right number of times with the expected argument.

Example code:

function foo(obj: Bar) {
   obj.bar('bar');
}

Example test:

test('foo gets bar', t => {
    const mockBar: Bar = {
       bar: sinon.stub(),
    };
    foo(mockBar);
    t.true(mockBar.bar.calledWith('bar'));
});

Now Bar is a really complicated type with lots of properties and such and is not easy to mock fully and here I only want to test that 'bar' is given. Flowtype has none of this and errors out on saying that my mockBar is not really Bar and I'm not sure what to do at this point other than to not use flowtype in testing or mock out Bar completely somehow which would be a lot of work for a little test.

Can I simply somehow force cast the mock to be Bar in a way flowtype will be happy with?

Aucun commentaire:

Enregistrer un commentaire