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

Testing with inject-loader does not work out of the box. #947

Closed
thomasmichaelwallace opened this issue Mar 6, 2018 · 13 comments
Closed

Comments

@thomasmichaelwallace
Copy link

Version

2.9.3

Steps to reproduce

  • Setup a project using babel, mocha/chai and inject-loader
  • Injecting a module will return: "Module parse failed: 'import' and 'export' may only appear at the top level inject"

What is expected?

The module injection function to be returned.

What is actually happening?

Test fails due to invalid configuration.


A workaround for this is to set the VUE_CLI_BABEL_TRANSPILE_MODULES flag while testing.

@yyx990803
Copy link
Member

vue-loader no longer works with inject-loader in 14.0+ because it does not work with ES modules.

We will suggest a different solution for mocking during tests after more investigation.

@thomasmichaelwallace
Copy link
Author

Thanks @yyx990803 - isn't esModules an option in vue-loader that can be set to false (or you can use the babel workaround I mentioned, which seems to be used when Jest is selected.)

I'm happy to do a PR to switch this on for mocha/chai so that the inject-loader approach noted on the vue-loader documentation works out of the box: https://vue-loader.vuejs.org/en/workflow/testing-with-mocks.html

@morningdew830
Copy link

morningdew830 commented Aug 20, 2018

@yyx990803 Is there any replacement for inject-loader? or is there an option to enable using of inject-loader in vue-cli 3?

@thomasmichaelwallace
Copy link
Author

@morningdew830 - you can work around it by using VUE_CLI_BABEL_TRANSPILE_MODULES=*

e.g. the test command should be:
"test": "VUE_CLI_BABEL_TRANSPILE_MODULES=* vue-cli-service test"

@thomasmichaelwallace
Copy link
Author

I actually made the jump to karma in the end because I needed some of the browser built-ins; but happy to do a PR to fix this with mocha if it's still an issue?

It's all about this line:

useESModules: !process.env.VUE_CLI_BABEL_TRANSPILE_MODULES,

@morningdew830
Copy link

morningdew830 commented Aug 20, 2018

Thanks for quick help! @thomasmichaelwallace
Unfortunately, I just tried with the environment but still getting the same error.

This is the old test code that had been working with vue-loader v13.

// eslint-disable-next-line import/no-webpack-loader-syntax
const injector = require('inject-loader!@/store/modules/hello-world')
/* test command */
"test:unit": "cross-env VUE_CLI_BABEL_TRANSPILE_MODULES=* vue-cli-service test:unit",

The project is created by vue-cli 3.0.1.

Any good idea?

@thomasmichaelwallace
Copy link
Author

thomasmichaelwallace commented Aug 20, 2018 via email

@morningdew830
Copy link

Thanks again!
I just updated the presets in the babel configuration so it worked.

--- babel.config.js ---

  presets: [
    ['@vue/app', {
      polyfills: [
        'es6.promise',
        'es6.symbol'
      ]
    }]
  ]

@thomasmichaelwallace
Copy link
Author

thomasmichaelwallace commented Aug 20, 2018 via email

@morningdew830
Copy link

no need, that env flag doesn't effect for me but I think it would be better if it works.

@salvadordiaz
Copy link

@thomasmichaelwallace @morningdew830 :

I had to add es6.module to the polyfills section to make it work. My babel.config.js looks like this:

module.exports = {
    presets: [
        ['@vue/app', {
            polyfills: [
                'es6.promise',
                'es6.symbol',
                'es6.module'
            ]
        }]
    ]
};

@cngu cngu mentioned this issue Mar 25, 2019
19 tasks
@IlyaEremin
Copy link

Hello. Since inject-module no longer supported then I guess It would be good to remove inject-module from examples: https://vuex.vuejs.org/guide/testing.html
const actionsInjector = require('inject-loader!./actions')

@OziOcb
Copy link

OziOcb commented Aug 19, 2021

Hello. Since inject-module no longer supported then I guess It would be good to remove inject-module from examples: https://vuex.vuejs.org/guide/testing.html
const actionsInjector = require('inject-loader!./actions')

I completely agree, I've just spent 30 min trying to make it work. All because of the example that you've mentioned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants