

Mocking custom service queries with FeathersJS
source link: https://blog.feathersjs.com/mocking-custom-service-queries-with-feathersjs-3aae74003259
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Mocking custom service queries with FeathersJS
This article describes how to extend FeathersJS capabilities to handle custom queries in your tests suite based on the specific example of geospatial queries.
Introduction to service mocking
FeathersJS provides a common query syntax, which is a subset of the MongoDB query syntax, on top of its unified service interface out of the box. Any database adapter complies with it and has the same interface as the other adapters. This is really good news because if you’d like to unit test your service you can easily mock it up by using the feathers-memory adapter instead of your actual DB adapter. Indeed, it is usually a better strategy for at least three key reasons:
- it avoids the headache of running a DB in your test setup,
- it is more flexible to generate/check test data using in-memory structures,
- it will run faster.


Custom queries support
So far, so good. However, each adapter can have this fancy yet powerful feature that you might want to use. For instance, our Weacast solution relies on MongoDB support of query operations on geospatial data. A typical use case is that we store GeoJson polygon features describing the coverage area of some data in the DB and we’d like to retrieve the ones that cover a particular region. For this we issue a query like this:
Geospatial query example on a MongoDB FeathersJS serviceLike feathers-reactive, feathers-memory relies on the great sift module to manage the common query syntax on in-memory structures. Indeed, sift works like MongoDB out of the box for the most usual operators. Unfortunately, mocking our geospatial service won’t work out of the box because sift is not aware of any $geoIntersects
operator by default. Luckily, you’re able to modify the default behavior to suite your needs. Just like standard expressions $not
, $eq
, and others, you will simply need to add your custom one and replace the default filtering function generated by feathers-memory:
To implement the intersection check between the query region and our in-memory GeoJson structures we use the great turf.js module.
If you liked this article feel free to have a look at our Open Source solutions and enjoy our other articles on Feathers, the Kalisio team !
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK