I am getting Unexpected token on the React Component Name while running npm test. Tried reading several other similar questions but none seems to be working for me. I have added content of babelrc , package.json and my test file content below
<!-- content of .babelrc file -->
{ "presets": ["env"] }
<!-- content of package.son file -->
"dependencies": {
"react": "^16.2.0",
"react-bootstrap": "^0.32.1",
"react-dom": "^16.2.0",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.1",
"redux": "^3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-jest": "^23.0.0-alpha.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-to-json": "^3.3.3",
"jest": "^22.4.2",
"react-test-renderer": "^16.2.0"
},
"jest": {
"notify": true,
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"setupTestFrameworkScriptFile": "./src/setupTests.js",
"transform": {
"^.+\\.jsx?$": "babel-jest"
}
}
<!-- Content of Test file App.test.js -->
import React from 'react';
import { shallow, mount } from 'enzyme';
import App from '../../src/components/App';
// describe what we are testing
describe('Render App Component', () => {
// make our assertion and what we expect to happen
it('should render without throwing an error', () => {
wrapper = shallow(<App />);
expect(wrapper.find('.app__wrapper').length).toEqual(1);
})
})
Here is how I resolved the issue.
• Add the following content to your .babelrc file and make sure .babelrc is in the root folder
{ "presets": ["env","react"] }
• Make sure you exclude static assets like CSS, images, SCSS, PDF, fonts, etc. Add the following to package.json as highlighted in the screenshot
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
}
Screenshot:
If anybody comes here to figure out what is happening, I think the accepted answer is not related to the issue here.
When the file extension is .js and if you try to write JSX in that file, linter will complain if you have a correct ESlint configuration.
Instead, try to change the file extension to .jsx or .tsx (if Typescript involved).
Related
I am trying to add code coverage to cypress following this tutorial
I added #cypress/code-coverage and babel-plugin-istanbul to my package.json file below
{
"name": "quiz",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"cypress": "cypress open"
},
"dependencies": {
"next": "12.0.10",
"react": "17.0.2",
"react-dom": "17.0.2",
"sass": "^1.49.7"
},
"devDependencies": {
"#cypress/code-coverage": "^3.9.12",
"#types/node": "17.0.15",
"#types/react": "17.0.39",
"babel-plugin-istanbul": "^6.1.1",
"cypress": "^9.4.1",
"eslint": "8.8.0",
"eslint-config-next": "12.0.10",
"typescript": "4.5.5"
}
}
I configured my cypress/support/index.js
// Import commands.js using ES2015 syntax:
import './commands'
// Alternatively you can use CommonJS syntax:
// require('./commands')
import '#cypress/code-coverage/support'
I configured my cypress/plugins/index.js
/**
* #type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
require('#cypress/code-coverage/task')(on, config)
on('file:preprocessor', require('#cypress/code-coverage/use-babelrc'))
return config
}
My .babelrc
{
"presets": [
"next/babel"
],
"plugins": [
"istanbul"
]
}
After I run my test I get the following message
Only found unit test code coverage. [#cypress/code-coverage]
After I run my quiz.spec.js test, I run
npx nyc report --reporter=text-summary
And get an empty coverage summary
What am I doing wrong?
My github
I think it was just a node_modules problem, as your repo worked after download and initialization. I think babel can fail silently if it's dependencies are out of order.
I removed package.lock (because I use yarn to manage things) and then yarn to init the dependecies.
I have a React app going, and I'm trying to get the hang of decorators. They're causing unexpected token errors.
I'm using the transform-decorators-legacy and transform-class-properties babel plugins.
I've tried both the stage-0 and stage-2 presets. All the related posts on SO and elsewhere seem to think that the plugins should make it work. Some say to use the stage-0 preset, while others recommend stage-2.
Here's the file with the decorator:
import React, {Component} from 'react';
import Actions from '../firebase/actions';
import RecipeList from '../Recipe/recipe-list';
import connectToStores from 'alt-utils/lib/connectToStores';
import RecipeStore from '../../store/recipe-store';
#connectToStores
class Main extends Component {
constructor() {
super();
Actions.getRecipes();
}
static getStores() {
return [RecipeStore];
}
static getPropsFromStores() {
return RecipeStore.getState();
}
render() {
return (
<section>
<section className="container">
{
this.props.recipes
?
<RecipeList recipeList={this.props.recipes}/>
:
null
}
</section>
</section>
);
}
}
export default Main;
Here's the package.json file:
{
"name": "glutenfreehacker",
"version": "0.1.0",
"private": true,
"dependencies": {
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"firebase": "^4.12.1",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.4",
"recompose": "^0.26.0",
"redux": "^4.0.0"
},
"babel": {
"presets": [
"env",
"stage-2",
"react"
],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties"
]
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
I understand this has been discussed here and elsewhere. However, the solutions I've seen suggest using the above plugins and presets, and I still can't get it to work.
Thanks for reading, and any help is greatly appreciated.
For decorators, use the babel-plugin-transform-decorators.
Installation
npm install --save-dev babel-plugin-transform-decorators
Usage
Via .babelrc:
{
"plugins": ["transform-decorators"]
}
I'm trying to build a very simple react-native app to test react-navigation. It works fine until I install react-navigation and load the following code.
import { StackNavigator } from 'react-navigation';
Upon running, it gives me the message "unable to resolve module
'react/lib/ReactComponentWithPureRenderMixin' from 'Users/me/Desktop/Code/flexbox/node_modules/react-navigation/src/views/Header.js'..." despite the file actually existing at that location when I navigate to it manually. I've tried the clearing watchman, deleting / reinstalling the modules, and resetting the packager cache many times. Any thoughts? My package.json below.
{
"name": "flexbox",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.0.0-alpha.6",
"react-native": "0.43.3",
"react-navigation": "^1.0.0-beta.7"
},
"devDependencies": {
"babel-jest": "19.0.0",
"babel-preset-react-native": "1.9.1",
"jest": "19.0.2",
"react-test-renderer": "16.0.0-alpha.6"
},
"jest": {
"preset": "react-native"
}
}
You need to change your dependency to this
{
"react": "16.0.0-alpha.6",
"react-native": "0.43.3",
"react-navigation": "git+https://github.com/react-community/react-navigation.git#7edd9a7"
}
As discussed in this ticket: https://github.com/react-community/react-navigation/issues/923
I am trying to use ES2017 in my React project with webpack
I have a .babelrc file, and a package.json.
this is my .babelrc file:
{
"presets": ["es2017"]
}
and this is package.json:
{
"name": "myapp",
"version": "1.0.0",
"private": true,
"devDependencies": {
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-plugin-syntax-async-functions": "^6.13.0",
"babel-plugin-syntax-trailing-function-commas": "^6.22.0",
"babel-plugin-transform-async-to-generator": "^6.22.0",
"babel-preset-es2017": "^6.22.0",
"enzyme": "^2.4.1",
"react-addons-test-utils": "^15.3.0",
"react-scripts": "^0.4.0",
"webpack": "^2.2.1"
},
"dependencies": {
"css-loader": "^0.26.1",
"react": "^15.3.0",
"react-bootstrap": "^0.30.7",
"react-dom": "^15.3.0",
"react-redux": "^4.4.5",
"react-router": "^3.0.0",
"react-router-redux": "^4.0.7",
"redux": "^3.5.2",
"redux-logger": "^2.6.1",
"redux-thunk": "^2.1.0",
"style-loader": "^0.13.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject",
"test": "react-scripts test"
},
"eslintConfig": {
"extends": "./node_modules/react-scripts/config/eslint.js"
}
}
When I am trying to use double colon, console reports syntax error
<div onMouseEnter={::this.mouseEnter()}>
</div>
anything wrong?
In order to make use of the :: operator to bind the function, you need to use the babel-plugin-transform-function-bind plugin.
Install it using the below command
npm install --save-dev babel-plugin-transform-function-bind
and then in your .babelrc include it as
{
"presets": ["react", "stage-0", "es2015"],
"plugins": ["transform-function-bind"]
}
Make sure you install the above using
npm install -S babel-preset-stage-0 babel-preset-2015 babel-preset-react
:: is the ES2015+ function bind syntax and not ES2017
Read more about it here :
Not an answer per se, but you can achieve similar behavior by declaring class methods as lambdas (or any other method mentioned here) :
export class MyComponent extends React.Component {
mouseEnter = () => {
console.log('hover');
}
render() {
return <h1 onMouseEnter={this.mouseEnter}>hi!</h1>
}
}
I am now using React Jest to test code. If a component is single, and not importing anything else, "npm test" runs smoothly. Now I want to test multiple components together, and I immediately get this error:
SyntaxError: Unexpected token .
It seemed whenever React is importing something else, such as this one:
require( './style/fixed-data-table.css' );
require( './style/jnpr-datatable.scss' );
and then using Jest, it is throwing the unexpected token "." error.
There must be something wrong in my settings, but where? My package.json file contains:
"jest": {
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react/",
"<rootDir>/node_modules/react-dom/",
"<rootDir>/node_modules/react-addons-test-utils/"
]
}
And the .babelrc file is already in the root. Also babel-jest is included.
Have a look at the Jest documentation for Webpack integration. The problem is that Jest can’t work with other stuff than JavaScript. So you have to mock all non-JavaScript files you import. The easiest way is to configure a moduleNameMapper in your Jest configuration.
{
"jest": {
"moduleNameMapper": {
"\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js"
}
}
}
With a __mocks__/styleMock.js, that looks like this.
module.exports = {};
The easiest is to add an identity-obj-proxy package:
yarn add --dev identity-obj-proxy
And use it to automatically mock CSS/SCSS modules.
Add this to the package.json:
"jest": {
"moduleNameMapper": {
"\\.(css|scss)$": "identity-obj-proxy"
}
}
Or the following to jest.config.ts:
moduleNameMapper: {
'\\.(css|scss)$': 'identity-obj-proxy'
}
This way (S)CSS module class names will be automatically retrieved in tests.
Here is the source.
npm i -D identity-obj-proxy
Add the below to file jest.config.js
moduleNameMapper: {
"\\.(css|less|scss|sass)$": "identity-obj-proxy"
}
File jest.config.js:
testEnvironment: 'jsdom',
The way I got away with this was by adding these two lines to my .babelrc file:
{
"presets": ["env", "react"],
"plugins": ["transform-class-properties"]
}
And my package.json file looks like this:
{
"name": "crud-redux",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "NODE_ENV=test jest",
"eject": "react-scripts eject"
},
"devDependencies": {
"babel-jest": "^18.0.0",
"babel-loader": "^6.4.1",
"babel-plugin-transform-decorators-legacy": "^1.3.5",
"enzyme": "^2.9.1",
"jest": "^23.1.0",
"react-addons-test-utils": "^15.6.2",
"react-test-renderer": "^15.6.2",
"redux-mock-store": "^1.5.1",
"webpack": "^1.15.0",
"webpack-dev-server": "^1.16.5"
}
}