0

我正在尝试对vue-testing-library包含 jQuery UI 的可拖动小部件的文件运行单元测试。我实际上并没有尝试测试可拖动小部件,但这会导致错误,尽管尝试了一些类似问题的建议,但我似乎也无法找到解决方案。

以下是我使用该.draggable函数的代码。

$('#batch').draggable({disabled: !enable});

在我的jest.setup.js中,我有以下内容:

window.jQuery = require('jquery');
window.$ = require('jquery');

我也试过这个:

import * as $ from 'jquery';
Object.defineProperty(window, '$', {value: $});
Object.defineProperty(global, '$', {value: $});
Object.defineProperty(global, 'jQuery', {value: $});

在这两种情况下,我都会收到以下错误:

  ● Test suite failed to run

    ReferenceError: jQuery is not defined

    > 21 | import 'jquery-ui/ui/widgets/draggable'

这是我的jest.config.js

module.exports = {
    moduleFileExtensions: [
        'js',
        'json',
        'vue'
    ],
    transform: {
        '^.+\\.vue$': 'vue-jest',
        '.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
        '.*\\.(js)$': 'babel-jest',
    },
    testPathIgnorePatterns: [
        '<rootDir>/node_modules/',
    ],
    transformIgnorePatterns: [
        '<rootDir>/node_modules/(?!(laravel-mix-vue-svgicon/)'
    ],
    // support the same @ -> src alias mapping in source code
    moduleNameMapper: {
        '^@/(.*)$': '<rootDir>/js/$1',
    },
    testEnvironment: 'jest-environment-jsdom',
    // serializer for snapshots
    snapshotSerializers: [
        'jest-serializer-vue'
    ],
    testMatch: [
        '**/js/tests/unit/**/*.spec.js',
    ],
    // https://github.com/facebook/jest/issues/6766
    testURL: 'http://localhost/',
    watchPlugins: [],
    setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
}

错误来自jquery-ui/ui/widgets/draggable.js但我不确定为什么它认为jQuery没有定义为当我jQuery在组件上登录时,我可以看到它按预期定义。

4

0 回答 0