-
Notifications
You must be signed in to change notification settings - Fork 14.6k
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
[Bug Report] ReferenceError: _Message is not defined in jest tests #17121
Comments
You need to fix webpack loaders. On-demand import from element-ui requires additional configuration: https://element.eleme.io/#/en-US/component/quickstart#on-demand |
|
testing environment usually has saperate loader/env configuration. |
Double checking the docs, I am modifying babel config to have on-demand importing:
Is this not a global configuration, that will be applied to all environments? Do I have to just specify the same configuration for all environments manually (I'd think not)? Can you perhaps provide an example of correct configuration @wacky6? |
Which configuration is used depends on the particular testing framework / configuration you are using. I can not give exact advice on this, just a hint on where to look at. |
It would be great to see a working example, I've tried all the various Jest configuration options (for example I thought it might have been |
Still banging my head against this issue, I've tried numerous things, also made a StackOverflow question with some more details: https://stackoverflow.com/questions/57639448/elementui-tests-throw-referenceerror-message-is-not-defined Would love to hear from anyone who uses on-demand loading and Jest for testing |
You can use the test environment in babel to use different config file |
That is not really the answer to this issue, as it's not just about specifying the plugin configuration for test env. For everyone out there who would ever encounter the same issue (and avoid trying to debug this for days as I did), this is what actually had to be done:
module.exports = {
presets: [
'@vue/app',
],
plugins: [
[
'component',
{
libraryName: 'element-ui',
styleLibraryName: 'theme-chalk',
},
],
],
"env": { "test": { "plugins": ["transform-es2015-modules-commonjs", "dynamic-import-node"] } }
};
This is what I had to do specifically when using Vue CLI. There is some specific Babel configuration in there, as it uses babel-preset-app. You can dive into it to understand the intricacies of it. So thanks @wacky6 for pointing me in the right direction, although it still took days and another person to help find the solution. I think having some testing documentation in Element UI would really help people out... |
Here is my full module.exports = {
moduleFileExtensions: [
'js',
'jsx',
'json',
'vue',
],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.(js|jsx)?$': 'babel-jest',
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
'^.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
},
snapshotSerializers: [
'jest-serializer-vue',
],
testMatch: [
'**/tests/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)',
],
testURL: 'http://localhost/',
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
collectCoverage: true,
coverageReporters: ['lcov', 'text-summary'],
}; |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Ran into a similar issue, however, the solution above did not work for me. Was wondering if anyone has come across another solution that worked? Prerequisite: using vue test utils to install Jest: https://vue-test-utils.vuejs.org/installation/#using-vue-test-utils-with-jest-recommended Steps to replicate error: Step 1:
Step 2:
Step 3: Temp Solution: Then the unit test works as expected. Question: Also, why is this error happening? I searched the element-ui repo and |
Element UI version
2.10.1
OS/Browsers version
Mac High Sierra
Vue version
2.6.10
Reproduction Link
https://jsfiddle.com
Steps to reproduce
What is Expected?
import { Message } from 'element-ui' works without issues during my testing. Or being able to do a simple manual mock of 'element-ui'.
What is actually happening?
I get a ReferenceError: _Message is not defined error. It happens either in tests or inside the actual vue file. That is not a case for any other node modules. I tried doing all kinds of mocks (manual mock, automatic mock, I tried to spy on it...) but I was unable to get that to work.
I would be fine not using mocks, but simple imports don't work either inside the vue file itself, so my tests just fail.
The text was updated successfully, but these errors were encountered: