Jest test setup — Файлы плагинов/предустановок не позволяют экспортировать объекты, только функции

#vue.js #vuejs2 #jestjs #babel-jest

Вопрос:

Я не уверен, как подойти к этой проблеме. Я прочитал некоторые ответы на такого рода ошибки и пришел к выводу, что это, по-видимому, результат смешения Вавилона 6 и Вавилона 7. Я думаю, что я был осторожен, чтобы не смешивать эти два, и я все еще получаю эту ошибку. Это приложение Vue.

вавилонский дворец

 {
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": [
    "transform-vue-jsx", 
    "transform-runtime"],
  "comments": false
}
 

пакет.json

   "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js",
    "test:unit": "jest"
  },
  "dependencies": {
    "@babel/runtime-corejs2": "^7.4.3",
    "babel-polyfill": "^6.26.0",
    "babel-runtime": "^6.26.0",
    "es6-promise": "^4.2.5",
    "moment": "^2.23.0",
    "vue": "^2.5.2",
    "vue-froala-wysiwyg": "^2.9.5",
    "vue-router": "^3.0.1",
    "vue-virtual-scroll-list": "^1.3.2",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "@vue/test-utils": "^1.2.2",
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.26.3",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-jest": "^27.1.1",
    "babel-loader": "^7.1.1",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "chalk": "^2.0.1",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "exif-js": "^2.3.0",
    "extract-text-webpack-plugin": "^3.0.0",
    "file-loader": "^1.1.11",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "html-webpack-plugin": "^2.30.1",
    "jest": "^27.1.1",
    "js-htmlencode": "^0.3.0",
    "linkifyjs": "^2.1.6",
    "node-notifier": "^5.1.2",
    "node-sass": "^4.9.0",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "ora": "^1.2.0",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.8",
    "postcss-url": "^7.2.1",
    "rimraf": "^2.6.0",
    "sass-loader": "^7.0.1",
    "sass-resources-loader": "^1.3.3",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "style-loader": "^0.20.2",
    "uglifyjs-webpack-plugin": "^1.1.1",
    "url-loader": "^0.5.8",
    "vue-jest": "^3.0.7",
    "vue-js-modal": "^1.3.28",
    "vue-loader": "^13.3.0",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.6.14",
    "vue-toasted": "^1.1.24",
    "webpack": "^3.6.0",
    "webpack-bundle-analyzer": "^2.9.0",
    "webpack-dev-server": "^2.9.1",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 6.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}
 

Когда я запускаю «npm run test:unit», я получаю

 > cec-discussionboard@1.0.0 test:unit <HOME>sourcereposDiscussionBoard
> jest

 FAIL  src/components/Common/__tests__/CecMessageCard.spec.js
  ● Test suite failed to run

    Plugin/Preset files are not allowed to export objects, only functions. In <HOME>sourcereposDiscussionBoardnode_modulesbabel-preset-stage-2libindex.js

      at createDescriptor (node_modules/@babel/core/lib/config/config-descriptors.js:211:11)
          at createDescriptor.next (<anonymous>)
      at evaluateSync (node_modules/gensync/index.js:251:28)
      at node_modules/gensync/index.js:31:34
          at Array.map (<anonymous>)
      at Function.sync (node_modules/gensync/index.js:31:22)
      at Function.all (node_modules/gensync/index.js:210:24)
      at createDescriptors (node_modules/@babel/core/lib/config/config-descriptors.js:142:41)
          at createDescriptors.next (<anonymous>)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.232 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! cec-discussionboard@1.0.0 test:unit: `jest`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cec-discussionboard@1.0.0 test:unit 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!     <HOME>AppDataRoamingnpm-cache_logs2021-09-13T14_23_16_751Z-debug.log
 

Тестовый код является

 import { mount } from '@vue/test-utils'
import CecMessageCard from '../src/components/Common/CecMessageCard.vue'

describe('CecMessageCard mount', () => {
    test('mounts CecMessageCard', () => {
        const wrapper = mount(CecMessageCard, {
            propsData: { message: 'Test message', title: 'Test title' }
        });
        expect(wrapper.html()).toMatchSnapshot()
        return
    })
})
 

Очевидная ошибка при выполнении теста, указанная после «Сбой». Любые предложения о том, как настроить тестирование для этого приложения Vue2?

Комментарии:

1. опубликуйте свой тестовый код, пожалуйста