【问题标题】:TypeError: Super expression must either be null or a functionTypeError:超级表达式必须为空或函数
【发布时间】:2023-04-10 01:13:05
【问题描述】:

我对 vue js 中的单元测试非常陌生。我刚刚通过这个网站学习单元测试“https://vue-test-utils.vuejs.org/guides/#testing-single-file-components-with-mocha-webpack”。当我在“mocha-webpack”中练习示例时,我得到了这个错误

WEBPACK  Failed to compile with 1 error(s)
Error in ./src/Counter.vue
TypeError: Super expression must either be null or a function
at /opt/htdocs/guru/unitTest_prct/node_modules/prettier/index.js:32893:5
at /opt/htdocs/guru/unitTest_prct/node_modules/prettier/index.js:32913:4
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! mdbvue@4.2.0 test: `mocha-webpack --webpack-config  webpack.config.js --require test/setup.js test/**/*.spec.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the mdbvue@4.2.0 test script.
npm ERR! This is probably not a problem with npm. There is likely     additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-09-06T10_28_47_073Z-debug.log

谁能告诉我如何解决这个错误。这是我的 Counter.vue 文件

<template>
  <div>
   <div>
     {{ count }}
     <button @click="increment">Increment</button>
   </div>
  </div>
</template>

<script>
export default {
    data () {
       return {
         count: 0
       }
    },
   methods: {
      increment () {
        this.count++;
      }
    }
};
</script>

这是我的 Counter.spec.js 文件

import { shallowMount } from '@vue/test-utils'
import Counter from '../src/docs/Counter.vue'
describe('Counter.vue', () => {
  it('increments count when button is clicked', () => {
    const wrapper = shallowMount(Counter)
    wrapper.find('button').trigger('click')
    expect(wrapper.find('div').text()).toMatch('1')
  })
})

【问题讨论】:

  • 你能发布你的 Counter.vue 文件吗?
  • @yuriy636 我已经更新了我的 counter.vue 文件
  • 我们可以看看你的进口吗?
  • @P3trur0 我在这里添加我的 Counter.spec.js 文件

标签: javascript node.js vue.js


【解决方案1】:

我查看了the issue 链接的above

我在测试脚本中有--require test/setup.js。下面是它的样子:

require('jsdom-global')();
window.Date = Date;

它解决了这个问题。试试看!

【讨论】:

    【解决方案2】:

    这是与prettier1.14.1 版本相关的问题,即您的场景中使用的NPM 包。

    确实,查看他们的GitHub repo 报告了该问题。目前有一个可能的解决方法:基本上就是注释掉prettier/index.js的第32893行。

    在您的环境中,您可以在此处找到该文件:/opt/htdocs/guru/unitTest_prct/node_modules/

    【讨论】:

    • 好!当然,请注意,这是一种不适合生产环境的解决方法。
    • 但我怀疑这个版本是否是我的问题意味着现在我已经将我的版本更新到 1.14.1 到 1.14.2 但我仍然面临这个问题。为什么?
    • 该错误在 GitHub 上看起来仍处于打开状态; 1.14.1 是引入它的版本。我想它会在未来得到修复。
    • 我正在使用sed -i.bak '32893s/^../\/\//' node_modules/prettier/index.js 在我们进行新设置时注释掉该行 - 但希望获得指导如何降级有问题的美化版本?你的意思是降级vue测试工具?
    • @themathmagician 是的,我的意思是这样。您应该检查哪个版本的 vue-test-utils 依赖于使用 prettifier 1.14.1 版本的 NPM 模块并相应地替换它。但是,恕我直言,由于这个错误应该在未来的版本中得到修复,所以在考虑降级之前,我会使用该解决方法一段时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 2021-10-20
    相关资源
    最近更新 更多