mocha describe 'unexpected string error' - javascript

I am writing the below test cases using Mocha in node.js. Please find the below code :
describe('Car', function() {
it('can park?',function()
{
const car = new Car();
assert.equal(car.park(),'stopped'); }); });
I am running this using following command :
npm run test
It populates following error :
> inbox#1.0.0 test /Users/abc/test/
> mocha
/Users/abc/test/Inbox.test.js:18
describe('Car', function() {
^^^^^
SyntaxError: Unexpected string
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:670:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
Can you please help me, what i am doing wrong here?
Note : package.json is also updated with mocha in scripts field.

I know i am answering to my own question but its more like recording it for others.
Just Use following statement in your code at top or before describe statements
"use strict";

Related

NPM is giving me an Error with every command

Whenever I'm running a command with npm. it's giving an Error. A few days ago, It was totally fine. I used it in some of my projects but now it is can't do nothing.
node:internal/modules/cjs/loader:941
const err = new Error(message);
^
Error: Cannot find module '../../package.json'
Require stack:
- C:\Users\nuur hassan\AppData\Roaming\npm\node_modules\npm\lib\utils\unsupported.js
- C:\Users\nuur hassan\AppData\Roaming\npm\node_modules\npm\lib\cli.js
- C:\Users\nuur hassan\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:941:15)
at Function.Module._load (node:internal/modules/cjs/loader:774:27)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at Object.<anonymous> (C:\Users\nuur hassan\AppData\Roaming\npm\node_modules\npm\lib\utils\unsupported.js:2:19)
at Module._compile (node:internal/modules/cjs/loader:1109:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Module.require (node:internal/modules/cjs/loader:1013:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\nuur hassan\\AppData\\Roaming\\npm\\node_modules\\npm\\lib\\utils\\unsupported.js',
'C:\\Users\\nuur hassan\\AppData\\Roaming\\npm\\node_modules\\npm\\lib\\cli.js',
'C:\\Users\\nuur hassan\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js'
]
}
The error says that npm can't find the package.json file. Make sure you haven't deleted it. If you want to create one you can use npm init to initialize one in the current directory. Even if that fails I'd reinstall node js and double check the NODE_PATH.

export default data SyntaxError: Unexpected token export during bulding on next.js using typescript

Code available here =>
https://codesandbox.io/s/sweet-mcclintock-dhczx?file=/pages/index.js
Initial error when trying to use #iconify-icons/cryptocurrency with next.js and typescript (it happens only when in typescript).
SyntaxError: Unexpected token export
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at eval (webpack-internal:///#iconify-icons/cryptocurrency/usdt:1:18)
at Object.#iconify-icons/cryptocurrency/usdt (/sandbox/.next/server/pages/index.js:409:1)
at __webpack_require__ (/sandbox/.next/server/pages/index.js:23:31)
at eval (webpack-internal:///./pages/index.js:9:92)
at Module../pages/index.js (/sandbox/.next/server/pages/index.js:398:1)
at __webpack_require__ (/sandbox/.next/server/pages/index.js:23:31)
at /sandbox/.next/server/pages/index.js:91:18
at Object.<anonymous> (/sandbox/.next/server/pages/index.js:94:10)
at Module._compile (internal/modules/cjs/loader.js:778:30)
/sandbox/node_modules/#iconify-icons/cryptocurrency/usdt.js:6
export default data;
The problem does not happen with all libraries. It seems to me that during the transpilation babel has a problem with the library syntax.
Then I've tried to transpile the lib next-transpile-modules to fix the problem using the code below.
const withTM = require("next-transpile-modules")([
"#iconify-icons/cryptocurrency"
]);
module.exports = withTM();
But a new problem has happened.
Error: next-transpile-modules - an unexpected error happened when trying to resolve "#iconify-icons/cryptocurrency"
Error: Can't resolve '#iconify-icons/cryptocurrency' in '/sandbox'
at getPackageRootDirectory (/sandbox/node_modules/next-transpile-modules/src/next-transpile-modules.js:87:11)
at Array.map (<anonymous>)
at generateModulesPaths (/sandbox/node_modules/next-transpile-modules/src/next-transpile-modules.js:99:33)
To conclude, transpiling the module is the way I'm trying to achieve the main goal. It might not be necessary.
The way the #iconify-icons/cryptocurrency library is exported means you need to transpile each icon package you use individually.
const withTM = require("next-transpile-modules")([
"#iconify-icons/cryptocurrency/usdt"
]);
module.exports = withTM();

Realm-js throws exception

I'm trying to write a little test of realm-js using node.js console, but after insertion of te first string I see an exception:
> var Realm = require('realm')
TypeError: utf8 is not a function
at Function.from (native)
at Function.from (native)
at module.exports (/home/hermann/node_modules/realm/lib/submit-analytics.js:92:60)
at Object.<anonymous> (/home/hermann/node_modules/realm/lib/index.js:70:42)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
Anybody can help me with this problem?
UPD. An error occures in realm/lib/submit-analytics.js in this call:
request(`https://api.mixpanel.com/track/?data=${Buffer.from(JSON.stringify(payload), 'utf8').toString('base64')}&ip=1`,
() => { /* Analytics failed. Do nothing. */ });
That's a bug, you must be using Node.js 4.x? I've created an issue to track this -- for now you can just comment out that entire line to make it work for you.

I cannot use import in Jasmine2 spec when using protractor 4.0.9 when using typescript 2.0

When I try to use typescript .ts spec and run them in protractor, I get:
[17:05:52] E/launcher - Error: SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at C:\Users\gwk736\Gitlab\inform-locate-E2E-tests\node_modules\jasmine\lib\jasmine.js:84:5
at Array.forEach (native)
at Jasmine.loadSpecs (C:\Users\gwk736\Gitlab\inform-locate-E2E-tests\node_modules\jasmine\lib\jasmine.js:83:18)
[17:05:52] E/launcher - Process exited with error code 100
When I have .ts file and no import is present it's behaving ok. Why ?
) Get the same error, using: Jasmine2, Protractor 4.0.11, typescript 2.0.3
And I don't know what seems to be the problem. But my workaround is:
in file: main-page.ts
module.exports = {
setUrl: function(){
browser.get("/");
}
};
and in file you want to use the function setURL for example:
var mainPage = require('./main-page.ts');
describe('Test Main Page', function() {
it('should have a title', function() {
mainPage.setUrl();
expect(browser.getTitle()).toEqual('Titel meiner Seite');
});
});
That's just my workaround - actually I didn't find out the real reason for the problem you described!

terminal errors when running gulp

I am getting these errors when i run gulp command. what is causing this?
SyntaxError: Unexpected token :
at exports.runInThisContext (vm.js:54:16)
at Module._compile (module.js:375:25)
at Object.Module._extensions..js (module.js:406:10)
at Module.load (module.js:345:32)
at Function.Module._load (module.js:302:12)
at Module.require (module.js:355:17)
at require (internal/module.js:13:17)
at execute (/usr/local/lib/node_modules/gulp-cli/lib/versioned/^3.7.0/index.js:23:3)
at Liftoff.handleArguments (/usr/local/lib/node_modules/gulp-cli/index.js:150:63)
at Liftoff.<anonymous> (/usr/local/lib/node_modules/gulp-cli/node_modules/liftoff/index.js:192:16)
That looks like a : instead of ; in your gulpfile.js on line 29.

Categories

Resources