Jest遇到意外的令牌-为AceEditor编写Jest测试时

时间:2019-04-09 19:11:31

标签: jestjs gutenberg-blocks react-ace

虽然为wordpress创建了一个Gutenberg块, 我写了一个简单的笑话测试:

const cssInspector = require ('../src/controls/custom-css')

  console.log("Testinig Custom CSS")

test('Adding 1 + 1 equals 2', () => {
    expect(2).toBe(2)
  })

运行测试时出现的错误是:

测试套件无法运行

Jest遇到意外令牌

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: /Users/username/Local Sites/beansdev/app/public/wp-content/plugins/beanblocks/src/controls/custom-css/index.js: Unexpected token (123:12)

  121 | 
  122 |         return (
> 123 |             <AceEditor
      |             ^
  124 |                 value={cssInspectorText}
  125 |                 mode="css"
  126 |                 theme="github"

自定义css文件包含以下内容

const AceEditor = require('react-ace')
const { Component } = wp.element;

package.json中包含以下内容

{
  "name": "test-blocks",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "start": "cgb-scripts start",
    "build": "cgb-scripts build",
    "eject": "cgb-scripts eject",
    "test": "jest"
  },
  "jest": {
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!.*)"
    ]
  },
  "dependencies": {
    "@wordpress/compose": "^3.2.0",
    "babel-jest": "^24.7.1",
    "brace": "^0.11.1",
    "cgb-scripts": "1.15.0",
    "classnames": "^2.2.6",
    "css-selector-extract": "^4.0.0",
    "eslint-config-wordpress": "^2.0.0",
    "lodash": "^4.17.11",
    "memize": "^1.0.5",
    "postcss-discard-empty": "^4.0.1",
    "postcss-discard-unused": "^4.0.1",
    "re-resizable": "^4.11.0",
    "react-ace": "^6.4.0",
    "react-inlinesvg": "^0.8.4"
  },
  "devDependencies": {
    "@babel/core": "^7.4.3",
    "@babel/preset-env": "^7.4.3",
    "@wordpress/jest-preset-default": "^4.0.0",
    "clean-css": "^4.2.1",
    "cssnano": "^4.1.10",
    "jest": "^24.7.1"
  }
}

有什么想法吗?我在此处添加其他注释以符合堆栈溢出准则(需要更多文本)

0 个答案:

没有答案