Jest tests affects other tests

941 Views Asked by At

I am using Jest with vuejs 2.

When I run the test separetly It succeed, but when I run tests in the whole file It fails.

I tried all solutions proposed to clearMocks and resetMocks ect, but It didn't work with me.

The project is available on github.

Here is my Code:

    // views/analytics.ts
    import { getAnalytics } from '@/client/analytics'; // this method returns a Promise
    
    export default Vue.extend({
    ...
    data() {
      return {
       property1: '',
     }
    },
    mounted() { 
      getAnalytics(this.agentName, size).then((response) => {
        this.property1 = response.propertyName
       })
      }
     })

    // client/analytics.ts
    const getAnalytics = async (agentName: string, size: number): Promise<any> => {
      const response = await axios.get(url, opt);
      _.reverse((response as any).analytics);
      return response;
    };
    
    export {
      getAnalytics,
    };

// analytics.spec.ts
   jest.mock('@/client/analytics', () => ({
        getAnalytics: () => Promise.resolve(mockedData), // mockedData is a JSON object
      }));

   describe ('analytics.vue', () => {
     beforeEach(async () => {
       jest.resetModules();
       jest.clearAllMocks();
       const localVue = createLocalVue();
       localVue.use(ElementUI);
       wrapper = shallowMount(Analytics, {
         localVue,
         mocks: {
           $route,
         },
       });
     comp = wrapper.vm;
    });

     afterEach(() => {
       wrapper.destroy();
     });

     it('test 1', async () => {
        const myProperty = comp.$data.property1;
        expect(myProperty).toEqual('expectedValue');
     })

      it('test 2', async () => {
        // same body as test 1.
      })
})

I tried different propositions from here and here but It doesn't work with me.

0

There are 0 best solutions below