0

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.

Amir Choubani
  • 829
  • 2
  • 14
  • 28
  • What's not working? – Nick McCurdy Sep 20 '21 at 15:54
  • @NickMcCurdy When I run the test separetly It succeed, but when I run tests in the whole file It fails. – Amir Choubani Sep 20 '21 at 15:56
  • *the test separetly* - which test? *tests in the whole file* - which tests? There are only 2 tests in the code and both are empty, so they cannot cause problems. *It fails* - fails how? Please, show the exact errors and all the code that is affected. See https://stackoverflow.com/help/mcve and https://stackoverflow.com/help/how-to-ask , currently you're the only person who has all needed information at hand. You have fundamental problem with module mocking but it's unknown if it causes problems or something else. – Estus Flask Sep 20 '21 at 16:03
  • @EstusFlask I didn't put the body of tests. It does not matter. The problem is not with their body. – Amir Choubani Sep 20 '21 at 16:05
  • If you knew what the problem is and where it is, you'd be able to solve it, correct? If you aren't, consider providing users with all necessary information they need to solve your problem. The problem may be not in their bodies exactly but in what happens inside them. Please, provide MCVE and clear problem statement as required by SO rules, otherwise the question can't be answered and can be closed. – Estus Flask Sep 20 '21 at 16:08
  • @EstusFlask I updated my question. – Amir Choubani Sep 20 '21 at 16:15
  • 1
    The question still contains no errors and other tests. There are no clues what a possible problem could be. – Estus Flask Sep 20 '21 at 16:20
  • @EstusFlask I added a link to the project on github. You can take a look. – Amir Choubani Sep 21 '21 at 06:23
  • I'll do. Please, provide the exact error messages as well. – Estus Flask Sep 21 '21 at 06:25
  • try to run yarn test Analytics.spec.ts. – Amir Choubani Sep 21 '21 at 06:57
  • any help here ?! – Amir Choubani Sep 22 '21 at 09:07

0 Answers0