While unit React-native test files with Mocha, it gives error unexpected tocken - react-native

In a React-native project, I want to setup unit testing, For this I am using Mocha and Chai. When I run command npm test it executes mocha __tests__/*.js internally. In test file my code is following:
import { expect } from 'chai';
import Adapter from 'enzyme-adapter-react-16';
import { RefundedEmd } from './RefundedEmd';
import React from 'react';
import renderer from 'react-test-renderer';
describe('App', () => {
it('renders correctly', () => {
renderer.create(<App />);
});
});
I get following error in console:
C:\Technical work\Lorex\slf-mobile-impl\__tests__\App-test.js:1
import { expect } from 'chai';
^
SyntaxError: Unexpected token {
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 C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\lib\mocha.js:311:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\lib\mocha.js:308:14)
at Mocha.run (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\lib\mocha.js:849:10)
at Object.exports.singleRun (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\lib\cli\run-helpers.js:108:16)
at exports.runMocha (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\lib\cli\run-helpers.js:143:13)
at Object.exports.handler.argv [as handler] (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\lib\cli\run.js:305:3)
at Object.runCommand (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\node_modules\yargs\yargs.js:1087:28)
at Object.parse (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\lib\cli\cli.js:68:6)
at Object.<anonymous> (C:\Technical work\Lorex\slf-mobile-impl\node_modules\mocha\bin\mocha:133:29)
at Module._compile (internal/modules/cjs/loader.js:778:30)
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 Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! Test failed. See above for more details.

Related

Why npm can't find module '../../package.json' even if it already exists

The problem: when I start using npm commands I see the same error:
node:internal/modules/cjs/loader:933
const err = new Error(message);
^
Error: Cannot find module '../../package.json'
Require stack:
- C:\Users\Demian\AppData\Roaming\npm\node_modules\npm\lib\utils\unsupported.js
- C:\Users\Demian\AppData\Roaming\npm\node_modules\npm\lib\cli.js
- C:\Users\Demian\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\Users\Demian\AppData\Roaming\npm\node_modules\npm\lib\utils\unsupported.js:2:19)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\Demian\\AppData\\Roaming\\npm\\node_modules\\npm\\lib\\utils\\unsupported.js',
'C:\\Users\\Demian\\AppData\\Roaming\\npm\\node_modules\\npm\\lib\\cli.js',
'C:\\Users\\Demian\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js'
]
}
The same problem with npm i, -v, init and etc. The same error is when I start it from C:/user or in a project with package.json where all works before. node works correctly. What should I do?

Invalid shorthand property initializer error connecting to Mongo DB Atlas

I want to connect my project to Mongo DB Atlas. It shows the following error while connecting the Database:
me :~/Desktop/prod$ npm run server
> #0.1.0 server /home/me/Desktop/
> nodemon server.js
[nodemon] 2.0.4 [nodemon] to restart at any time, enter `rs` [nodemon] watching path(s): *.* [nodemon] watching extensions:
js,mjs,json [nodemon] starting `node server.js` Url/keys.js:2
mongoURI = "mongodb+srv://<username>:<password>#cluster0-ttxhq.mongodb.net/test"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Invalid shorthand property initializer
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
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 (internal/module.js:11:18)
at Object.<anonymous> (/home/saurabh/Desktop/prod/server.js:10:12)
at Module._compile (module.js:652:30)
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 Function.Module.runMain (module.js:693:10) [nodemon] app crashed - waiting for file changes before starting...
My code is :
const express = require("express")
const mongoose = require("mongoose")
const bodyParser = require("body-parser")
const app = express()
app.use(bodyParser.json())
const db = require("./config/keys").mongoURI
mongoose
.connect(db, {
useNewUrlParser: true,
useCreateIndex : true,
useUnifiedTopology : true
})
.then(()=>console.log("Database Connected"))
.catch(err => console.log(err))
const port = process.env.PORT || 5000
app.listen(port, ()=> console.log(`Server Starter on port ${port}`))
keys.js file is:*
modules.exports = {
mongoURI = "MONGODBURI"
}
Try:
modules.exports = {
mongoURI: "MONGODBURI"
}
This is a JSON so we cannot use '=' here.
have you tried IP whitelisting yourAtlas MongoDB cluster?
https://docs.atlas.mongodb.com/security-whitelist/
Medium tutorial

How to use ssr supported packages in vue with vue-server-renderer?

I tried to use vue2-google-maps and it worked well.
Suddenly my computer had stopped working and then I restarted my computer.
Then, strangely, 500-error comes out.
I reverted all codes associated with vue2-google-maps then my project works well.
Even though when I import vue2-google-maps then It occurs 500-error. :(
App.js
import * as VueGoogleMaps from 'vue2-google-maps';
It was working well but now cause of the unknown reason it has been broken...
Here is my console shows error.
webpack built 5410edae88d11b814c0b in 2690ms
error during render : /search
/media/bossminion/Work/WeMeet/frontend/node_modules/vue2-google-maps/dist/components/infoWindow.vue:3
<template>
^
SyntaxError: Unexpected token <
at Module._compile (internal/modules/cjs/loader.js:720:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:683:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.<anonymous> (/media/bossminion/Work/WeMeet/frontend/node_modules/vue2-google-maps/dist/main.js:42:19)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:683:19)
at require (internal/modules/cjs/helpers.js:16:16)
at r (/media/bossminion/Work/WeMeet/frontend/node_modules/vue-server-renderer/build.dev.js:9295:16)
at Object.<anonymous> (webpack:/external "vue2-google-maps":1:0)
at __webpack_require__ (webpack:/webpack/bootstrap a442baf8af813fadc2a4:25:0)
error during render : /favicon.ico
/media/bossminion/Work/WeMeet/frontend/node_modules/vue2-google-maps/dist/components/infoWindow.vue:3
<template>
^
SyntaxError: Unexpected token <
at Module._compile (internal/modules/cjs/loader.js:720:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:683:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.<anonymous> (/media/bossminion/Work/WeMeet/frontend/node_modules/vue2-google-maps/dist/main.js:42:19)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:683:19)
at require (internal/modules/cjs/helpers.js:16:16)
at r (/media/bossminion/Work/WeMeet/frontend/node_modules/vue-server-renderer/build.dev.js:9295:16)
at Object.<anonymous> (webpack:/external "vue2-google-maps":1:0)
at __webpack_require__ (webpack:/webpack/bootstrap a442baf8af813fadc2a4:25:0)
Is it possible that project that worked well could cause 500-error for no reason?
Environment: Ubuntu 18.04, npm v6.9.0, node v12.6.0
I have found how to handle ssr.
App.js
if (process.browser) {
const VueGoogleMaps = require('vue2-google-maps');
Vue.use(VueGoogleMaps, {
load: {
key: 'myKey',
libraries: 'places',
},
});
}
Instead of using import, I used require when it is client rendering.
I will be happy if it helped you. :D

How can I retry failed end to end tests in Detox using Mocha?

I have a flaky end-to-end test that uses Detox and Mocha.
The Mocha test runner allows for multiple retries. I tried using the recommended Mocha syntax:
// auth.spec.js
describe('App authentication', () => {
this.retries(2);
it('should have a login screen', async () => {
await expect(element(by.id('LoginScreenView'))).toBeVisible();
});
});
However, it appears this is an empty object ({}) and does not have a retries function when running Detox with Mocha as the test runner.
It generates the following error:
node_modules/.bin/mocha __e2e__ --opts __e2e__/config/mocha.opts --
configuration ios.sim.debug --grep :android: --invert --artifacts-location "__e2e__/artifacts/ios.sim.debug.2018-10-17 22-51-12Z"
/<path>/__e2e__/auth.spec.js:10
this.retries(2);
^
TypeError: this.retries is not a function
at Suite.describe (/<path>/__e2e__/auth.spec.js:4:8)
at Object.create (/<path>/node_modules/mocha/lib/interfaces/common.js:112:19)
at context.describe.context.context (/<path>/node_modules/mocha/lib/interfaces/bdd.js:40:27)
at Object.<anonymous> (/<path>/__e2e__/auth.spec.js:3:1)
at Module._compile (module.js:652:30)
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 (internal/module.js:11:18)
at /<path>/node_modules/mocha/lib/mocha.js:250:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/<path>/node_modules/mocha/lib/mocha.js:247:14)
at Mocha.run (/<path>/node_modules/mocha/lib/mocha.js:576:10)
at Object.<anonymous> (/<path>/node_modules/mocha/bin/_mocha:637:18)
at Module._compile (module.js:652:30)
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 Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
child_process.js:644
throw err;
^
Is there a way to automatically retry failed tests in Detox?
this in mocha can't work with arrow function, so must change it into ordinary one
// auth.spec.js
describe('App authentication', function () { // change it
this.retries(2);
It is not recommended best practice to use in Mocha
Ref:
https://mochajs.org/#arrow-functions
Hope it helps

jest-haste-map: watch error complaining that it can't find a file in the jest-haste-map node_modules folder

I just upgraded to RN 0.56.0 and am using jest version: 22.4.3 (updating the jest version doesn't help)
When I try to run my test suite, I get the following error in the terminal:
jest-haste-map: watch error: Error: ENOENT: no such file or directory,
open '/Users/mobilepractice/builds/0bcf5ed0/0/cx-mobile/posting-
plus/PostingPlus/node_modules/jest-haste-map/build/worker.js' at Error
(native) at Object.fs.openSync (fs.js:640:18) at Object.fs.readFileSync
(fs.js:508:33) at Module._extensions..js (module.js:578:20) at
Object.require.extensions.(anonymous function) [as .js]
(/Users/mobilepractice/builds/0bcf5ed0/0/cx-mobile/posting-
plus/PostingPlus/node_modules/babel-register/lib/node.js:152:7) 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)
and this error as well:
/Users/mobilepractice/builds/0bcf5ed0/0/cx-mobile/posting-
plus/PostingPlus/node_modules/react-native/jest/hasteImpl.js:50 ) /*:
string | void */ { ^ SyntaxError: Unexpected token ) at
Object.exports.runInThisContext (vm.js:76:16) 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/mobilepractice/builds/0bcf5ed0/0/cx-mobile/posting-
plus/PostingPlus/node_modules/jest-runtime/node_modules/jest-haste-
map/build/worker.js:16:19) at next (native)
I can see the worker.js file in my node_modules folder, which is why the ENOENT error is confusing.
A solution is to make sure node 8 or higher is installed. This error occurs on node 6.9.1.