簡體   English   中英

(Vue.js | Jest | Babel) 導入特定 vue 文件時出現意外令牌

[英](Vue.js | Jest | Babel) Unexpected token when importing specific vue file

我正在導入一個 vue 文件,其中只有 vue 文件中的腳本,只有模板標簽
然后我導入js文件來幫助我做我需要的
在最后一個導入中是問題(我認為),但只是這個文件,因為我已經用另一個文件進行了測試並且測試通過了

PS:抱歉我的新手問題和我的英語,這是我的第一個問題。 感謝您的關注

銷售.test.js

import ApexChart from 'vue-apexcharts'
import BootstrapVue from 'bootstrap-vue'
import sinon from 'sinon'
import { shallowMount, createLocalVue } from '@vue/test-utils'

import salesPage from '../src/views/vendas/Vendas'

包.json

"jest": {
    "moduleFileExtensions": [
      "js",
      "vue"
    ],
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/src/$1"
    },
    "transform": {
      "^[^.]+.(vue|jsx)$": "vue-jest",
      "^.+\\.(js|jsx)$": "babel-jest"
    },
    "snapshotSerializers": ["jest-serializer-vue"]
  }

.babelrc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "entry",
        "corejs": 2,
        "targets": {
          "esmodules": true,
          "node": "current"
        }
      }
    ]
  ],
  "plugins": [
    ["@babel/plugin-syntax-dynamic-import"],
    ["@babel/plugin-syntax-jsx"],
    [
      "@babel/plugin-transform-runtime",
      {
        "corejs": 2,
        "regenerator": true
      }
    ]
  ]
}

函數.js

'use strict'

import moment from 'moment'

export function dateFormatter(date) {
  return moment(date).format('DD/MM/YYYY')
}

export function changeInitialDate(date) {
  if (date) {
    this.disabledDatesEnd = null
    this.disabledDatesEnd = new Object()
    this.disabledDatesEnd.to = new Date(date)
  }
}

export function changeFinalDate(date) {
  if (date) {
    this.disabledDatesStart = null
    this.disabledDatesStart = new Object()
    this.disabledDatesStart.from = new Date(date)
  }
}

export function cnpjFormatter(cnpj) {
  return cnpj.toString().replace(/[^0-9a-zA-Z]+/g, '')
}

錯誤

Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    SyntaxError: /home/victor/development/accesys/AccCRM/Sources/cliente-web-clientes/unknown: Unexpected token (16:0)

      14 |   changeFinalDate,
      15 |   cnpjFormatter,
    > 16 | } from "../../functions";
         | ^
      17 | 
      18 | import {
      19 |   onClassChange,

      at Object._raise (node_modules/@babel/parser/src/parser/error.js:60:45)
      at Object.raiseWithData (node_modules/@babel/parser/src/parser/error.js:55:17)
      at Object.raise (node_modules/@babel/parser/src/parser/error.js:39:17)
      at Object.unexpected (node_modules/@babel/parser/src/parser/util.js:139:16)
      at Object.parseExprAtom (node_modules/@babel/parser/src/parser/expression.js:1186:20)
      at Object.parseExprAtom (node_modules/@babel/parser/src/plugins/jsx/index.js:535:22)
      at Object.parseExprSubscripts (node_modules/@babel/parser/src/parser/expression.js:563:23)
      at Object.parseUpdate (node_modules/@babel/parser/src/parser/expression.js:543:21)
      at Object.parseMaybeUnary (node_modules/@babel/parser/src/parser/expression.js:527:17)
      at Object.parseExprOps (node_modules/@babel/parser/src/parser/expression.js:343:23)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.524s

它可能是您導入中的尾隨逗號嗎?

由此:

import { changeFinalDate, cnpjFormatter, } from "../../functions";
                                       ^

對此:

import { changeFinalDate, cnpjFormatter } from "../../functions";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM