vuex-composition-helpers icon indicating copy to clipboard operation
vuex-composition-helpers copied to clipboard

Error importing with typescript from jest unit test

Open mblandfo opened this issue 4 years ago • 3 comments

I am seeing this error (#13 ) when running a jest test against a .vue file that imports 'vuex-composition-helpers'

"vuex-composition-helpers": "1.0.21"

I have tried the mentioned updates to vue.config.js and tsconfig.json from https://github.com/greenpress/vuex-composition-helpers#transpiling

mblandfo avatar Nov 02 '20 02:11 mblandfo

@mblandfo A fix you could apply to get your tests working again is to mock out the library. In Jest I will do this:

jest.mock('vuex-composition-helpers', () => ({
    useState: jest.fn(() => {
        return {
             /* values in your store */
        };
    }),
    // Other functions here
}));

codejockie avatar Jan 14 '21 11:01 codejockie

A quick workaround would be to slightly modify the way you import the library, going directly to the dist folder:

import { createNamespacedHelpers } from 'vuex-composition-helpers/dist';

pgarciaegido avatar Dec 02 '21 16:12 pgarciaegido

@mblandfo A fix you could apply to get your tests working again is to mock out the library. In Jest I will do this:

jest.mock('vuex-composition-helpers', () => ({
    useState: jest.fn(() => {
        return {
             /* values in your store */
        };
    }),
    // Other functions here
}));

I've tried this but then the store isn't available within the test, how would you test the component that uses a getter that retrieves the mocked state for example.

rowanfuchs avatar Jan 10 '23 13:01 rowanfuchs