TestsNodejs - compilers deprecation in mocha and babel - node.js

I'm building api rest on node with integration test (Babel, chai, mocha)
I changed
--compilers js:babel-core/register
for
--require babel-core/register
as recommended by the documentation:
https://github.com/mochajs/mocha/wiki/compilers-deprecation
But when I make this change in my mocha.opts file, the error appears:
C:\Users\Ranulfo\Desktop\noderest>npm run test-integration
> noderest#1.0.0 test-integration C:\Users\Ranulfo\Desktop\noderest
> mocha --opts test/integration/mocha.opts test/integration/*.js
C:\Users\Ranulfo\Desktop\noderest\test\integration\helpers.js:1
(function (exports, require, module, __filename, __dirname) { import supertest f
rom 'supertest';
^^^^^^
SyntaxError: Unexpected token import
For more details of the code:
mocha.opts(test/integration/mocha):
--require test/integration/helpers.js
--reporter spec
--require babel-core/register
--slow 5000
helpers.js (test/integration/helpers.js)
import supertest from 'supertest';
import chai from 'chai';
import app from '../../app';
global.app = app;
global.request = supertest(app);
global.expect = chai.expect;
package.json
{
"name": "noderest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "babel-node ./index.js",
"test": "echo \"Error: no test specified\" && exit 1",
"test-integration": "mocha --opts test/integration/mocha.opts test/integration/*.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-node6": "^11.0.0",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.26.0",
"chai": "^4.1.2",
"mocha": "^5.2.0",
"supertest": "^3.1.0"
},
"dependencies": {
"express": "^4.16.3",
"sequelize": "^4.37.10",
"sqlite3": "^4.0.0"
}
}
.babelrc
{
"presets": ["env"]
}

For the people who are just here due to the error of deprecation notice of using compilers and have no other issues with the babel.
Just install the dependency #babel/register.
And change the test script from mocha --compilers js:babel-core/register to mocha --require #babel/register
This should solve your error regarding the error notice from mocha.

With the update of "Babel" for version 7, the addition of some configuration steps has occurred.
To adjust the "bug" do the following steps:
Install the specified dependency:
npm install --save-dev # babel / register
Change the "mocha.opts" file to the compilers for the following line:
--compilers js: # babel / register
Once this is done, your workflow should be normalized. (I.e.
Note: Look out for the following dependencies that are not present in the tutorial.
npm install --save-dev #babel/cli
npm install --save-dev #babel/core
npm install --save-dev #babel/node
npm install --save-dev #babel/preset-env
npm install --save-dev #babel/register
Auxiliary content:
Setup: https://babeljs.io/setup#installation
Migration: https://babeljs.io/docs/en/v7-migration
Mocha issues - Compilers deprecation https://github.com/mochajs/mocha/wiki/compilers-deprecation

Related

Node js + TypeSc + Babel "Error: Cannot find module 'koa'"

I have cloned a demo . after installing koa,koa-router etc, I got an error. Here is my index.ts file
import Koa from "koa"
import Router from "koa-router"
import logger from "koa-logger"
import json from "koa-json"
const app = new Koa()
const router = new Router()
router.get("/",async(ctx: any,next: any)=>{
ctx.body = {
meg:"Hello world"
}
await next
})
app.use(logger())
app.use(json())
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000,()=>console.log("app is running at 3000"))
Here is my package.json
{
"name": "babel-typescript-sample",
"version": "0.7.2",
"license": "MIT",
"scripts": {
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch",
"build": "npm run build:types && npm run build:js",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline"
},
"devDependencies": {
"#babel/cli": "^7.8.3",
"#babel/core": "^7.8.3",
"#babel/plugin-proposal-class-properties": "^7.8.3",
"#babel/preset-env": "^7.8.3",
"#babel/preset-typescript": "^7.8.3",
"#types/koa": "^2.11.3",
"typescript": "^3.7.5"
},
"dependencies": {
"koa": "^2.11.0",
"koa-bodyparser": "^4.3.0",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.1",
"koa-router": "^8.0.8"
}
}
my file structure loos like:
/-------
-lib
-index.js
-index.d.ts
-node_modules
-src
-index.ts
Actually,before this ,when I run npm run build,I got error src/index.ts:2:20 - error TS2307: Cannot find module 'koa-router'. I write koa-router.d.ts myself, but I don't think it's a great idea, How do you guys resolve?
You have a few options:
Install koa-router typings with npm install --save-dev #types/koa-router.
Set moduleResolution inside tsconfig.json to node. See this for differences. Note that this option only works if the dependency has typings included. In this case, it does not, so first option would be more correct.
Edit paths (points to directory) or types (points to .d.ts) inside tsconfig.json to point to your typings. Yes, writing your typings ("by hand") for existing dependency is generally considered a bad idea, since it could update and therefore break your typings. If a dependency does not have typings, you can contribute to it by generating it using JSDoc, if it uses JavaScript.

Unable to run Gatsby application using npm run develop command

I am developing my first project with Gatsby and prismic. Today when i tried to start my development server i faced this error. I searched but i couldn't find similar errors anywhere. I am completely blocked on this one and have no clue what's causing this error.
extract from package.json :
"scripts": {
"build-dev": "env-cmd -f .env gatsby build",
"develop-dev": "env-cmd -f .env gatsby develop",
....
},
The error
ERROR
UNHANDLED REJECTION Union type PrismicAllDocumentTypes must define one or more member types.
Error: Union type PrismicAllDocumentTypes must define one or more member types.
- query-compiler.js:202 extractOperations
[site]/[gatsby]/dist/query/query-compiler.js:202:20
- query-compiler.js:176 processQueries
[site]/[gatsby]/dist/query/query-compiler.js:176:7
- query-compiler.js:96 compile
[vav_site]/[gatsby]/dist/query/query-compiler.js:96:19
- index.js:484 async module.exports
[site]/[gatsby]/dist/bootstrap/index.js:484:3
- develop.js:446 async module.exports
[site]/[gatsby]/dist/commands/develop.js:446:7
not finished extract queries from components - 0.675s
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gatsby-starter-default#0.1.0 develop-dev: `env-cmd -f .env gatsby develop`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gatsby-starter-default#0.1.0 develop-dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Internal\AppData\Roaming\npm-cache\_logs\2020-03-10T07_36_38_243Z-debug.log
The terminal process terminated with exit code: 1
gatsby version : 2.19.34
react version : 16.13.0
prismic-reactjs version :1.2.0
Have you installed npm install -g gatsby-cli.
[Update] Prismic is not longer recommending the gatsby-source-prismic-graphql plugin.
Here's an article that'll help you migrating to the other one:
How to migrate a project from 'gatsby-source-prismic' to 'gatsby-source-prismic-graphql'
Error says that PrismicAllDocumentTypes has no children. You need to check that your prismic repo has at least one content type and, most important, you have at least one schema added to your codebase and gatsby-config.js:
module.exports = {
plugins: [
{
resolve: `gatsby-source-prismic`,
options: {
repositoryName: `repositoryName`,
accessToken: `accessToken`,
linkResolver: ({ node, key, value }) => post => `/${post.uid}`,
schemas: {
page: require("./src/schemas/page.json"),
},
},
},
],
}
using my own prismic source gatsby package.json file:
{
"name": "gatsby-VARIABLE-prismic",
"description": "YOUR DESCRIPTION",
"private": true,
"license": "MIT",
"version": "0.0.0",
"author": "YOURNAME <YOUR#EMAIL.ADDRESS> (#USERNAME)",
"scripts": {
"build": "gatsby build",
"dev": "gatsby develop -o",
"develop": "gatsby develop",
"serve": "gatsby serve",
"lint": "eslint . --ext .js,.jsx --ignore-path .gitignore",
"lint:fix": "eslint . --ext .js,.jsx --fix --ignore-path .gitignore",
"lint:ci": "yarn lint --format junit -o results/eslint/result.xml",
"format": "prettier \"**/*.md \" --write",
"cy:open": "cypress open",
"cy:run": "cypress run",
"cy:run:ci": "cypress run --browser chrome --reporter junit --reporter-options 'mochaFile=results/cypress/result.xml'",
"test:e2e:dev": "cross-env CYPRESS_SUPPORT=y start-server-and-test dev http://localhost:8000 cy:open",
"test:e2e:run": "cross-env CYPRESS_SUPPORT=y start-server-and-test develop http://localhost:8000 cy:run",
"test:e2e:ci": "cross-env CYPRESS_SUPPORT=y start-server-and-test develop http://localhost:8000 cy:run:ci"
},
"dependencies": {
"#emotion/core": "^10.0.28",
"#emotion/styled": "^10.0.27",
"#emotion/styled-base": "^10.0.28",
"#reach/skip-nav": "^0.8.5",
"emotion": "^10.0.27",
"emotion-server": "^10.0.27",
"emotion-theming": "^10.0.27",
"gatsby": "^2.19.23",
"gatsby-image": "^2.2.41",
"gatsby-plugin-emotion": "^4.1.22",
"gatsby-plugin-google-analytics": "^2.1.36",
"gatsby-plugin-lodash": "^3.1.20",
"gatsby-plugin-manifest": "^2.2.42",
"gatsby-plugin-netlify": "^2.1.33",
"gatsby-plugin-offline": "^3.0.35",
"gatsby-plugin-react-helmet": "^3.1.22",
"gatsby-plugin-sharp": "^2.4.5",
"gatsby-plugin-sitemap": "^2.2.27",
"gatsby-plugin-typography": "^2.3.22",
"gatsby-source-prismic": "^2.2.0",
"gatsby-transformer-sharp": "^2.3.16",
"lodash": "^4.17.15",
"prismic-dom": "^2.1.0",
"prismjs": "^1.19.0",
"prop-types": "^15.7.2",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-helmet": "^5.2.1",
"react-typography": "^0.16.19",
"typeface-lora": "^0.0.72",
"typeface-source-sans-pro": "^1.1.5",
"typography": "^0.16.19"
},
"devDependencies": {
"#testing-library/cypress": "^5.1.2",
"babel-eslint": "^10.1.0",
"cross-env": "^7.0.0",
"cypress": "^3.8.3",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-cypress": "^2.10.3",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.18.3",
"eslint-plugin-react-hooks": "^2.5.0",
"gatsby-cypress": "^0.2.22",
"prettier": "^1.19.1",
"start-server-and-test": "^1.10.9"
},
"keywords": [
"gatsby",
"starter",
"prismic",
"typography",
"minimal",
"gatsby-starter"
]
}
in your gatsby-condig.js:
resolve: 'gatsby-source-prismic',
options: {
repositoryName: 'gatsby-starter-prismic',
accessToken: `${process.env.API_KEY}`,
// Get the correct URLs in blog posts
linkResolver: () => post => `/${post.uid}`,
// PrismJS highlighting for labels and slices
htmlSerializer: () => prismicHtmlSerializer,
// Remove this config option if you only have one language in your Prismic repository
lang: 'en-gb',
},
},
you are probbly using .dotenv - a way to pass secret keys outside of publc ic repo's.
in your root directory create a file named " .env.develop " and a second: ".env.prod "
.env.develop file content:
API_KEY = COPYPASE YOUR API KEY HERE

