【问题标题】:Getting Unexpected Token error on running Jest/React test运行 Jest/React 测试时出现意外令牌错误
【发布时间】:2017-10-26 00:38:03
【问题描述】:

我正在运行 jest/react 测试并收到以下错误:

/Users/sn/Documents/react-testing-with-jest-master/starter/CountdownTimer/jest.config.js:2
    "moduleFileExtensions": [
                          ^

SyntaxError: Unexpected token :

npm ERR! Test failed.  See above for more details.

这是我的 jest.config.js:

{
    "moduleFileExtensions": [
        "js",
        "jsx"
    ],
    "moduleNameMapper": {
        "Clock": "<rootDir>/app/components/Clock.jsx",
        "CountdownForm": "<rootDir>/app/components/CountdownForm.jsx",
        "Countdown": "<rootDir>/app/components/Countdown.jsx"
    }
}

【问题讨论】:

    标签: reactjs configuration jestjs


    【解决方案1】:

    由于您将文件命名为 jest.config.js 而不是 jest.config.json,因此您应该导出一个有效的 JavaScript 对象。

    要么:

    1. 将文件重命名为jest.config.json
    2. 导出一个有效的 JS 对象:

    jest.config.js:

    {
        moduleFileExtensions: [
            "js",
            "jsx"
        ],
        moduleNameMapper: {
            Clock: "<rootDir>/app/components/Clock.jsx",
            CountdownForm: "<rootDir>/app/components/CountdownForm.jsx",
            Countdown: "<rootDir>/app/components/Countdown.jsx"
        }
    }
    

    【讨论】:

    • 谢谢,@Dane。如何导出有效的 JS 对象?
    • 现在我得到:moduleNameMapper: { ^ SyntaxError: Unexpected token :
    • 尝试使用你以前的文件,并将"json" 添加到"moduleFileExtensions" facebook.github.io/jest/docs/en/…
    猜你喜欢
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 2023-03-28
    • 2017-11-12
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多