Search code examples
vue.jsjestjsvue-test-utilselement-ui

ElementUI tests throw ReferenceError: _Message is not defined


Bug report on ElementUI

I am using On-demand loading of ElementUI components. I've followed the instructions correctly and it works just fine when running the app.

The problem arises when I try to test with Jest and Vue test utils. None of the components I am importing seem to be found, so I get this error when running my tests:

ReferenceError: _Message is not defined

I get the same error for any of the other components, that my test touches.

On the bug report I mentioned above, I am being suggested that my babel configuration is not being applied in my testing environment? Or its something about my Jest configuration. I've tried various things to fix this:

  1. Doing a manual mocks
  2. Spying on the component
  3. Importing the whole ElementUI package inside my test
  4. Updating Jest configuration

Nothing seems to work and I have no idea what is wrong...

bebel.config.js

module.exports = {
  presets: [
    '@vue/app',
  ],
  plugins: [
    [
      'component',
      {
        libraryName: 'element-ui',
        styleLibraryName: 'theme-chalk',
      },
    ],
  ],
};

jest.config.js

module.exports = {
  // roots: ['<rootDir>/src/', '<rootDir>/tests/'],
  moduleFileExtensions: [
    'js',
    'jsx',
    'json',
    'vue',
  ],
  transform: {
    '^.+\\.vue$': 'vue-jest',
    '.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
    '^.+\\.jsx?$': 'babel-jest',
  },
  transformIgnorePatterns: [
    '/node_modules/(?!element-ui)',
  ],
  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'],
};

Solution

  • I have a couple of suggestions for you here:

    change this in jest.config.js

    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'],
    };
    

    and this in babel.config.js

    module.exports = {
      presets: [
        '@vue/app',
      ],
      plugins: [
        [
          'component',
          {
            libraryName: 'element-ui',
            styleLibraryName: 'theme-chalk',
          },
        ],
      ],
      "env": { "test": { "plugins": ["transform-es2015-modules-commonjs", "dynamic-import-node"] } }
    };
    

    Also I believe @babel/plugin-transform-modules-commonjs is needed in yarn. Let me know if this works.