导入文件时的Jest SyntaxError

时间:2018-01-26 12:49:02

标签: javascript reactjs jestjs

我在运行Jest时遇到了SyntaxError。我可能需要在jest配置中添加一些内容才能使其正常工作。任何提示?

jest error - SyntaxError: Unexpected token . 以下是验证错误的代码。

Code.js

import React from 'react';
import hljs from 'highlight.js';
import 'highlight.js/styles/github.css';

class Code extends React.Component {
  componentDidMount() {
    hljs.highlightBlock(this.component)
  }

  render() {
    const code = "{'name': 'Daniel'}";
    return (
      <pre ref={(node) => { this.component = node }}>
        <code className="json">{code}</code>
      </pre>
    );
  }
}

export default Code;

Code.test.js

import Code from './Code';

describe('Code', () => {
  it('renders correctly', () => {
    expect(1).toBe(1);
  });
});

的package.json

{
  "scripts": {
    "test": "jest"
  },
  "dependencies": {
    "highlight.js": "^9.12.0",
    "react": "^16.2.0"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-jest": "^22.1.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "jest": "^22.1.4",
    "regenerator-runtime": "^0.11.1"
  },
  "babel": {
    "presets": [
      "env",
      "react"
    ]
  }
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

答案是in Jest docs(谁会想到?:)。

使用

更新package.json
{
  "jest": {
    "moduleNameMapper": {
        "\\.(css|less)$": "./styleMock.js"
    }
}

并添加styleMock.js

module.exports = {};