首页 文章

Vue,Jest:如何模拟element-ui组件?找不到模块element.css

提问于
浏览
0

我的vue代码调用 element-ui modules:

// in main.js
import { Notification } from "element-ui";

起初,我的测试是投掷

无法从'test.js'中找到模块'Element'

所以我用模拟了模块

jest.mock('element-ui', () => ({
  Element: jest.fn(),
}))

然而,它仍然错误

无法从'test.js'中找到模块'element-ui / lib / theme-default / element.css'

我不知道如何通过这个 .

有任何想法吗 ?谢谢 .


如果我在test.js中导入element-ui组件:

ReferenceError: document is not defined

      at Object.<anonymous> (node_modules/element-ui/lib/utils/dom.js:22:39)
      at Object.<anonymous> (node_modules/element-ui/lib/utils/popup/popup-manager.js:9:12)
      at Object.<anonymous> (node_modules/element-ui/lib/utils/popup/index.js:14:21)

(Jest 21.2,vue-test-utils 1.0.0)

1 回答

  • 0

    这有助于:https://github.com/eddyerburgh/vue-hackernews/

    我现在导入main.js中所有与元素相关的组件,并且我模拟调用其中一个组件的每个方法:

    beforeEach(() => {
        // Mock this method that calls a Notification element-ui component.
        vm.notifyWarning = jest.fn();
      })
    

相关问题