I'm trying to learn how to do unit testing with React and jest. I've run into the following error:
Unexpected token import
Here are my babel presets:
{
"presets": ["es2015", "stage-0", "react"],
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
Here is the file i'm trying to test:
import jest from 'jest';
import React from 'react';
import { shallow } from 'enzyme';
import Step from '../src/components/step.js';
const wrapper = shallow(<Step />);
wrapper.find('a').simulate('click', { preventDefault() {} });
and here is my package.json:
{
"name": "react-simple-boilerplate",
"version": "1.0.0",
"description": "Simple and lightweight Boilerplate for ReactJS projects",
"scripts": {
"lint": "eslint src",
"start": "node server.js",
"test": "jest"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^0.28.0",
"enzyme": "^3.2.0",
"enzyme-adapter-react-16": "^1.1.0",
"eslint": "^3.19.0",
"eslint-plugin-react": "^6.10.3",
"jest": "^21.2.1",
"node-sass": "^4.5.2",
"react-addons-test-utils": "^15.6.2",
"sass-loader": "^6.0.3",
"style-loader": "^0.16.1",
"webpack": "^2.4.1",
"webpack-dev-server": "^2.4.4"
},
"dependencies": {
"babel-loader": "^7.0.0",
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
"main": "server.js"
}
Have no idea why its giving me this message?
It looks like babel-jest is missing among your dependencies. That's why jest is not running babel on your ES6+ code before executing tests.
I'm pretty sure you need to add your babel presets to test as well, jest sets the env variable to test.
So...
"test": {
"plugins": ["transform-es2015-modules-commonjs"],
"presets": ["es2015", "react", "stage-0"]
}
Related
I'm trying to server render a react app, I configured babel with preset-env in order to allow the use of import statement but it seems to be not working, ,when I launch the server I get this error :SyntaxError: Cannot use import statement outside a module
package.json :
"babel": {
"presets": [
"react",
"env",
"stage-2"
]
},
"devDependencies": {
"babel-eslint": "^7.2.3",
"eslint": "^3.19.0",
"eslint-plugin-react": "^6.10.3",
"jest": "^19.0.2"
},
"dependencies": {
"babel-cli": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.4.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"ejs": "^2.5.6",
"express": "^4.15.2",
"pm2": "^2.4.6",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"webpack": "^2.5.0"
server.js :
app.use("/", (req,res)=>{
const initialContent = serverRender();
res.render("index",{initialContent});
});
serverRender.js :
import React from "react";
import ReactDOMServer from "react-dom/server";
import App from "./components/app";
const serverRender =()=>{
return ReactDOMServer.renderToString(<App/>);
};
export default serverRender;
i am using babel on my server side my package.json has those babel related packages.
"scripts": {
"babel-node": "babel-node ",
"start": "nodemon --exec npm run babel-node -- ./app.js",
},
"devDependencies": {
"babel-polyfill": "^6.26.0",
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-latest": "^6.24.1",
"babel-register": "^6.26.0",
}
for preset i have created .babelrc file
{
"presets": ["env"],
"plugins": [
[
"transform-runtime",
{
"polyfill": false,
"regenerator": true
}
],
]
}
hope it helps
I am new to React and Jest and during my first experiment I got stuck with the following error when running jest --watchAll:
FAIL src/components/App.test.js
● Test suite failed to run
Cannot find module '#babel/preset-register' from '/Users/joaomatos/projects/dummy'
- If you want to resolve "#babel/register", use "module:#babel/register"
at Function.module.exports [as sync] (node_modules/resolve/lib/sync.js:74:15)
at resolveStandardizedName (node_modules/#babel/core/lib/config/files/plugins.js:101:31)
at resolvePreset (node_modules/#babel/core/lib/config/files/plugins.js:58:10)
at loadPreset (node_modules/#babel/core/lib/config/files/plugins.js:77:20)
at createDescriptor (node_modules/#babel/core/lib/config/config-descriptors.js:154:9)
at node_modules/#babel/core/lib/config/config-descriptors.js:109:50
at Array.map (<anonymous>)
at createDescriptors (node_modules/#babel/core/lib/config/config-descriptors.js:109:29)
at createPresetDescriptors (node_modules/#babel/core/lib/config/config-descriptors.js:101:10)
at presets (node_modules/#babel/core/lib/config/config-descriptors.js:47:19)
I installed several babel packages namely babel register, but I cannot find anything else related to #babel/preset-register. My package.json:
{
"name": "dummy",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/preset-env": "^7.7.1",
"acorn": "^7.1.0",
"babel-plugin-transform-export-extensions": "^6.22.0",
"babel-preset-env": "^1.7.0",
"history": "^4.10.1",
"npm-preset": "^1.5.0",
"parcel": "^1.12.4",
"react": "^16.11.0",
"react-app-polyfill": "^1.0.4",
"react-dev-utils": "^9.1.0",
"react-dom": "^16.6.3",
"react-dropzone": "^10.1.10",
"react-file-drop": "^0.2.8",
"react-flip-move": "^3.0.4",
"react-redux": "^6.0.0",
"react-router-dom": "^4.3.1",
"react-scripts": "3.2.0",
"react-switch": "^5.0.1",
"react-test-renderer": "^16.11.0",
"redux": "^4.0.4",
"typescript": "^3.7.2"
},
"scripts": {
"dev": "parcel src/index.html",
"build": "parcel build src/index.html",
"start": "npm run build && live-server dist",
"test": "jest --watchAll"
},
"devDependencies": {
"#babel/core": "^7.7.2",
"#babel/plugin-proposal-class-properties": "^7.7.0",
"#babel/preset-react": "^7.7.0",
"#babel/preset-stage-2": "^7.0.0",
"#babel/register": "^7.7.0",
"babel-loader": "^8.0.6",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1"
}
}
and .babel.rc:
{
"plugins": [
"#babel/plugin-proposal-class-properties"
],
"env": {
"test": {
"presets": [
"env",
"#babel/preset-env",
"#babel/preset-react",
"#babel/preset-stage-2",
"#babel/register",
"stage-2",
"react"
],
"plugins": [
"transform-export-extensions",
"transform-class-properties"
],
"only": [
"./**/*.js",
"node_modules/jest-runtime"
]
}
}
And my test:
import React from 'react';
import Enzyme, {shallow} from 'enzyme';
import EnzymeAdapter from 'enzyme-adapter-react-16';
import App from './App';
Enzyme.configure({adapter: new EnzymeAdapter()});
test(
'renders without crashing',
() => {
const wrapper = shallow(<App />);
expect(wrapper).toBeTruthy();
}
);
Does anyone know why I am getting this error?
Thank you for your help.
Babel-register is not a preset, hence the error message you are facing. Are you sure you need it for a starter project? Try removing it from the presets array in babel.rc
I'm using a very up-to-date (December 2017) stack of dependencies. As I try-out isomorphic react tests with Jest, the test suit keeps failing with the following error:
* Test suite failed to run
[BABEL] /__tests__/router.test.js: Plugin/Preset files are not allowed to
export objects, only functions.
Here are my dependencies:
"dependencies": {
"axios": "^0.17.1",
"babel-polyfill": "^6.26.0",
"cors": "^2.8.4",
"express": "^4.16.2",
"react": "^16.1.1",
"react-dom": "^16.1.1",
"react-router-dom": "^4.2.2"
},
"devDependencies": {
"#babel/core": "^7.0.0-beta.35",
"babel-cli": "^6.26.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^8.0.2",
"babel-jest": "^22.0.1",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"enzyme": "^3.2.0",
"enzyme-adapter-react-16": "^1.1.0",
"enzyme-to-json": "^3.2.2",
"eslint": "^4.11.0",
"eslint-plugin-react": "^7.5.1",
"html-webpack-plugin": "^2.30.1",
"jest": "^21.2.1",
"nodemon": "^1.11.0",
"parallelshell": "^3.0.2",
"react-test-renderer": "^16.2.0",
"regenerator-runtime": "^0.11.1",
"supertest": "^3.0.0",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.4"
},
"peerDependencies": {
"babel-core": "^7.0.0-0"
}
.babelrc :
{
"presets": [
"env",
"react",
]
}
Does anyone have any insights as to why Jest won't run?
{
"presets": [
"env",
"react"
],
"test": [
"jest"
]
}
Add the last block of code for "test" to you babel.rc
Here is my .babelrc code for reference
{
"presets": [
"env",
"react"
],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
],
"test": [
"jest"
]
}
Here is my output from the command-line
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 9.264s
Ran all test suites.
Done in 12.99s.
babel bridge is meant to cover any issues between 6 and 7
That is 100% not what the bridge package does. All it does is allow tools that use babel-core to pass through to #babel/core. The entire package is this single line of code.
If you are using #babel/core, you need to use plugins that work on Babel 7. That means babel-preset-react should be changed to #babel/preset-react and same for #babel/preset-env and your .babelrc should be:
{
"presets": [
"#babel/env",
"#babel/react",
]
}
Similarly, babel-polyfill should be #babel/polyfill.
None of this is well documented yet because Babel 7 is still an unstable beta.
I met the same challenge while configuring jest to work with babel-6.
See complete explanation here
But in summary, this combination of --devDependencies worked for me, and i set-up babel-jest to transform my **.js files:
// package.json
"devDependencies": {
"babel-core": "6.26.0",
"babel-jest": "21.2.0",
"babel-loader": "7.1.2",
"babel-preset-env": "1.6.0",
"babel-preset-react": "6.24.1",
"babel-preset-stage-0": "6.24.1",
"jest": "21.2.1",
"webpack": "3.6.0"
},
"jest": {
"transform": {
"^.+\\.jsx?$": "babel-jest"
}
}
// .babelrc
{
"presets": [
"env",
"stage-0",
"react"
]
}
I ejected from create-react-app and I am having problems using import type when I run the test site (using yarn start).
Module parse failed: /project/src/web/MarkdownField.js Unexpected token (6:12)
You may need an appropriate loader to handle this file type.
When I use yarn flow, the process completes without errors.
.babelrc
{
"presets": ["flow", "es2015", "react", "stage-2"]
}
.flowconfig
[libs]
./flow-typed
[options]
esproposal.class_static_fields=enable
esproposal.class_instance_fields=enable
[include]
./src/**
mdTypes.types.js
// #flow
import { propTypes } from 'redux-form';
export type FieldType = {
field: propTypes.fieldPropTypes
};
MarkdownField.js
// #flow
import React from 'react';
import PropTypes from 'prop-types';
import MDE from 'medium-editor';
import MeMarkdown from 'medium-editor-markdown';
import type { FieldType } from '../mdTypes.types';
class MarkdownField extends React.PureComponent<void, FieldType> {
package.json
{
"name": "project",
"version": "0.0.1",
"private": false,
"dependencies": {
"medium-editor": "^5.23.2",
"medium-editor-markdown": "^2.6.0",
"react-markdown": "^2.5.0",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-form": "^7.0.4"
},
"devDependencies": {
"autoprefixer": "7.1.2",
"babel-cli": "^6.26.0",
"babel-core": "6.25.0",
"babel-eslint": "7.2.3",
"babel-jest": "20.0.3",
"babel-loader": "7.1.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-flow": "^6.23.0",
"babel-preset-react": "^6.24.1",
"babel-preset-react-app": "^3.0.3",
"babel-preset-stage-2": "^6.24.1",
"babel-runtime": "6.26.0",
"chalk": "1.1.3",
"eslint": "4.4.1",
"eslint-config-react-app": "^2.0.1",
"eslint-loader": "1.9.0",
"eslint-plugin-flowtype": "^2.37.0",
"eslint-plugin-import": "2.7.0",
"eslint-plugin-jsx-a11y": "5.1.1",
"eslint-plugin-react": "7.1.0",
"flow-bin": "^0.56.0",
"jest": "20.0.4",
"react": "^16.0.0",
"react-dev-utils": "^4.1.0",
"react-dom": "^16.0.0"
},
"scripts": {
"start": "node scripts/start.js",
"test": "node scripts/test.js --env=jsdom",
"build": "flow-remove-types src/ -d lib/",
"prepublish": "yarn run build"
},
"jest": {
"collectCoverageFrom": ["src/**/*.{js,jsx}"],
"setupFiles": ["<rootDir>/config/polyfills.js"],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.js?(x)",
"<rootDir>/src/**/?(*.)(spec|test).js?(x)"
],
"testEnvironment": "node",
"testURL": "http://localhost",
"transform": {
"^.+\\.(js|jsx)$": "<rootDir>/node_modules/babel-jest"
},
"transformIgnorePatterns": ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"],
"moduleNameMapper": {
"^react-native$": "react-native-web"
},
"moduleFileExtensions": ["web.js", "js", "json", "web.jsx", "jsx", "node"]
}
}
Any suggestions?
We have a similar setup and had the same error. You may need to add e plugin for the eslint check in .eslintrc like this:
{
"extends": ["plugin:flowtype/recommended"],
"plugins": [
"flowtype"
]
}
Please refer import that is causing error.It compiles without any issue if I comment this line.
import React, {Component} from 'react';
import {returnCurrencies} from '../reducers/reducer_currency';
import {DropdownMenu, DropdownMenuList, DropdownMenuListItem} from 'react-lds';
export default class CurrencyDropDown extends Component
{
constructor(props)
{
super(props);
//console.log("props",props);
}
renderCurrencies(curr)
{
//console.log("Curr ", curr);
return (<option key={curr} value={curr}>{curr}</option>);
}
render()
{
if(!this.props.currencies)
{
return (<div></div>);
}
let currencyList = returnCurrencies(this.props.currencies);
return (<div>
<select value={this.props.initValue} onChange=
{this.props.selectCurrency}>
{currencyList.map(this.renderCurrencies)}
</select>
</div>);
}
}
Package.json
{
"name": "redux-simple-starter",
"version": "1.0.0",
"description": "Simple starter package for Redux with React and Babel support",
"main": "index.js",
"repository": "",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
"test:watch": "npm run test -- --watch"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "^0.16.2",
"babel-preset-stage-1": "^6.1.18",
"lodash": "^3.10.1",
"prop-types": "^15.5.10",
"react": "^0.14.9",
"react-dom": "^0.14.9",
"react-lds": "^1.1.11",
"react-redux": "4.3.0",
"react-router": "^2.0.1",
"redux": "^3.0.4",
"redux-promise": "^0.5.3"
}
}
Failed to compile with below error.
ERROR in ./~/moment-timezone/data/packed/latest.json
Module parse failed: C:..\XXXX\node_modules\moment-timezone\data\packed\latest.json Unexpected token (2:10)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:10)
at Parser.pp$4.raise (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:2221:15)
at Parser.pp.unexpected (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:603:10)
at Parser.pp.semicolon (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:581:61)
at Parser.pp$1.parseExpressionStatement (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:966:10)
at Parser.pp$1.parseStatement (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:730:24)
at Parser.pp$1.parseBlock (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:981:25)
at Parser.pp$1.parseStatement (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:709:33)
at Parser.pp$1.parseTopLevel (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:638:25)
at Parser.parse (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:516:17)
at Object.parse (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:3098:39)
# ./~/moment-timezone/index.js 2:15-51
It fails because it can not load the JSON file. You need to configure webpack to load the JSON files:
Install the json-loader:
npm install --save-dev json-loader
And adjust your webpack config loaders section to use something like this:
rules: [
...
{
test: /\.json$/,
use: 'json-loader'
}
...
]
Given that you are using the webpack v1 config may look a bit different to you.