Webpack compilation error when unit testing. I try to modify the test file but still so, it should be webpack configuration problems, test the error component error
Webpack compilation error when unit testing. I try to modify the test file but still so, it should be webpack configuration problems, test the error component error
webpack.test.config.js
const path = require('path');
const config = {
entry: {
vendor: ["babel-polyfill", "react", "react-dom"],
app: './index.js'
},
resolve: {
alias: {
'fonts': path.resolve(__dirname, 'public/fonts/'),
'#components': path.resolve(__dirname, 'src/components/'),
'#services': path.resolve(__dirname, 'src/services/'),
'#routes': path.resolve(__dirname, 'src/routes/'),
'#utils': path.resolve(__dirname, 'src/utils/'),
'#less': path.resolve(__dirname, 'src/less/'),
'#images': path.resolve(__dirname, 'public/images/'),
'#public': path.resolve(__dirname, 'public/'),
}
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-0']
}
}, {
test: /\.less$/,
exclude: /node_modules/,
loader: 'style-loader!css-loader!less-loader'
}]
}
};
module.exports = config;
Agent.spec.js
import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import Agent from '#components/agent/index';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
describe('<Agent />', () => {
it('Agent renderning', function () {
let app = shallow(<Agent/>);
expect(app.find('button').exists());
});
});
the error
WEBPACK Failed to compile with 1 error(s)
Error in ./test/unit/specs/Agent.spec.js
TypeError: Cannot read property 'babel' of undefined
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! # unit: `mocha-webpack --recursive test/unit/specs/*.js --webpack-config webpack.test.config.js`
npm ERR! Exit status 1
Related
I get this error when I try to run webpack but it doesnt really show me what module it cant find. I get this error 4 times in different files. I am starting webpack via commandline with "webpack". I dont see the point
ERROR in ./src/index.js
Module not found: Error: Can't resolve '' in 'C:\Users\topal\IdeaProjects\cts-abwesendheitstool\cts-abwesendheit-fe'
# ./src/index.js 4:0-21
This is my code in index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
ReactDOM.render(
<App/>,
document.getElementById('root')
);
my webpack config
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: path.resolve(__dirname, './src/index.js'),
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [[
'#babel/preset-env', {
targets: {
esmodules: true
}
}],
'#babel/preset-react']
}
}
},
{
test: /\.css$/,
loader: 'style-loader!css-loader!',
},
{
test: path.join(__dirname, '.'),
exclude: /(node_modules)/,
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env',
'#babel/react', {
'plugins': ['#babel/plugin-proposal-class-properties']
}]
}
},
{test: /\.json$/, loader: 'json-loader'}
],
},
resolve: {
extensions: ['*', '.js', '.jsx','.ts', '.tsx'],
},
output: {
path: path.resolve(__dirname, './dist'),
filename: 'bundle.js',
},
plugins: [new webpack.HotModuleReplacementPlugin()],
devServer: {
contentBase: path.resolve(__dirname, './dist'),
hot: true,
},
};
In a different file:
import "./App.css";
import Datagrid from "./pages/Datagrid"
import {React, useEffect, useState} from "react";
import * as config from "./config/config";
import DaterangePicker from "./components/DaterangePicker";
function App() {
const [data,setData]= useState([]);
useEffect(()=>{
const requestOptions = {
method: 'GET'
};
fetch(config.SERVER_URL + `/caldav/getEvents/${config.CALENDAR}`, requestOptions)
.then((response) => response.json())
.then(data => setData(data))
.catch(error => console.log(error));
},[data.length]);
return (
<div className="App">
<header className="App-header">
Abwesendheitscheckliste
</header>
<DaterangePicker/>
<Datagrid rows={data}></Datagrid>
</div>
);
}
export default App;
I get the same error:
ERROR in ./src/App.js
Module not found: Error: Can't resolve '' in 'C:\Users\topal\IdeaProjects\cts-abwesendheitstool\cts-abwesendheit-fe'
# ./src/App.js 2:0-19
# ./src/index.js
Can you try something like this in webpack
use: ["style-loader", "css-loader"],
I'm trying to make enzyme tests in react.
I make this simple test that mount a import component and check the states:
import React from 'react';
import { expect } from 'chai';
import { mount } from 'enzyme';
import WorkOutForm from './workOutForm';
describe('<WorkOutForm>', () => {
describe('workoutForm component', () => {
it('should start a new workoutForm with empty state', () => {
const component = mount(<WorkOutForm />);
expect(component).toEqual({})
expect(component.state().tempoGasto).toEqual(null)
expect(component.state().tipoAtividade).toEqual(null)
expect(component.state().data).toEqual(null)
component.unmount()
})
})
})
But when i run npm run test i get:
Jest encountered an unexpected token const component =
mount()
I try to make like the doc but i can't see my error.
Obs: i follow the jest getting started and i run:
npm i --save babel-jest #babel/core #babel/preset-env --dev
i added a babel.config.js file in the root with this content:
module.exports = {
presets: [
[
'#babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
and this is my webpack:
module: {
loaders: [{
test: /.js[x]?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react', '#babel/preset-env'],
plugins: ['transform-object-rest-spread']
}
}, {
test: /\.css$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
}, {
test: /\.woff|.woff2|.ttf|.eot|.svg*.*$/,
loader: 'file'
},
]
}
Please try adding the following in your package.json jest config:
"transform": {
"\\.js$": "<rootDir>/node_modules/babel-jest"
},
Make sure you install the babel-jest package first
I'm trying to get started with webpack and Grommet working together. I'm following this tutorial: https://github.com/grommet/grommet-standalone but I'm getting the following error:
ERROR in ./src/app/index.js
Module not found: Error: Can't resolve 'grommet/scss/vanilla/index' in '/home/john/Development/Work/Utilities/react_practice/test_app/src/app'
# ./src/app/index.js 31:0-37
# multi library
Clearly it's looking for the scss file files in the source directory rather than node_modules - but I have no idea whats causing the error or how to fix it.
I'm using this sass loader: https://github.com/jtangelder/sass-loader
And I'm using webpack 2.10 because of this: https://stackoverflow.com/a/39608145/1596288
Additionally, these are my webpack.config.babel.js and index.js files:
import webpack from 'webpack';
module.exports = {
entry: {
library: './src/app/index.js',
},
output: {
library: 'bundle',
libraryTarget: 'umd',
filename: 'bundle.js',
path: './public/dist'
},
devServer : {
inline: true,
contentBase: './public',
port: 8100
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react']
}
},
{
test: /\.scss$/,
loader: 'style!css!sass?outputStyle=compressed'
}
]
},
sassLoader: {
sourceMap: true,
includePaths: [
'./node_modules',
'./node_modules/grommet/node_modules'
]
}
}
and ...
import Header from 'grommet/components/Header';
import Title from 'grommet/components/Title';
import Box from 'grommet/components/Box';
import Search from 'grommet/components/Search';
import Menu from 'grommet/components/Menu';
import Anchor from 'grommet/components/Anchor';
import Actions from 'grommet/components/icons/base/Actions'
import 'grommet/scss/vanilla/index';
import React from 'react'
import { render } from 'react-dom'
const TesterComponent = () => (
<Header>
<Title>
Sample Title
</Title>
<Box flex={true}
justify='end'
direction='row'
responsive={false}>
<Search inline={true}
fill={true}
size='medium'
placeHolder='Search'
dropAlign={{"right": "right"}} />
<Menu icon={<Actions />}
dropAlign={{"right": "right"}}>
<Anchor href='#'
className='active'>
First
</Anchor>
<Anchor href='#'>
Second
</Anchor>
<Anchor href='#'>
Third
</Anchor>
</Menu>
</Box>
</Header>
)
render (
<TesterComponent />,
document.getElementById('root')
)
Managed to fix the issue by firstly modifiying my webpack.config.babel.js to the following:
import webpack from 'webpack';
module.exports = {
entry: {
library: './src/app/index.js',
},
output: {
library: 'bundle',
libraryTarget: 'umd',
filename: 'bundle.js',
path: './public/dist'
},
devServer : {
inline: true,
contentBase: './public',
port: 8100
},
resolve: {
extensions: ['', '.js', '.scss', '.css']
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react']
}
},
{
test: /\.scss$/,
loader: 'style-loader!css-loader!sass-loader?outputStyle=compressed'
}
]
},
sassLoader: {
includePaths: [
'./node_modules',
'./node_modules/grommet/node_modules'
]
}
}
Make sure you have the following libs installed too:
sudo npm install style-loader --save-dev
sudo npm install css-loader --save-dev
sudo npm install sass-loader --save-dev
try this
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style-loader', "css-loader!postcss-loader!sass-loader")
},
I am making a sample app using react, bower and webpack.
I have this index.jsx file:
import React from 'react';
import {ReactDOM} from 'react-dom';
import AwesomeComponent from './AwesomeComponent.jsx';
var App = React.createClass({
render: function() {
return (<p> Hello React!</p>);
}
});
ReactDOM.render(<App/>, document.getElementById('app'));
I installed the following:
npm i babel-loader babel-preset-es2015 babel-preset-react -S
My webpack.config.js is:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'src/client/public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
loaders : [
{
test : /\.jsx?/,
loader: 'babel',
include : APP_DIR,
query: {
presets: ['react', 'es2015']
}
}
]
};
module.exports = config;
My issues is that in the index.jsx file I am getting:
ERROR in ./src/client/app/index.jsx
Module parse failed: /path/to/file/src/client/app/index.jsx Unexpected token (7:12)
You may need an appropriate loader to handle this file type.
However, I've installed the bower packages and made bower the default loader.
Why does this still come up then?
You need to put your loaders config under a "module" key.
...
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?/,
loader: 'babel',
include : APP_DIR,
query: {
presets: ['react', 'es2015']
}
}]
}
...
This question already has answers here:
babel-loader jsx SyntaxError: Unexpected token [duplicate]
(8 answers)
Closed 6 years ago.
I am trying to build my app using wepback and getting getting stuck at this unexpected token error. All the dependencies for my project are fine and upto date I am using the same project somewhere else and it is building fine but when I try to build my current code which is same it gives the error, below are config.js and error descriptions
Here is my app.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import {Router} from 'react-router';
import Routes from '../routes/routes';
import injectTapEventPlugin from 'react-tap-event-plugin';
import { browserHistory } from 'react-router';
require('../Config');
injectTapEventPlugin();
window.EventEmitter = {
_events: {},
dispatch: function (event, data, returnFirstResult) {
if (!this._events[event]) return;
for (var i = 0; i < this._events[event].length; i++)
{
if(this._events[event][i])
{
var r = this._events[event][i](data);
if(returnFirstResult)
return r;
}
}
},
subscribe: function (event, callback, onlyOne) {
if (!this._events[event] || onlyOne) this._events[event] = []; // new event
this._events[event].push(callback);
}
};
// Render the main app react component into the app div.
// For more details see: https://facebook.github.io/react/docs/top-level-api.html#react.render
ReactDOM.render(<Router history={browserHistory}>{Routes}</Router>, document.getElementById('app'));
Here is my config.js
var webpack = require('webpack');
var path = require('path');
var buildPath = path.resolve(__dirname, '../project/public/js/');
var nodeModulesPath = path.resolve(__dirname, 'node_modules');
var TransferWebpackPlugin = require('transfer-webpack-plugin');
var config = {
entry: {
app: path.join(__dirname, '/app/entry_points/app.jsx'),
vendor: ['react', 'radium'],
},
resolve: {
extensions: ["", ".js", ".jsx"]
},
devtool: 'source-map',
output: {
path: buildPath,
publicPath: '/js/',
filename: 'mobile.[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin("vendor", "mobile.vendor.js"),
new webpack.DefinePlugin({}),
new webpack.NoErrorsPlugin(),
],
module: {
preLoaders: [
{
test: /\.(js|jsx)$/,
loader: 'eslint-loader',
include: [path.resolve(__dirname, "src/app")],
exclude: [nodeModulesPath]
},
],
loaders: [
{
test: /\.(js|jsx)$/,
loaders: [
'babel-loader'
],
exclude: [nodeModulesPath]
},
{
test: /\.css$/,
loader: "style-loader!css-loader"
},
]
},
eslint: {
configFile: '.eslintrc'
},
};
module.exports = config;
This is the error I get when I try to build:
ERROR in ./app/entry_points/app.jsx
Module build failed: SyntaxError: /home/zeus/Glide/project/project-mobile/app/entry_points/app.jsx: Unexpected token (46:16)
44 | // Render the main app react component into the app div.
45 | // For more details see: https://facebook.github.io/react/docs/top-level-api.html#react.render
> 46 | ReactDOM.render(<Router history={browserHistory}>{Routes}</Router>, document.getElementById('app'));
| ^
I am using react v0.14.8, react-dom v0.14.8, babel-loader ^6.2.1
i believe you need to specify the presets with babel and install the npm module babel-preset-react
loaders: [
{
test: /\.(js|jsx)$/,
loaders: [
'babel-loader'
],
exclude: [nodeModulesPath],
query: {
presets: ['react']
}
},
...
]
you'd also want to add es2015 to that presets array if you're using it.