Moxios请求状态未在测试之间清除

问题描述 投票:1回答:1

我的规格很奇怪,因为当我单独运行测试时,它们会通过。但是,当我一起运行测试套件时,失败测试仍然继续使用成功axios mock而不是使用正确的失败http axios mock。这导致我的测试失败。我错过了在代码的不同部分隔离2个模拟的东西吗?

jobactions.js

export const loadUnassignedJobs = (job_type) => {
  if (!['unscheduled', 'overdue'].includes(job_type)) {
    throw 'Job Type must be "unscheduled" or "overdue".';
  }
  return (dispatch) => {
    dispatch({type: JobActionTypes.LOAD_UNASSIGNED_JOBS_STARTED, job_type });
    return axios.get(defaults.baseapi_uri + 'jobs/' + job_type)
      .then(function (response) {
        dispatch(updateUnassignedJobs(response.data.jobs));
        // handle success
      })
      .catch(function (error) {
        // handle error
        dispatch({ type: JobActionTypes.LOAD_UNASSIGNED_JOBS_FAILURE, error });
      })
      .then(function () {
        // always executed
      });
  }
};

export const updateUnassignedJobs = (unassigned_jobs) => {
  let unassigned_job_ids = [];
  let jobs = {};
  for (let job of unassigned_jobs) {
    unassigned_job_ids.push(job.id);
    jobs[job.id]=job;
  }

  return({
    type: JobActionTypes.LOAD_UNASSIGNED_JOBS_SUCCESS,
    jobs,
    unassigned_job_ids,
  });
};

spec.js

import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import * as jobActions from "../../../app/javascript/actions/JobActions"
import { findAction } from '../support/redux_store'
import * as JobActionTypes from '../../../app/javascript/constants/JobActionTypes'
import fixtures_jobs_unscheduled_success from '../fixtures/jobs_unscheduled_success'
import moxios from "moxios";

export const mockStore = configureMockStore([thunk]);

let store;

describe ('loadUnassignedJobs', () => {
  context('when bad parameters are passed', async () => {
    it('will raise an error', () => {
      const store = mockStore();

      expect(() => {
        store.dispatch(jobActions.loadUnassignedJobs('wrong_type'));
      }).to.throw('Job Type must be "unscheduled" or "overdue".');
    });
  });

  context('when unscheduled is passed', () => {
    beforeEach(() => {
      moxios.install();
      console.log("before each called");
      console.log(moxios.requests);
      store = mockStore();
      store.clearActions();
    });

    afterEach(() => {
      console.log("after each called");
      console.log(moxios.requests);
      moxios.uninstall();
    });
    context('on success', () => {
      beforeEach(() => {
        moxios.wait(() => {
          let request = moxios.requests.mostRecent();
          request.respondWith({
            status: 200,
            response: fixtures_jobs_unscheduled_success
          });
        });
      })
      it('dispatches LOAD_UNASSIGNED_JOBS_STARTED', () => {
        store.dispatch(jobActions.loadUnassignedJobs('unscheduled')).then(() => {
          expect(findAction(store, JobActionTypes.LOAD_UNASSIGNED_JOBS_STARTED)).to.be.eql({
            type: JobActionTypes.LOAD_UNASSIGNED_JOBS_STARTED,
            job_type: 'unscheduled'
          });
        });
      });

      it('dispatches updateUnassignedJobs()', () => {
        store.dispatch(jobActions.loadUnassignedJobs('unscheduled')).then(() => {
          expect(findAction(store,JobActionTypes.LOAD_UNASSIGNED_JOBS_SUCCESS)).to.be.eql(jobActions.updateUnassignedJobs(fixtures_jobs_unscheduled_success.jobs))
        });
      });
    });

    context('on error', () => {
      beforeEach(() => {
        //console.log("before each on error called");
        //console.log(moxios.requests);

        moxios.wait(() => {
          console.log('after waiting for moxios..')
          console.log(moxios.requests);
          let request = moxios.requests.mostRecent();
          request.respondWith({
            status: 500,
            response: { error: 'internal server error' }
          });
        });
      })
      it('dispatches LOAD_UNASSIGNED_JOBS_FAILURE', (done) => {
        console.log(moxios.requests);
        store.dispatch(jobActions.loadUnassignedJobs('unscheduled')).then(() => {
          console.log(moxios.requests);
          console.log(store.getActions());
          expect(findAction(store, JobActionTypes.LOAD_UNASSIGNED_JOBS_FAILURE)).to.include({
            type: JobActionTypes.LOAD_UNASSIGNED_JOBS_FAILURE
          });
          expect(findAction(store, JobActionTypes.LOAD_UNASSIGNED_JOBS_FAILURE).error).to.include({
            message: 'Request failed with status code 500'
          });
          done();
        });
      });
      it('does not dispatch LOAD_UNASSIGNED_JOBS_SUCCESS', (done) => {
        store.dispatch(jobActions.loadUnassignedJobs('unscheduled')).then(() => {
          expect(findAction(store, JobActionTypes.LOAD_UNASSIGNED_JOBS_SUCCESS)).to.be.undefined;
          done();
        });
      });
    })
  });
});

describe('updateUnassignedJobs', () => {
  it('assigns jobs to hash and creates an unassigned_job_ids array', () => {
    expect(jobActions.updateUnassignedJobs([ { id: 1, step_status: 'all_complete' }, { id: 2, step_status: 'not_started' } ])).to.be.eql(
      {
        type: JobActionTypes.LOAD_UNASSIGNED_JOBS_SUCCESS,
        jobs: { 1: { id: 1, step_status: 'all_complete' }, 2: { id: 2, step_status: 'not_started' } },
        unassigned_job_ids: [ 1,2 ]
      }
    )
  });
});
mocha karma-runner axios chai moxios
1个回答
1
投票

发现了这个问题!

成功案例的it()阻止没有使用done回调导致afterEach() moxios.uninstall()过早被调用,并且在呼叫完成后没有重置请求。修复此问题,现在所有测试都通过了。

© www.soinside.com 2019 - 2024. All rights reserved.