Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何修复Jest错误:TypeError:$不是函数_Unit Testing_Vue.js_Jestjs_Vue Test Utils - Fatal编程技术网

Unit testing 如何修复Jest错误:TypeError:$不是函数

Unit testing 如何修复Jest错误:TypeError:$不是函数,unit-testing,vue.js,jestjs,vue-test-utils,Unit Testing,Vue.js,Jestjs,Vue Test Utils,我正在为Vue.js组件编写单元测试用例 当我尝试使用vue test utilshallowMount组件时,我得到一个错误: TypeError:$不是VueComponent.mounted上的函数 (src/components/gridComponent/index.vue:7579:7) 我的代码: import $ from 'jquery'; global['$'] = global['jQuery'] = $; import { shallowMount, createLo

我正在为Vue.js组件编写单元测试用例

当我尝试使用
vue test util
shallowMount
组件时,我得到一个错误:

TypeError:$不是VueComponent.mounted上的函数 (src/components/gridComponent/index.vue:7579:7)

我的代码:

import $ from 'jquery';

global['$'] = global['jQuery'] = $;

import { shallowMount, createLocalVue } from '@vue/test-utils'
import gridComponent from '@/components/gridComponent/index'

describe('grid view component', () => {
    it('has the expected HTML structure', async () => {
        let wrapper = await shallowMount(gridComponent, {
            stubs: ['GridLayout', 'GridItem'],
            propsData: {componentsList}
        })
        expect(wrapper.element).toMatchSnapshot()
    })
})
以下是gridComponent的代码:

import * as $ from 'jquery'

export default {
    name: 'gridComponent',
    components: { GridLayout, GridItem, DxpCommonModal },
    props: ['componentsList'],
    watch: {
        componentsList: function (newVal, oldVal) {
          // eslint-disable-next-line
          this.matchingComponents = this.componentsList
        }
    },
    data () {
        return {
          isDraggable: true,
          isResizable: true
        }
    },
    created () {
    },
    computed: {
        ...mapGetters(['getResources'])
    },
    mounted () {
       $('#headerComponent').html(this.getSelectorHTML(this.siteDetails.header))
       $('#footerComponent').html(this.getSelectorHTML(this.siteDetails.footer))
        this.addHtmlOfComponentsInAPage()
    },
    destroyed () {
    },
    methods: {
        addHtmlOfComponentsInAPage () {
            this.selectedPage.Components.forEach((element, index) => {
                $('#${index}').html(this.getSelectorHTML(this.selectedPage.Components[index]))
            })
        },
        getSelectorHTML (component) {
            const element = document.createElement(component.componentType)
            element.setAttribute('content-id', new Date().getTime().toString())
            if (!this.values && component.demoData) {
                this.values = component.demoData
            }
            if (this.values) {
                this.createMarkup(element, this.values)
                this.values = ''
            }
            return element
        }
    }
}
故障排除 在组件中,尝试

const $ = require('jquery')
将其置于导致错误的方法中的用法之前

这会让你知道你是否正在某处取消定义或重新定义它

解决方案2 您可能需要jsdom

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM(`<!DOCTYPE html>`);
const $ = require('jquery')(window);
constjsdom=require(“jsdom”);
const{JSDOM}=JSDOM;
const{window}=newjsdom(``);
const$=require('jquery')(窗口);

您能显示gridComponent的代码吗?我已经添加了代码gridComponent在您使用它之前将其放入,而不是在方法
挂载的
之外,并且jquery在您的node\u modules文件夹中?您的配置中有jquery的别名吗?这可能是解决问题的方法……我不确定。您正在谈论在jest配置中添加它吗?您是否也尝试过在根文件中添加
require…
,而不是
import…