Skip to content

Flaky Tests with Moxios #75

Open
Open
@cashmann

Description

@cashmann

I'm using moxios in two files to mock out API requests and getting tests that seem to randomly fail.
For example:

import { expect } from 'chai'
import moxios from 'moxios'
import { SharedState, updateRenewal, mbcRequest, fetchVersions } from '../../../app/javascript/packs/sharedState.js'

const genericResponse = {
  id: 1,
  status: 'approved',
  versions: ['underwriting', 'approved']
}

describe('SharedState', () => {
  SharedState.currentRenewal = { id: 1 }

  beforeAll(() => {
    moxios.install()
    moxios.stubRequest('/commercial_renewals/1', {
      status: 200,
      response: genericResponse
    })
  })

  afterAll(() => {
    moxios.uninstall()
  })

  describe('updateRenewal', () => {
    let basic_params = {
      id: 1,
      status: 'approved'
    }

    it('updates the attributes on the SharedState updatedRenewals field', (done) => {
      let attribute = basic_params['status'];

      moxios.wait(() => {
        updateRenewal(basic_params);
        expect(SharedState.updatedRenewals[basic_params['id']]['status']).to.equal(attribute);
        done();
      })
    });
  });
...

If I run this test file, followed by another test file with a similar moxios setup, I get various errors around afterAll and variables being undefined and I don't quite get why.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions