mocha tests with ES6 style imports - javascript

I'm trying to run a Mocha test with some ES6 style imports in the file but I keep getting the error:
import assert from 'assert';
^^^^^^
SyntaxError: Unexpected identifier
I tried to invoke mocha with both
mocha --require #babel/register --recursive
and
mocha --require babel-register --recursive
but the error would not go away.
What is the correct way to run ES6 style Mocha tests?

For anyone coming from Google:
You can also install esm: npm i esm --save-dev or use your preferred package manager.
Then pass it as an argument to mocha: mocha 'index.test.js' --require esm

Try Below Code
import { strict as assert } from 'assert';
Or
import * as assert from 'assert';
Hope this helps

I found the answer to my question here -> https://dev.to/bnorbertjs/my-nodejs-setup-mocha--chai-babel7-es6-43ei
This package.json file
{
"name": "mochatest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha --require #babel/register --recursive"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.1.2",
"#babel/preset-env": "^7.5.4",
"#babel/register": "^7.4.4",
"mocha": "^6.1.4"
},
"dependencies": {}
}
together with this .babelrc
{
"presets": ["#babel/preset-env"]
}
solved my problem.

Related

How to prevent babel/cli not accessible error when deploying to Google Cloud?

I have a Node.js Google cloud function which I now try to modularize into two js modules. It works fine locally on my machine but when trying to deploy to Google I get the following error:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: npm ERR! #babel/cli not accessible from data-model
My main-function is using my data-model module via the following entry in package.json:
"dependencies": {
"#google-cloud/datastore": "^6.1.1",
"data-model": "file:../../data-model",
...
},
The data-model module in turn has the following package.json:
{
"name": "data-model",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "$(gcloud beta emulators datastore env-unset) && export GOOGLE_APPLICATION_CREDENTIALS=\"${GCPPRODFILEPATH}\" && jest --verbose --forceExit",
"build": "npx babel src --out-dir dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#google-cloud/datastore": "^6.6.2",
"#google-cloud/storage": "^5.16.1",
"cache-manager-redis-store": "^2.0.0",
"difference": "^1.0.2",
"gstore-node": "^7.2.6",
"ioredis": "^4.28.2",
"lodash": "^4.17.21"
},
"devDependencies": {
"#babel/cli": "^7.16.7",
"#babel/core": "^7.16.7",
"#babel/plugin-transform-modules-commonjs": "^7.16.5",
"#babel/plugin-transform-runtime": "^7.16.5",
"#babel/preset-env": "^7.16.5",
"jest": "^27.4.5"
}
}
and the index.js for data-model is just:
module.exports = Object.assign(
{},
require('./dist/garage'),
require('./dist/car'),
require('./dist/bike'),
require('./dist/util/cacheService'),
);
I'm very much a novice on npm and babel but my understanding was that when I build data-model to /dist babel would transform whatever needed and when I then use the module from main-function babel should be out of the picture.
Clearly my understanding is wrong, but where?
I finally managed to solve this, got a bit side-tracked by the babel/cli error message that CloudFunction threw.
The issue was that my data-model module was in another directory:
"data-model": "file:../../data-model",
I put it inside the main-function instead:
"data-model": "file:data-model",
and it works just fine to deploy to cloud functions.
Why it is so I have no clue but it is kind of documented in the Google Cloud Docs too:
The code for this local module should be stored somewhere other than the node_modules folder within your function's root directory.

Jest not running tests - Error while loading config - You appear to be using a native ECMAScript module configuration file

I'm having a problem configuring Jest to run my tests. When running npm test -- --watchAll --no-cache I get the message
babel.config.js: Error while loading config - You appear to be using a
native ECMAScript module configuration file, which is only supported
when running Babel asynchronously.
I've already checked other issues here on the OS, notably this one, but when I change the extension from babel.config to .cjs, the following message appears
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is
not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Details:
SyntaxError: <project_root>/src/filmes/gerenciador.js:
Unexpected reserved word 'await'. (13:24)
Below is my configuration files
package.json
{
"name": "myfilms",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.15.0",
"#babel/preset-env": "^7.15.0",
"babel-jest": "^27.0.6",
"jest": "^27.0.6",
"json-server": "^0.16.3"
},
"dependencies": {
"axios": "^0.21.1",
}
}
babel.config.js
module.exports = {
presets: [
[
"#babel/preset-env",
{
targets: {
node: "current",
},
},
],
],
};
Jest's and Babel's documentation is pretty confusing about these cases, and each of the solutions I try alternates between these two types of errors. Is there any standard way to configure Jest to support imports/exports and await/async?

can't transpile relative path of threejs with browserify and babel

i'm new at babel and browserify, I have a problem with transpile with browserify and babel, i've installed the threejs package and add
import * as THREE from 'three'
it works fine when i transpiled using command below
browserify input.js > output.js -t babelify
but when i add another import
import {GLTFLoader} from '../node_modules/three/examples/jsm/loaders/GLTFLoader'
and i used that command again, it doesn't work and it says
'import' and 'export' may appear only with 'sourceType: module' (1:0)
i've also added type module in the package.json but it still doesn't work, here is my package.json
{
"name": "three.js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"three": "^0.129.0"
},
"devDependencies": {
"#babel/core": "^7.14.5",
"#babel/preset-env": "^7.14.5",
"babelify": "^10.0.0"
},
"type": "module"
}
also here is my babel.config.json
{
"presets": ["#babel/preset-env"]
}
is there anything i need to add or change?
Importing the examples from the js folder would work
import {GLTFLoader} from './node_modules/three/examples/js/loaders/GLTFLoader'
// or depending on your path
import {GLTFLoader} from '../node_modules/three/examples/js/loaders/GLTFLoader'
The files in the jsm folder imports from three.module.js

npx ts-lint cannot find module 'typescript' when run in docker

I'm setting up a dockerized dev environment for node/typescript for an api project. The goal is to run everything in docker and not have any of installed node, npm or modules installed on the host. This is to isolate all versions of node and all modules from other projects.
./node
docker run \
-it \
-p "8080:80" \
--rm \
-w "/app" \
-v "$(pwd):/app" \
"node:10" "$#"
./npm
#!/bin/sh
./node npm $#
./npx
#!/bin/sh
./node npx $#
./package.json
{
"name": "testapi",
"version": "0.0.1",
"description": "a hello world api",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "npx ts-node src/app.ts",
"lint": "npx ts-lint --project src $#"
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"fastify": "^1.13.2",
"ts-node": "^7.0.1"
},
"devDependencies": {
"#types/node": "^10.12.15",
"ts-lint": "^4.5.1",
"typescript": "^3.2.2"
}
}
[edit]
I use ./npm install to build my node_modules. The node_modules is in a shared volume so it persists on the host after the container is removed. That way I don't need a Dockerfile to build an image.
[/edit]
When I run the lint command, I get the following error:
testapi$ ./npx ts-lint -i
10: Pulling from node
Digest: sha256:5af431757f84bf7878ff72447eb993fc37afcd975874fff13278157bf83661e6
Status: Image is up to date for docker-remote.registry.kroger.com/node:10
npx: installed 32 in 2.883s
Cannot find module 'typescript'
I think this has to do with module resolution, but I don't know this for sure. I see people install typescript globally, but that would mean I have to do a Dockerfile instead of using the stock node image. I don't mind using a Dockerfile for dev, but I think there should be a way to make this work without doing that.
So I figured out the answer. It wasn't obvious, and I stumbled upon it by accident.
I had installed ts-lint (see package.json above), and I saw an example which referenced tslint (without the hyphen).
So I removed ts-lint and installed tslint and it worked like a champ. I'm not sure what the difference is, but the one with the hyphen does not work in my project configuration. Also, the one without the hyphen installed a higher version number than the one with the hyphen.
See my new package.json containing the working dependency:
{
"name": "testapi",
"version": "0.0.1",
"description": "a hello world api",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "npx ts-node src/app.ts",
"lint": "npx tslint --project ./ 'src/**/*.ts?(x)' $#"
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"fastify": "^1.13.2",
"ts-node": "^7.0.1"
},
"devDependencies": {
"#types/node": "^10.12.15",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
}
}
This works when run in a docker container, just using the public node:10 image. It doesn't need a Dockerfile to install any global dependencies.
Try
yarn global add tslint typescript
or if it complained for permissions:
sudo yarn global add tslint typescript

The Lodash Install In Webpack Guides page

When I look this page they use npm install --save lodash to install lodash. But when I look this page, the package.json looks like this:
{
"name": "webpack-demo",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
===> "devDependencies": { <===
"webpack": "^4.0.1",
"webpack-cli": "^2.0.9",
"lodash": "^4.17.5" <- ???
}
}
Am I miss something?
No need to get confused. It's pretty simple.
use below command if you need to lodash in the production
npm install lodash --save
use below command if you don't need to lodash in the production
npm install lodash --save-dev
Once you are done. lodash's functions will be available globally. If you get any error like _ is not defined. Then you can import lodash like this
import _ from 'lodash'; OR let _ = require('lodash')

Categories