Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically mock require.context when VUE_CLI_BABEL_TRANSPILE_MODULES is true #193

Closed
jardimfelipe opened this issue Feb 17, 2020 · 7 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@jardimfelipe
Copy link

I used yarn new module command to create a new vuex module to test my app. However, I stumbled in the following error:

src/state/modules/team.unit.js
  ● Test suite failed to run

    TypeError: require.context is not a function

      11 |   // Allow us to dynamically require all Vuex module files.
      12 |   // https://webpack.js.org/guides/dependency-management/#require-context
    > 13 |   const requireModule = require.context(
         |                                 ^
      14 |     // Search for files in the current directory.
      15 |     '.',
      16 |     // Search for files in subdirectories.

      at updateModules (src/state/modules/index.js:13:33)
      at Object.<anonymous> (src/state/modules/index.js:10:2)
      at Object.<anonymous> (src/utils/dispatch-action-for-all-modules.js:1:1)
      at Object.<anonymous> (src/state/store.js:3:1)
      at Object.<anonymous> (src/main.js:4:1)
      at Object.<anonymous> (src/services/api-instance.js:2:1)
      at Object.<anonymous> (src/services/team.js:2:1)
      at Object.<anonymous> (src/state/modules/team.js:1:1)
      at Object.<anonymous> (src/state/modules/team.unit.js:1:1)

This error occurs on every vuex module file.

Bellow, this is an example of a vuex module:

import { GetTeamList } from '@src/services/team'
export const state = {
    teamList: [],
}

export const getters = {
    totalList({ state }) {
        return state.teamList.totalItemCount
    },
}

export const mutations = {
    SET_LIST(state, newList) {
        state.teamList = newList
    },
}

export const actions = {
    async fetchList() {
        const resp = await GetTeamList()
        console.log(resp)
    },
}

And this is its test:

import * as teamModule from './team'

describe('@state/modules/team', () => {
  it('exports a valid Vuex module', () => {
    expect(teamModule).toBeAVuexModule()
  })
})

This error doesn't occurs on newly created module using yarn new modules so I'm probably doing something wrong. If anybody have any clues in this matter, I would be very grateful

@EgorFront
Copy link
Contributor

As far as I understand, the problem is that Jest is not able to use dynamic imports (this is a feature of webpack)
The solution is to mock require.context
or u can polyfill it, see here

@chrisvfritz
Copy link
Collaborator

As @EgorFront mentioned, there are a number of solutions. My preferred solution has usually been to isolate the require.context call in a file that can be mocked, but I'm thinking about possibly building in an automatic workaround to the problem - the issue is none of the ones I know of are really ideal, so I'll probably have build in something custom.

@chrisvfritz chrisvfritz added enhancement New feature or request question Further information is requested labels Mar 2, 2020
@chrisvfritz chrisvfritz changed the title yarn:unit require.context error Automatically mock require.context when VUE_CLI_BABEL_TRANSPILE_MODULES is true Mar 2, 2020
@satyamqainfotech
Copy link

Adding a babel-plugin-transform-require-context plugin to .babelrc helps me solve the issue.

https://www.npmjs.com/package/babel-plugin-transform-require-context

Just add into .babelrc for test env and it will fix the issue

{ "env": { "test": { "plugins": ["transform-require-context"] } } }

@beamsies
Copy link

beamsies commented Oct 7, 2020

@satyamqainfotech

Adding a babel-plugin-transform-require-context plugin to .babelrc helps me solve the issue.

https://www.npmjs.com/package/babel-plugin-transform-require-context

Just add into .babelrc for test env and it will fix the issue

{ "env": { "test": { "plugins": ["transform-require-context"] } } }

Is this all you did? Because I can't get this to work.

Also, I'm new to Jest and not sure how to mock this if that is what I have to do.

tHanks.

@satyamqainfotech
Copy link

@satyamqainfotech

Adding a babel-plugin-transform-require-context plugin to .babelrc helps me solve the issue.
https://www.npmjs.com/package/babel-plugin-transform-require-context
Just add into .babelrc for test env and it will fix the issue
{ "env": { "test": { "plugins": ["transform-require-context"] } } }

Is this all you did? Because I can't get this to work.

Also, I'm new to Jest and not sure how to mock this if that is what I have to do.

tHanks.

Hey @beamsies - See this answer: https://stackoverflow.com/a/61137440/7358308, also you can look at other answers and try it out

@boydaihungst
Copy link

I used this package to fix this problem: https://www.npmjs.com/package/require-context.macro

@bencodezen
Copy link
Owner

Sorry for the delay in responding to this. I let the project lapse and am in the process of currently updating it to Vue 3 standards.

In order to clean up issues, I'll be closing this issue at this time, but if you still have issues with the new boilerplate or have questions, please don't hesitate to open another issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants