我有错误 SyntaxError: Unexpected token .
在我的代码中
import 'react-dates/lib/css/_datepicker.css'
Run Code Online (Sandbox Code Playgroud)
import 'semantic-ui-css/semantic.min.css'
Run Code Online (Sandbox Code Playgroud)
那些不在我的 spec.js 中,而是在我的实现代码中,知道为什么吗?我运行我的应用程序没有问题,但是当我尝试运行测试时开玩笑抛出错误。
我现在使用反应JEST来测试代码.如果一个组件是单个的,而不是导入任何其他组件,"npm test"运行顺利,现在,我想要一起测试多个组件,我立即得到了这个错误:
SyntaxError: Unexpected token .
Run Code Online (Sandbox Code Playgroud)
似乎只要反应是导入别的东西,比如这个:
require( './style/fixed-data-table.css' );
require( './style/jnpr-datatable.scss' );
Run Code Online (Sandbox Code Playgroud)
然后使用jest抛出意外的标记"." 错误.
我的设置一定有问题,但在哪里?我的Package.json包含:
"jest": {
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react/",
"<rootDir>/node_modules/react-dom/",
"<rootDir>/node_modules/react-addons-test-utils/"
]
}
Run Code Online (Sandbox Code Playgroud)
而.babelrc已经在根目录中了.还包括babel-jest.谢谢
Jest 遇到意外令牌
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how …
Run Code Online (Sandbox Code Playgroud) 我需要在我的笑话/酶测试中模拟一个导入的 CSS 文件:
头文件.test.js
import React from 'react'
import { shallow } from 'enzyme'
import { Header } from './Header'
jest.mock('semantic-ui-css/semantic.min.css') // <-- no effect...
it('should render page title', () => {
const wrapper = shallow(<Header title='Test' />)
expect(wrapper.find('title').text()).toEqual('Test')
})
Run Code Online (Sandbox Code Playgroud)
我确实收到了该import 'semantic-ui-css/semantic.min.css'
行的错误:
Test suite failed to run
/Users/node_modules/semantic-ui-css/semantic.min.css:11
@import url(https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic&subset=latin);/*!
^
SyntaxError: Invalid or unexpected token
Run Code Online (Sandbox Code Playgroud)
我不需要为测试导入 css 文件。所以我想模拟该导入以进行测试。我怎样才能做到这一点?
我试图这样做,jest.mock('semantic-ui-css/semantic.min.css')
但这不起作用。
这是 Header.js 的样子:
头文件.js
import Head from 'next/head'
import 'semantic-ui-css/semantic.min.css'
export const Header = props => …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Jest 和 Enzyme 来测试我的应用程序。我收到错误:
\n\n FAIL app/containers/Navbar/NavbarContainer.test.js\n \xe2\x97\x8f Test suite failed to run\n\n app/components/Navbar/styles.css: Unexpected token (1:0)\n > 1 | @import "../../styles/base/_variables";\n | ^\n 2 |\n 3 | .navbar{\n 4 | width: $navbar-width;\n
Run Code Online (Sandbox Code Playgroud)\n\n这是我在 package.json 中的 Jest 配置:
\n\n"jest": {\n "verbose": true,\n "globals": {\n "env": {\n "isProd": false,\n "isDev": true,\n "command": "start"\n }\n },\n "moduleNameMapper": {\n "\\\\.(css)$": "identity-obj-proxy"\n },\n "moduleFileExtensions": [\n "js",\n "jsx",\n "json",\n "css"\n ],\n "modulePaths": [\n "/app"\n ],\n "moduleDirectories": [\n "node_modules"\n ],\n "verbose": true,\n "setupFiles": …
Run Code Online (Sandbox Code Playgroud) jestjs ×5
javascript ×3
reactjs ×3
babel-jest ×1
css-modules ×1
enzyme ×1
next.js ×1
testing ×1
token ×1
unit-testing ×1