gpt4 book ai didi

reactjs - 通过 jest 运行测试,我在包含的 .jsx 文件中得到 `Unexpected token`

转载 作者:行者123 更新时间:2023-12-03 13:15:20 24 4
gpt4 key购买 nike

我正在使用 jest 和 Babel 6 并尝试运行包含 .jsx 文件的测试。由于某种原因,该文件没有被视为 .jsx,并且似乎被视为纯 js,从而导致具有 React 组件的行出现错误。

这是测试:

var searchPath = '../../../../../app/assets/javascripts/components/navigation/search_icon.js.jsx';
jest.dontMock(searchPath);

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils'

const SearchIcon = require(searchPath);

describe('components', () => {
describe('SearchIcon', () => {
it('Should dispatch an action to toggle the search bar when clicked', () => {

var icon = TestUtils.renderIntoDocument(
<SearchIcon labelOn="On" labelOff="Off"/>
);

// Smoke test - I can't even get this far :(
expect(true).to.eq(true)

})
})
});

我的 package.json 在这里:

{
"name": "fd-v5-web",
"version": "1.0.0",
"description": "Farmdrop mobile site",
"dependencies": {
"babel-preset-es2015": "^6.0",
"babel-preset-stage-0": "^6.0",
"babel-preset-react": "^6.0",
"babelify": "~>7.2",
"browserify": "~> 10.2.4",
"browserify-incremental": "^3.0.1",
"classnames": "~>2.1",
"immutable": "^3.7.5",
"lodash": "~3.9.3",
"moment": "~2.10.3",
"react": "^0.14.3",
"react-redux": "^4.0.0",
"react-dom": "^0.14.6",
"react-tools": "^0.13.1"
},
"engines": {
"node": "4.0.0",
"npm": "2.1.x"
},
"devDependencies": {
"babel-jest": "*",
"jest-cli": "*",
"react-addons-test-utils": "^0.14.3"
},
"scripts": {
"test": "BABEL_JEST_STAGE=0 jest"
},
"jest": {
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react",
"<rootDir>/node_modules/react-dom",
"<rootDir>/node_modules/react-addons-test-utils",
"<rootDir>/node_modules/fbjs"
],
"testFileExtensions": [
"js"
],
"moduleFileExtensions": [
"js",
"jsx",
"json",
"es6"
]
}
}

我正在命令行上运行 npm test

我的 .babelrc 看起来像这样:

{
"presets": [
"react",
"es2015"
]
}

最佳答案

已修复。我的 .babelrc 位于错误的文件夹中。它应该在根目录中。当我把它移到那里时,一切都正常了。

{
"presets": [
"react",
"es2015"
]
}

关于reactjs - 通过 jest 运行测试,我在包含的 .jsx 文件中得到 `Unexpected token`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34683758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com