vendredi 6 décembre 2019

How to get response from axios in offline test with local mock JSON

First off all thank you for reading this. I'm testing React app and I'm having some issues. I have a test:

import axios from'axios'
import 'babel-polyfill' -> imported because test throws error  ReferenceError: regeneratorRuntime is not defined

 function get(){
    return  axios.get('here I want to pass mock JSON file @public/mock/test.json How can I achieve this?')
}

describe('test simple GET', () => {
    it('simple key',  async () => {
        expect(
          await get().then(res =>  {
            console.log(res.data);
            return res.data
            }).catch(e=> console.log(e))
        ).toEqual(
            {
                //here is empty object because response is *undefined*
            }
        )
    })
})

I want to pass a mock JSON file @public/mock/test.json as an axios URL, is it even possible? How can I achieve it? In request config I see URL: 'http:/.public/mock/test.json', how can I overcome axios using HTTP.

I couldn't find accurate answer on web. Tried adding async/await. Also I'm having some problems with getting mock JSON. Thanks in advance for help.

Aucun commentaire:

Enregistrer un commentaire