Cannot download GraphQL schema from endpoint

I'm currently using graphql-cli from Prisma to download the schema from endpoint. But, even after I deploy the changes I made to my schema, which gets deployed successfully, whenever I try to download the schema, I get project prisma - No changes. And the generated prisma.graphql is left unchanged.
I use the following command to download the schema:
graphql get-schema -p prisma --dotenv config/dev.env
dev.env is simply to get PRISMA_ENDPOINT=http://localhost:4466/ environment variable.
I tried to generate prisma.graphql in a different way by having the following in prisma.yml:
endpoint: ${env:PRISMA_ENDPOINT}
datamodel: datamodel.prisma
generate:
- generator: graphql-schema
output: ./generated/
And executed prisma generate, but I get the error:
▸ [WARNING] in
/Users/F/Documents/d/server/prisma/prisma.yml: A valid
environment ▸ variable to satisfy the declaration
'env:PRISMA_ENDPOINT' could not be found.
Tried stopping and recreating Docker as well as deleting the node_module and re-installing, but to no avail.
My package.json:
{
"name": "graphql-basics",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node dist/index.js",
"heroku-postbuild": "babel src --out-dir dist --copy-files",
"dev": "env-cmd ./config/dev.env nodemon src/index.js --ext js,graphql --exec babel-node",
"test": "env-cmd ./config/test.env jest --watch --runInBand",
"get-schema": "graphql get-schema -p prisma --dotenv config/dev.env"
},
"jest": {
"globalSetup": "./tests/jest/globalSetup.js",
"globalTeardown": "./tests/jest/globalTeardown.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"#babel/polyfill": "^7.0.0",
"babel-cli": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-register": "^6.26.0",
"bcryptjs": "^2.4.3",
"cross-fetch": "^2.2.2",
"env-cmd": "^8.0.2",
"google-auth-library": "^4.2.3",
"graphql-cli": "^3.0.14",
"graphql-yoga": "^1.14.10",
"jsonwebtoken": "^8.3.0",
"prisma-binding": "^2.1.1"
},
"devDependencies": {
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
"jest": "^23.5.0",
"nodemon": "^1.17.5"
},
"resolutions": {
"graphql": "^14.5.8"
}
}
To fix the error "variable to satisfy the declaration 'env:PRISMA_ENDPOINT' could not be found." when calling prisma generate you should either set the PRISMA_ENDPOINT variable manually or load it via dotenv. For example you could run npx dotenv -- prisma generate to load the env vars from your .env file.
To download the schema from the endpoint via graphql get-schema make sure to provide a properly configured .graphqlconfig.yml and provide the correct project.
A sample configuration for a prisma project could look like this:
projects:
prisma:
schemaPath: 'src/schema.graphql'
extensions:
endpoints:
default: 'http://localhost:4000/graphql'
database:
schemaPath: 'src/generated/prisma.graphql'
extensions:
prisma: 'database/prisma.yml'
endpoints:
default: 'http://localhost:4466'

Unable to run a node.js file with #babel/preset-env

I'm trying to run a simple node.js file which needs the #babel/preset-env preset. The moment I run the js file, I get a message saying
Requires Babel “7.0.0-0” but was loaded with “6.26.3”
To replicate the issue, please try the following in a new folder:
1. Run the following commands
npm init
npm install #babel/register
npm install #babel/core#^7.2.2
npm install #babel/preset-env
Create a .babelrc file with the following
{
"presets": ["#babel/preset-env"],
"plugins": []
}
Create a sample emp.jsx with the following
import React from "react";
class CommentBox extends React.Component {}
Create a parse.js file with the following
require('babel-register')({presets: ['env', 'react']});
let Emp = require('./emp.jsx');
Now run the parse.js file by running
node parse.js
You should see the error mentioned above. I have been trying to fix with for a long time now. Please help.
Many Thanks
followed your instructions and using #babel/register instead with
this package.json run with no issues
tasted on
node : v8.11.2
yarn : 1.12.3
paese.json
require('#babel/register')({});
let Emp = require('./emp.jsx');
console.log(Emp)
packge.json
{
"name": "sof",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.2.2",
"#babel/preset-env": "^7.2.3",
"#babel/register": "^7.0.0"
},
"dependencies": {
"react": "^16.7.0"
}
}
Found the problem. The .babelrc file that contained a reference to #babel/preset-env. Removed it and the js file compiled just fine.

Javascript Trying to run AMIjs examples in local it does not work

Hello and thank you for reading this.
I would like to load the examples in local WebStorm IDE. In particular I am trying to load the loaders example:
https://github.com/FNNDSC/ami/tree/dev/examples/viewers_upload
The problem I get is the default imports look like thay are not properly set:
So if I try to run npm install
I get this error:
Can not install Node.js module: "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" install base/core/core.utils
Standard error:
npm ERR! code ENOLOCAL
npm ERR! Could not install from "base\core\core.utils" as it does not contain a package.json file.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\YonePC\AppData\Roaming\npm-cache\_logs\2018-01-16T18_50_29_864Z-debug.log
I thought it was related to my package.json but it looks like it has all the dependencies set up:
{
"name": "ami.js",
"version": "0.0.23-dev",
"main": "build/ami.js",
"keywords": [
"ami",
"ami.js",
"three.js",
"webgl",
"dicom",
"nifti",
"awesome",
"medical",
"imaging",
"xtk",
"nrrd",
"vtk",
"stl",
"trk"
],
"author": {
"name": "Nicolas Rannou",
"email": "nicolas#eunate.ch",
"url": "https://eunate.ch"
},
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://fnndsc.github.io/ami"
},
"config": {
"threeVersion": "87",
"amiCDN": "https://cdnjs.cloudflare.com/ajax/libs/ami.js",
"gaKey": "UA-39303022-3",
"babel": "--module-bind js=babel-loader --colors --display-error-details"
},
"dependencies": {
"dicom-parser": "1.7.3",
"image-JPEG2000": "OHIF/image-JPEG2000#master",
"jpeg-lossless-decoder-js": "1.2.3",
"math-float32-to-binary-string": "^1.0.0",
"nifti-reader-js": "v0.5.3",
"nrrd-js": "^0.2.1",
"pako": "1.0.1",
"three": "0.87.0"
},
"scripts": {
"build:ami": "webpack --config webpack.config.build.js",
"build:ami:prod": "cross-env NODE_ENV=production yarn build:ami",
"build:clean": "rimraf -rf build/*",
"build:clean:hot": "rimraf -rf build/*.hot-update.*",
"dev:ami": "webpack --config webpack.config.build.js --hot --watch --colors",
"dist:ami": "yarn build:clean && yarn build:ami && yarn build:ami:prod && yarn doc",
"dist:examples": "node ./scripts/buildDist.js && node ./scripts/router.js examples deploy",
"dist:clean": "rimraf -rf dist/*",
"analyze:ami": "cross-env NODE_WEBPACK_ANALYZE=true yarn build:ami",
"analyze:ami:prod": "cross-env NODE_WEBPACK_ANALYZE=true yarn build:ami:prod",
"clean": "yarn build:clean && yarn dist:clean",
"example": "node ./scripts/router.js examples",
"lesson": "node ./scripts/router.js lessons",
"gen:index:examples": "node ./scripts/genIndexFiles.js examples",
"gen:index:examples:ga": "cross-env NODE_GA=true node ./scripts/genIndexFiles.js examples",
"gen:index:lessons": "node ./scripts/genIndexFiles.js lessons",
"gen:index:lessons:cdn": "node ./scripts/genIndexFiles.js lessons cdn",
"test": "karma start",
"lint": "eslint src/**/*.js",
"doc": "jsdoc -p -r -R README.md -c jsdoc.json -d dist/doc src",
"ami": "yarn lint && yarn dist:ami && yarn test",
"deploy": "yarn dist:clean && yarn build:clean && yarn dist:ami && yarn dist:examples && gh-pages -d dist"
},
"devDependencies": {
"babel-cli": "latest",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-runtime": "^6.26.0",
"compression-webpack-plugin": "^1.0.1",
"cross-env": "^3.2.3",
"eslint": "latest",
"eslint-config-google": "latest",
"gh-pages": "latest",
"glslify": "5.1.0",
"jasmine-core": "latest",
"jsdoc": "jsdoc3/jsdoc#master",
"karma": "latest",
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "latest",
"karma-sinon": "^1.0.5",
"karma-spec-reporter": "latest",
"karma-webpack": "^2.0.4",
"live-server": "^1.1.0",
"puppeteer": "^0.13.0",
"rimraf": "^2.6.1",
"rollup-plugin-node-builtins": "^2.1.2",
"shelljs": "latest",
"sinon": "^2.0.0",
"uglifyjs-webpack-plugin": "^1.0.0-beta.3",
"webpack": "^3.7.1",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-server": "^2.9.1",
"webpack-watch-livereload-plugin": "^0.0.1"
},
"engines": {
"node": ">=6.9.0"
}
}
In addition I looked and tried to find if I would have Node modules isntalled, but as it does not appear in the package.json they are not installed by default:
However I do have node installed:
Microsoft Windows [Versión 6.3.9600]
(c) 2013 Microsoft Corporation. Todos los derechos reservados.
C:\Users\YonePC\WebstormProjects\ATLAS>node -v
v8.9.0
If I execute index.html it says on the developer console:
'Uncaught SyntaxError: Unexpected identifier'
And the line being said is:
import CoreUtils from 'base/core/core.utils';
If I try to redo the imports with IDE's help, this is the result:
After executing the updated index.html:
viewers_upload.js:5 Uncaught SyntaxError: Unexpected identifier
And the line referenced is:
import HelpersLut from "../../src/helpers/helpers.lut";
If I try to use the web CDN library version with:
I need to delete all imports and use the web library version's classes as follows:
I can execute the index.html and trigger the loader:
However I can not do it infinitely because of the class to parseUrl is being on the disk library and not in the web library:
The console says:
'Uncaught ReferenceError: CoreUtils is not defined
at HTMLInputElement.readMultipleFiles (viewers_upload.js:429)
readMultipleFiles # viewers_upload.js:429'
Could you help me please?
Thank you for your time reading this.
EDIT: I am still trying to get that example working on local. So far I have been able to include AMI library's files linking for example:
viewers_upload.js
let dataUrl = CoreUtils.parseUrl(evt.target.files[i].name);
Instead of the normal import generated by the IDE:
import CoreUtils from "../../src/core/core.utils";
Using a direct load:
index.html
<script src="../../src/core/core.utils.js"></script>
However I can not keep doing this infinitely because of the core.utils.js uses import so then the browser reports an error:
Uncaught SyntaxError: Unexpected identifier
In line:
import Validators from './core.validators';
Could you help me please???
To run an example you should:
start dev server: yarn example <example name> (in your case yarn example viewers_upload)
go to localhost:8081 in your web browser

Resources