I"m trying to do a simple Mocha test, which works, but once i start trying to add my own components for testing, it is unable to resolve the module.
package.json:
{
"name": "My Project",
"version": "0.1.0",
"scripts": {
"test": "mocha \"./src/**/*.spec.js\""
},
"dependencies": {
"react": "^0.14.7",
"react-dom": "^0.14.7",
"webpack": "^1.12.12"
},
"devDependencies": {
"bower": "^1.4.1",
"expect": "^1.14.0",
"expect-jsx": "^2.3.0",
"jsx-loader": "^0.13.2",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7"
}
}
src/dashboard/TestComponent.jsx:
var React = require('react');
var TestComponent = React.createClass({
render: function() {
return (
<div />
);
}
});
module.exports = TestComponent;
src/dashboard/test.spec.js:
var React = require("react");
var ReactTestUtils = require('react-addons-test-utils');
var expect = require("expect");
var expectJSX = require("expect-jsx");
expect.extend(expectJSX);
var TestComponent = require('./TestComponent');
describe('Test', function() {
it('should work', function() {
expect(true).toEqual(true)
});
});
If i remove the line var TestComponent = require('./TestComponent.jsx');, my spec passes. But once i try to require my component, it gives me this error:
$ npm test
>My Project#0.1.0 test C:\workspace\root
>mocha "./src/**/*.spec.js"
module.js:338
throw err;
^ Error: Cannot find module './TestComponent'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (C:\workspace\root\src\dashboard\test.spec.js:7:22)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at C:\workspace\root\node_modules\mocha\lib\mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (C:\workspace\root\node_modules\mocha\lib\mocha.js:216:14)
at Mocha.run (C:\workspace\root\node_modules\mocha\lib\mocha.js:468:10)
at Object.<anonymous> (C:\workspace\root\node_modules\mocha\bin\_mocha:403:18)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3 npm ERR! Test failed. See above for more details.
Mocha probably can't read your TestComponent because it contains syntax unfamiliar to it (the JSX). You'll need to specify a compiler for mocha as well as babel presets (assuming you're using Babel).
Assuming you already have babel-core and babel-preset-react installed for your project, make these changes in your package.json:
"scripts": {
"test": "mocha --compilers js:babel-register \"./src/**/*.spec.js\""
},
"babel": {
"presets": ["react"]
}
You should add the .jsx extension to your require call, as by default it won't recognize .jsx files as modules.
Rick is also right about the compiler but in this case the error is due to the synthax of require. Then if you fix the require you will see an error of unexpected token, which is the lack of compiler.
You should therefore correct both points.
Related
Issue
Hello,
I am trying to use SnapSVG in my react project, but I cannot get it to run in the first place. Could someone help me with the proper settings? I am not very familiar with webpack so detailed instructions would be very much appreciated.
error
index.js?46cb:602 Uncaught ReferenceError: window is not defined
at Object.<anonymous> (FILEPATH/node_modules/snapsvg/dist/snap.svg.js:485:3)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.snapsvg (FILEPATH/.next/server/pages/index.js:1291:18)
at
**webpack_require**
(FILEPATH/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///./src/components/domain/top-first-view/index.tsx:9:65)
import in React components
import * as Snap from 'snapsvg';
package.json
"dependencies": {
"next": "^13.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sanitize.css": "^13.0.0",
"sass": "^1.56.1"
},
"devDependencies": {
"typescript": "^4.8.4",
"imports-loader": "^4.0.1",
"snapsvg": "^0.5.1",
"#types/snapsvg": "^0.5.2",
"cjs-loader": "^0.1.0",
...
},
"volta": {
"node": "18.6.0",
"yarn": "1.22.19"
}
what I've tried so far
I have tried using imports-loader as is discussed in this Github thread, but this resulted in a new error.
new error after webpack settings
Uncaught Error: ENOENT: no such file or directory, open 'FILEPATH/.next/server/pages/index.js'
at Object.openSync (node:fs:594:3)
at Object.readFileSync (node:fs:462:35)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1143:18)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.requirePage (FILEPATH/node_modules/next/dist/server/require.js:88:12)
at <unknown> (FILEPATH/Documents/GitHub/toda-web-art/node_modules/next/dist/server/load-components.js:37:73)
at async Object.loadComponents (FILEPATH/node_modules/next/dist/server/load-components.js:37:26)
webpack config in next.config.js
webpack(config) {
config.module.rules.push(
{
test: require.resolve('snapsvg'),
use: 'imports-loader?this=>window,fix=>module.exports=0',
},
...
Thank you!
I'm trying to locally run and debug my client's site but I cannot get it to start locally. I tried updating npm but with no sucess. This site was made with create react app. I am not sure if something wrong is with the code, with my npm or maybe I'm not seeing something very obvious.
I use git clone, then npm i, then npm start and this happens:
compiler.hooks.done.tap('done', async stats => {
^^^^^
SyntaxError: missing ) after argument list
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/filipsikora/Downloads/avrio-shop2-react-1.1/node_modules/react-scripts/scripts/start.js:45:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! shop_2.0#0.1.0 start: `react-app-rewired start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the shop_2.0#0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
I know that it highlights the word async. I tried to update npm in many ways. Maybe I screwed something up and not updated it correctly.
Here is my package.json just in case
{
"name": "shop_2.0",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.1",
"bootstrap": "^4.5.3",
"firebase": "^6.6.2",
"node-sass": "^4.14.1",
"raven-js": "^3.27.2",
"react": "^16.14.0",
"react-app-rewire-disable-chunks": "0.0.1",
"react-app-rewired": "^2.1.6",
"react-dom": "^16.14.0",
"react-google-recaptcha": "^1.1.0",
"react-horizontal-scrolling-menu": "^0.7.8",
"react-localization": "^1.0.15",
"react-redux": "^6.0.1",
"react-redux-toastr": "^7.6.5",
"react-router-dom": "^5.2.0",
"react-scripts": "2.1.8",
"react-scrollbar": "^0.5.6",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-app-rewired start",
"watch": "node scripts/build-watch.js",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"eslint-plugin-react": "^7.21.5"
},
"homepage": "/plugin/shop-app"
}
Im trying to deploy a symfony application on my Ubuntu server.
Mind the following details.
It is a simple application but some yarn packages seem to be failing.
What can I do?
Im working with Symfony 5.
My package.json
{
"devDependencies": {
"#symfony/webpack-encore": "^0.30.2",
"bootstrap": "^4.5.2",
"jquery": "^3.5.1",
"node-sass": "^4.14.1",
"popper.js": "^1.16.1",
"webpack-notifier": "^1.6.0"
},
"dependencies": {
"add": "^2.0.6",
"copy-webpack-plugin": "^6.0.3",
"sass-loader": "^8.0.0",
"yarn": "^1.22.4"
},
"name": "fluid-cms",
"version": "1.0.0",
"main": "index.js",
"repository": "---",
"author": "---",
"license": "MIT"
}
The following output comes out.
I have tried many things.
Ubuntu 18.04.
alpha#ovhBrains:/var/www/fluid-cms$ sudo yarn encore prod
yarn run v1.22.4
$ /var/www/fluid-cms/node_modules/.bin/encore prod
Running webpack ...
/var/www/fluid-cms/node_modules/copy-webpack-plugin/node_modules/p-limit/index.js:30
} catch {}
^
SyntaxError: Unexpected token {
at NativeCompileCache._moduleCompile (/var/www/fluid-cms/node_modules/v8-compile-cache/v8-compile-cache.js:242:18)
at Module._compile (/var/www/fluid-cms/node_modules/v8-compile-cache/v8-compile-cache.js:186:36)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (/var/www/fluid-cms/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
at Object.<anonymous> (/var/www/fluid-cms/node_modules/copy-webpack-plugin/dist/index.js:10:38)
at Module._compile (/var/www/fluid-cms/node_modules/v8-compile-cache/v8-compile-
d.
---
Another error:
#symfony/webpack-encore#0.30.2: The engine "node" is incompatible with this module. Expected version ">= 10.13.0". Got "8.10.0"
Once I found out the second error log it was easy to fix.
My nodejs -v was not matching with the requirements.
I'm a bit puzzled why two different npm installs for a react-native calendar are breaking the solution. I am running react-native through expo, and it runs fine before installing the calendar.
But after installing either
react-native-general-calendars
or
react-native-calendars
I get this error in the console after running
expo start:
> [17:30:29] Starting project at --location--
[17:30:33] Error: React native is not installed. Please run `npm install` in your project directory.
[17:30:33] Couldn't start project. Please fix the errors and restart the project.
[17:30:33] Set EXPO_DEBUG=true in your env to view the stack trace.
I run npm install and get the following error:
> [17:33:34] Starting project at --location--
[17:33:40] --location--\node_modules\react-native\local-cli\server\checkNodeVersion.js:43
[17:33:40] );
[17:33:40] ^
[17:33:40] SyntaxError: Unexpected token )
[17:33:40] at createScript (vm.js:56:10)
[17:33:40] at Object.runInThisContext (vm.js:97:10)
[17:33:40] at Module._compile (module.js:542:28)
[17:33:40] at Object.Module._extensions..js (module.js:579:10)
[17:33:40] at Module.load (module.js:487:32)
[17:33:40] at tryModuleLoad (module.js:446:12)
[17:33:40] at Function.Module._load (module.js:438:3)
[17:33:40] at Module.require (module.js:497:17)
[17:33:40] at require (internal/module.js:20:19)
[17:33:40] at Object.<anonymous> (--location--\node_modules\react-native\local-cli\cli.js:17:1)
[17:33:40] Metro Bundler process exited with code 1
[17:33:40] Set EXPO_DEBUG=true in your env to view the stack trace.
Here is my package.json:
{
"name": "my-new-project",
"main": "node_modules/expo/AppEntry.js",
"private": true,
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject",
"test": "node ./node_modules/jest/bin/jest.js --watchAll"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"#expo/samples": "2.1.1",
"expo": "^29.0.0",
"react": "^16.4.2",
"react-native": "^0.56.0",
"react-native-general-calendars": "^1.7.2",
"react-navigation": "^2.9.3"
},
"devDependencies": {
"jest-expo": "^29.0.0"
}
}
When I uninstall the calendar I still get the error now. Not sure how to get expo to stop throwing the error. Anyone know how to fix this?
I can't seem to test my files anymore.
I "npm i react react-dom react-addon-test-utils"
but even after all those installs,
it tells me i'm missing something
i even tried to install both devdep. and dep.
module.js:339
throw err;
^
Error: Cannot find module 'react-dom/lib/ReactTestUtils'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/react-addons-test-utils/index.js:1:80)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at /Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/react-compat.js:92:17
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/react-compat.js:146:5)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/Utils.js:29:20)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/MountedTraversal.js:32:14)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/ReactWrapper.js:23:25)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/index.js:15:21)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (AddNewSong.spec.js:5:28)
at Module._compile (module.js:425:26)
at loader (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:128:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:138:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at /Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/mocha/lib/mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/mocha/lib/mocha.js:216:14)
at Mocha.run (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/mocha/lib/mocha.js:468:10)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
npm ERR! Test failed. See above for more details.
this is the package.json
{
"name": "KMR",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"start": "node app.js",
"build": "webpack",
"watch": "webpack --watch",
"test": "mocha --require test/helpers/setup.js",
"lint": "eslint --ignore-path .gitignore --cache ./",
"cover": "nyc --reporter=lcov --reporter=text --reporter=html --require babel-register --extension .jsx npm test"
},
"license": "MIT",
"dependencies": {
"axios": "0.9.1",
"bluebird": "^3.4.6",
"errorhandler": "^1.4.3",
"express": "4.13.4",
"express-favicon": "^2.0.0",
"express-jwt": "^5.1.0",
"lodash": "4.6.1",
"method-override": "^2.3.6",
"mysql": "^2.11.1",
"node-ensure": "0.0.0",
"pg": "^6.1.0",
"pg-hstore": "^2.3.2",
"pg-promise": "^5.3.4",
"react": "0.14.7",
"react-dom": "0.14.7",
"react-modal": "^1.5.2",
"react-redux": "4.4.0",
"react-responsive": "^1.2.5",
"react-router": "2.0.0",
"redux": "3.3.1",
"sequelize": "^2.1.3",
"sequelize-cli": "^2.4.0",
"sqlite3": "^3.1.6",
"webpack": "^1.13.2",
"webpack-dev-middleware": "^1.8.4",
"webpack-hot-middleware": "^2.13.0"
},
"devDependencies": {
"babel-core": "6.5.2",
"babel-loader": "6.2.2",
"babel-polyfill": "6.7.4",
"babel-preset-es2015": "6.5.0",
"babel-preset-react": "6.5.0",
"babel-register": "6.5.2",
"chai": "3.5.0",
"enzyme": "2.0.0",
"eslint": "2.2.0",
"eslint-config-standard": "5.1.0",
"eslint-config-standard-jsx": "1.1.1",
"eslint-config-standard-react": "2.3.0",
"eslint-loader": "1.3.0",
"eslint-plugin-promise": "1.0.8",
"eslint-plugin-react": "4.1.0",
"eslint-plugin-standard": "1.3.2",
"gulp": "^3.9.1",
"gulp-connect": "^5.0.0",
"gulp-sass": "^2.3.2",
"jsdom": "8.0.4",
"json-loader": "0.5.4",
"livereload": "^0.6.0",
"mocha": "2.4.5",
"nyc": "6.0.0",
"react": "^0.14.7",
"react-addons-test-utils": "^15.4.1",
"react-dom": "^0.14.7",
"sinon": "^1.17.3",
"supertest": "^2.0.1",
"webpack": "1.13.2"
},
"directories": {
"test": "test"
},
"keywords": [
"react",
"workshop",
"intro",
"redux"
]
}
I found a work around for this from here
make sure that your app contains follow dependencies all in the same
version in package.json
react-addons-test-utils
react
react-dom
"react": "^0.14.7",
"react-addons-test-utils": "^0.14.7",
"react-dom": "^0.14.7",
or upgrade to latest versions of above dependencies
For anyone who has run into this problem lately, note that react-addons-test-utils have been deprecated and all the test methods have been moved into react-dom.
So the fix looks like:
- Remove react-addons-test-utils from package.json and run npm update.
- Change the import statements to reflect the updates. For example:
import { renderIntoDocument } from 'react-dom/test-utils';
instead of the old
import { renderIntoDocument } from 'react-addons-test-utils';