node.js - Return JavaScript Promise rejected fulfilled value -
i'm trying use node-geocoder npm package latitude , longitude of position objects.
could me please understand javascript promises.
here example code:
import nodegeocoder 'node-geocoder' function getlocation (str) { // selecting google provider const geocoder = nodegeocoder({ provider: 'google', }) return geocoder.geocode(str) .then(response => { // want return latitude longitude return [response[0].latitude, response[0].longitude] }) .catch(error => { console.log(`geocoder error: ${ error }`) }) } export default getlocation
so. test (jest framework):
import getlocation './index' test('checking', () => { expect(getlocation('29 champs elysée paris')).toequal([48.8698679, 2.3072976]) })
when i'm trying use test promise status {"fulfillmentvalue": undefined, "isfulfilled": false, "isrejected": false, "rejectionreason": undefined}
.
but need promise resolved result. how can it?
i don't want edit test
for testing, test suites offer async callback promise based tests. expect (pun intended) work this:
import getlocation './index' test('checking', (done) => { getlocation('29 champs elysée paris').then(geodata => { expect(geodata).toequal([48.8698679, 2.3072976]); done(); }).catch(error => { done(error) }); });
depending on testing framework might using, way call resolver callback (i.e: done()
) can change. however, pattern should more or less same.
Comments
Post a Comment