How to wait for request to be finished with axios-mock-adapter like it's possible with moxios?










2















I try to test a rendering of some content after fetching it from server.
I use Vue Test Utils but this is irrelevant.



In the created hook of the component the ajax call is made with axios. I register the axios-mock-adapter response and 'render' the component, the call is made and everything works fine but i have to use the moxios lib only to wait for request to be finished.



it('displays metrics', (done) => 

this.mock.onGet('/pl/metrics').reply((config) =>
let value = 0
if (config.params.start == '2020-01-26')
value = 80

if (config.params.start == '2020-01-28')
value = 100

return [200,
metrics: [

key: "i18n-key",
type: "count",
value: value

]
]
)
.onAny().reply(404)

let wrapper = mount(Dashboard)

moxios.wait(function()
let text = wrapper.text()
expect(text).toContain('80')
expect(text).toContain('100')
expect(text).toContain('+20')
done()
)
)


Is it possible to get rid of moxios and achieve the same with axios-mock-adapter only?










share|improve this question




























    2















    I try to test a rendering of some content after fetching it from server.
    I use Vue Test Utils but this is irrelevant.



    In the created hook of the component the ajax call is made with axios. I register the axios-mock-adapter response and 'render' the component, the call is made and everything works fine but i have to use the moxios lib only to wait for request to be finished.



    it('displays metrics', (done) => 

    this.mock.onGet('/pl/metrics').reply((config) =>
    let value = 0
    if (config.params.start == '2020-01-26')
    value = 80

    if (config.params.start == '2020-01-28')
    value = 100

    return [200,
    metrics: [

    key: "i18n-key",
    type: "count",
    value: value

    ]
    ]
    )
    .onAny().reply(404)

    let wrapper = mount(Dashboard)

    moxios.wait(function()
    let text = wrapper.text()
    expect(text).toContain('80')
    expect(text).toContain('100')
    expect(text).toContain('+20')
    done()
    )
    )


    Is it possible to get rid of moxios and achieve the same with axios-mock-adapter only?










    share|improve this question


























      2












      2








      2








      I try to test a rendering of some content after fetching it from server.
      I use Vue Test Utils but this is irrelevant.



      In the created hook of the component the ajax call is made with axios. I register the axios-mock-adapter response and 'render' the component, the call is made and everything works fine but i have to use the moxios lib only to wait for request to be finished.



      it('displays metrics', (done) => 

      this.mock.onGet('/pl/metrics').reply((config) =>
      let value = 0
      if (config.params.start == '2020-01-26')
      value = 80

      if (config.params.start == '2020-01-28')
      value = 100

      return [200,
      metrics: [

      key: "i18n-key",
      type: "count",
      value: value

      ]
      ]
      )
      .onAny().reply(404)

      let wrapper = mount(Dashboard)

      moxios.wait(function()
      let text = wrapper.text()
      expect(text).toContain('80')
      expect(text).toContain('100')
      expect(text).toContain('+20')
      done()
      )
      )


      Is it possible to get rid of moxios and achieve the same with axios-mock-adapter only?










      share|improve this question
















      I try to test a rendering of some content after fetching it from server.
      I use Vue Test Utils but this is irrelevant.



      In the created hook of the component the ajax call is made with axios. I register the axios-mock-adapter response and 'render' the component, the call is made and everything works fine but i have to use the moxios lib only to wait for request to be finished.



      it('displays metrics', (done) => 

      this.mock.onGet('/pl/metrics').reply((config) =>
      let value = 0
      if (config.params.start == '2020-01-26')
      value = 80

      if (config.params.start == '2020-01-28')
      value = 100

      return [200,
      metrics: [

      key: "i18n-key",
      type: "count",
      value: value

      ]
      ]
      )
      .onAny().reply(404)

      let wrapper = mount(Dashboard)

      moxios.wait(function()
      let text = wrapper.text()
      expect(text).toContain('80')
      expect(text).toContain('100')
      expect(text).toContain('+20')
      done()
      )
      )


      Is it possible to get rid of moxios and achieve the same with axios-mock-adapter only?







      jasmine moxios axios-mock-adapter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 14:28







      Paul Geisler

















      asked Oct 26 '18 at 12:59









      Paul GeislerPaul Geisler

      3401319




      3401319






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Yes, you can implement your own flushPromises method with async/ await:



          const flushPromises = () => new Promise(resolve => setTimeout(resolve))

          it('displays metrics', async () =>
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          await flushPromises()

          expect(text).toContain('80')
          )


          Or use done and setTimeout:



          it('displays metrics', (done) => 
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          setTimeout(() =>
          expect(text).toContain('80')
          done()
          )
          )


          moxiois.wait simply schedules a callback with setTimeout. This works because a task scheduled by setTimeout always runs after the microtask queue, like promise callbacks, is emptied.






          share|improve this answer























          • Thanks a lot Edd! I will try it out as soon as possible! :)

            – Paul Geisler
            Nov 27 '18 at 11:00










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53009324%2fhow-to-wait-for-request-to-be-finished-with-axios-mock-adapter-like-its-possibl%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Yes, you can implement your own flushPromises method with async/ await:



          const flushPromises = () => new Promise(resolve => setTimeout(resolve))

          it('displays metrics', async () =>
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          await flushPromises()

          expect(text).toContain('80')
          )


          Or use done and setTimeout:



          it('displays metrics', (done) => 
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          setTimeout(() =>
          expect(text).toContain('80')
          done()
          )
          )


          moxiois.wait simply schedules a callback with setTimeout. This works because a task scheduled by setTimeout always runs after the microtask queue, like promise callbacks, is emptied.






          share|improve this answer























          • Thanks a lot Edd! I will try it out as soon as possible! :)

            – Paul Geisler
            Nov 27 '18 at 11:00















          1














          Yes, you can implement your own flushPromises method with async/ await:



          const flushPromises = () => new Promise(resolve => setTimeout(resolve))

          it('displays metrics', async () =>
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          await flushPromises()

          expect(text).toContain('80')
          )


          Or use done and setTimeout:



          it('displays metrics', (done) => 
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          setTimeout(() =>
          expect(text).toContain('80')
          done()
          )
          )


          moxiois.wait simply schedules a callback with setTimeout. This works because a task scheduled by setTimeout always runs after the microtask queue, like promise callbacks, is emptied.






          share|improve this answer























          • Thanks a lot Edd! I will try it out as soon as possible! :)

            – Paul Geisler
            Nov 27 '18 at 11:00













          1












          1








          1







          Yes, you can implement your own flushPromises method with async/ await:



          const flushPromises = () => new Promise(resolve => setTimeout(resolve))

          it('displays metrics', async () =>
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          await flushPromises()

          expect(text).toContain('80')
          )


          Or use done and setTimeout:



          it('displays metrics', (done) => 
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          setTimeout(() =>
          expect(text).toContain('80')
          done()
          )
          )


          moxiois.wait simply schedules a callback with setTimeout. This works because a task scheduled by setTimeout always runs after the microtask queue, like promise callbacks, is emptied.






          share|improve this answer













          Yes, you can implement your own flushPromises method with async/ await:



          const flushPromises = () => new Promise(resolve => setTimeout(resolve))

          it('displays metrics', async () =>
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          await flushPromises()

          expect(text).toContain('80')
          )


          Or use done and setTimeout:



          it('displays metrics', (done) => 
          this.mock.onGet('/pl/metrics').reply((config) =>
          // ..
          ).onAny().reply(404)

          let wrapper = mount(Dashboard)

          setTimeout(() =>
          expect(text).toContain('80')
          done()
          )
          )


          moxiois.wait simply schedules a callback with setTimeout. This works because a task scheduled by setTimeout always runs after the microtask queue, like promise callbacks, is emptied.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 18:02









          EddEdd

          2,19411426




          2,19411426












          • Thanks a lot Edd! I will try it out as soon as possible! :)

            – Paul Geisler
            Nov 27 '18 at 11:00

















          • Thanks a lot Edd! I will try it out as soon as possible! :)

            – Paul Geisler
            Nov 27 '18 at 11:00
















          Thanks a lot Edd! I will try it out as soon as possible! :)

          – Paul Geisler
          Nov 27 '18 at 11:00





          Thanks a lot Edd! I will try it out as soon as possible! :)

          – Paul Geisler
          Nov 27 '18 at 11:00

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53009324%2fhow-to-wait-for-request-to-be-finished-with-axios-mock-adapter-like-its-possibl%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線