-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Examples of testing mobx stores with async actions #494
Comments
@ochervak, you mean, how to check if your 'data' is correct? You can use something like const shallowEquals = require('shallow-equals')
// ...
const expectedData = { expectedKey: 'expectedValue' };
expect(shallowEquals(myStore.data.toJS(), expectedData)).toBe(true); Other Map methods can be also handy. |
@ochervak I use store.fetchData()
when(() => data.length > 0, () => { /* do assertions, end the test */ }) |
@mweststrate many thanks! i think issue can be closed |
Now my test looks like:
But there is a problem:
So i need to wrap my assertions to Can i fix this test without this ? |
@ochervak ah, I always use tape which doesn't throw on failed assertions :) In principle you can ignore the error, it's just a warning. I'll think about a proper solution for this! |
I’m using Jest and to get errors into the console when using
hope someone finds this useful. thanks @mweststrate for helping me out on twitter. |
Actually in our own projects we use the following utility (see below). Not that this is not just useful for So that you can do when(
() => expr
catchErrors(done, () => {
assertions
}
) /**
* In async tests, JEST will die (in watch mode) if an exception is thrown from a callback. This utility will catch
* the errors instead and report the test as failed in these case *
*
* @param {jest.DoneCallback} done
* @param {T} callback
* @returns {T}
*/
export function catchErrors<T extends Function>(done: jest.DoneCallback, callback: T): T {
return function() {
try {
callback.apply(null, arguments)
} catch (e) {
done.fail(e)
}
} as any as T
} cc: @cpojer is there a generic strategy for this in Jest? Didn't see anything in the docs |
I noticed this too and filed jestjs/jest#2059. We should definitely fix this in Jest. |
Thanks @cpojer! I added our typical workaround to the issue |
Closing for inactivity. |
Can you provide example (best practice) to write unit-tests for MobX store.
for example:
How should i unit test this store?
of course i mock my API bridge, however what should i use to check my 'data' hashmap.
thanks in advance
The text was updated successfully, but these errors were encountered: