for await (... of ...) not working. Babel present env, node v10 - node.js

It's been a while since I started a nodejs project from scratch, so was a bit of a headscratcher to set up and configure eslint, babel etc.
right now my babelrc is :
{
"presets": [
[
"env",
{
"targets": {
"node": "10"
}
}
]
],
"plugins": [
[
"transform-runtime",
{
"regenerator": true
}
]
]
}
package.json has dev dependencies:
"babel-cli": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
Now I want to loop over a list of objects. For each, I need to perform some asynchronous tasks that I'll need to await on, so I did:
for await (const thing of things) {
const foo = await doSomethingThatTakesAwhile(thing)
// etc
}
but when I run it in dev (nodemon via babel-node) now there's a syntax error on the await:
for await (const thing of things) {
^
Syntax Error Unexpected token, expected (
at Parser.pp$5.raise (... \node_modules\babylon\lib\index.js:4454:13)
at Parser.pp.unexpected (... \node_modules\babylon\lib\index.js:1761:8)
at Parser.pp.expect (... \node_modules\babylon\lib\index.js:1749:33)
at Parser.pp$1.parseForStatement (... \node_modules\babylon\lib\index.js:2008:8)
etc..
Do I have to change my babel config, and/or have I completely misunderstood for/await and await/async ?

I found another project in which i know for await of works... it looks like I'm using old babel plugins and not the new, separated out #babel/xxx libs. After trial and error installing and uninstalling stuff: this is the resulting babelrc that worked:
{
"presets": [
[
"#babel/preset-env",
{
"targets": {
"node": "10"
}
}
]
],
"plugins": [
[
"#babel/plugin-transform-runtime",
{
"regenerator": true
},
"#babel/preset-env"
]
]
}
By this point I had installed all of:
#babel/core
#babel/node
#babel/cli
#babel/preset-env
#babel/plugin-transform-runtime
Then I ran into this issue: https://github.com/meteor/meteor/issues/10128
So Had to also install #babel/runtime pegged at 7.0.0-beta.55 ... and now it builds!!

I believe you need the babel-plugin-proposal-async-generator-functions plugin to use the for await of syntax.

Related

Testing svelte components with import.meta.env

I'm struggeling now for a couple of days to get my testsetup running. Rough outline: Vite, Svelte (with ts), Jest.
I'm using import.meta.env.SOMENAME for my environment vars although this works fine for development as soon as a component uses import.meta.env the test will fail with:
SyntaxError: Cannot use 'import.meta' outside a module
I've tried different transformers, babel-plugins and configs but never succeeded...
My jest config:
"jest": {
"globals": {
"ts-jest": {
"isolatedModules": true
}
},
"verbose": true,
"transform": {
"^.+\\.svelte$": [
"svelte-jester",
{
"preprocess": true
}
],
"^.+\\.ts$": "ts-jest",
"^.+\\.js$": "babel-jest"
},
"setupFilesAfterEnv": ["<rootDir>/setupTests.ts"],
"moduleFileExtensions": ["js", "ts", "svelte"]
}
babel.config.js
module.exports = {
presets: [
[
"#babel/preset-env",
{
targets: {
node: "current"
}
}
]
]
};
svelte.config.cjs
const sveltePreprocess = require('svelte-preprocess')
module.exports = {
emitCss: true,
preprocess: sveltePreprocess()
};
Among other things I tried to use #babel/plugin-syntax-import-meta but ended up with the same error. Also vite-jest looked very promising but again I couldn't make it work.
I appreciate every hint I can get. If I can provide any additional info please let me know. Also my knowledge of vite and babel is very limited so REALLY appreciate any help IU can get on this topic.
Update (Solution)
So If you use babel you could use babel-preset-vite. The approach with esbuild-jest from Apu is also good solution that many people use. Unfortunately those things didn't work for me so I decided to use a workaround with vite's define.
This workaround consists of two steps.
replace import.meta.env with process.env (if this is a deal breaker for you then I hope you have luck with the solutions above) You only have to replace the instances in files you want to test with jest.
Update Vite config with define. This step is necessary or your build will break (dev will still work)
vite.config.js
const dotEnvConfig = dotenv.config();
export default defineConfig({
define: {
"process.env.NODE_ENV": `"${process.env.NODE_ENV}"`,
"process.env.VITE_APP_SOMENAME": `"${process.env.VITE_APP_SOMENAME}"`
},
...
)};
I know this is just a workaround but maybe this helps someone. Thanks & Good Luck.
A more recent alternative to Jest that understands import.meta.env is Vitest.
It should require almost no additional configuration to get started and it's highly compatible with Jest so it requires few changes to the actual tests.
The advantages of Vitest over Jest for this use case are:
It's designed specifically for Vite and will process tests on demand
It will reuse your existing Vite configuration:
Any define variables will be replaced as expected
Extensions that Vite adds to import.meta will be available as usual
I was having issues with svelte component testing as well using jest. babel is not good at resolving import.meta. I used esbuild-jest to transform both ts and js files. It solves the issue with the import.meta. Here is my jest.config.cjs.
npm i esbuild esbuild-jest -D
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');
const config = {
"transform": {
"^.+\\.svelte$": [
"svelte-jester",
{
"preprocess": true
}
],
"^.+\\.(ts|tsx|js|jsx)$": ["esbuild-jest"]
},
"moduleFileExtensions": [
"js",
"ts",
"tsx",
"svelte"
],
"setupFilesAfterEnv": [
"#testing-library/jest-dom/extend-expect"
],
"collectCoverageFrom": [
"**/*.(t|j)s",
"**/*.svelte"
],
coverageProvider: 'v8',
"coverageDirectory": "./coverage",
"coveragePathIgnorePatterns": [
"/node_modules/",
"/.svelte-kit/"
],
"moduleNameMapper": pathsToModuleNameMapper(compilerOptions.paths, {prefix: '<rootDir>/'})
};
module.exports = config;

How can I get ESLint to recognize aggregate export namespaces?

The Situation
I have a NodeJS project that uses Babel and ESLint (6.8).
I'm using the relatively new syntax for aggregate exports (export * as name1 from …;).
The Code
constants.js
export const x = 5
export const y = 6
index.js
export * as constants from './constants'
sandbox.js
import { constants } from './index'
console.log(constants.x)
When I run babel-node sandbox.js everything works just fine, and the value for x (5) is rendered.
.eslintrc
{
"extends": "airbnb-base",
"parser": "babel-eslint",
"env": {
"es6": true,
"node": true,
"jest": true
}
}
.babelrc
{
"presets": [
[
"#babel/preset-env",
{
"targets": {
"node": "13.10"
}
}
]
],
"plugins": [
"#babel/plugin-proposal-export-namespace-from"
]
}
The Problem
ESLint seems to be confused by my aggregate export, rendering the following error when I lint:
sandbox.js
1:10 error constants not found in './index' import/named
The Question
How do I get ESLint to recognize that the named aggregate does in fact exist? I would like to be able to still benefit from the import/named checks overall.

Trying to build Jest is throwing "Caching was left unconfigured."

I have the following .babelrc.js in the root folder:
{
"plugins": [
"#babel/plugin-transform-flow-strip-types",
"#babel/plugin-transform-modules-commonjs",
"#babel/plugin-transform-async-to-generator",
"#babel/plugin-transform-strict-mode",
"#babel/plugin-transform-runtime"
],
"cache": "true"
}
but when it tries to run node ./packages/jest-cli/bin/jest.js I see:
Caching was left unconfigured. Babel's plugins, presets, and .babelrc.js files can be configured
for various types of caching, using the first param of their handler functions:
What am I missing?
Use new babel.config.js
https://new.babeljs.io/docs/en/next/babelconfigjs.html
module.exports = function(api) {
api.cache(true)
return {
plugins: [
"#babel/plugin-transform-flow-strip-types",
"#babel/plugin-transform-modules-commonjs",
"#babel/plugin-transform-async-to-generator",
"#babel/plugin-transform-strict-mode",
"#babel/plugin-transform-runtime"
]
}
}

TypeError: Object.values is not a function -- How to correctly polyfill with babel-preset-env in jest?

I get the following error after upgrading to Jest v20 where they removed the automatic babel-polyfill due to memory leaks:
TypeError: Object.values is not a function
I realize I need to polyfill this on my own now, I am using babel-preset-env and have the following .babelrc file:
"env": {
"development": {
"plugins": ["transform-react-jsx-source"]
},
"test": {
"presets": [
"react",
"stage-3",
["env", {
"targets": {
"browsers": [
"firefox >= 36",
"chrome >= 38",
"opera >= 25",
"safari >= 9",
"ios >= 9"
],
"node": "6.11.4"
},
"useBuiltIns": "usage",
"include": ["es7.object.values"],
"debug": true
}],
"jest"
],
"plugins": [
"transform-class-properties"
],
}
}
I can see that es7.object.values is being polyfilled in the debug output:
Using polyfills:
...
es7.object.values {"chrome":"38","firefox":"36","ios":"9","safari":"9","node":"6.11.4"}
But I am still getting the error message, help!
Some of the options are:
bump node version to the one supporting Object.values (which
seems to be 7.0 judging from this answer),
polyfill it using babel-polyfill (via import 'babel-polyfill' in setupTests.js file).
In my case, the Node version was the reason.
I just updated the node 6 to node 7 and it fixed.

Cannot browserify the backbone-syphon

This is my first time using browserify. I am trying to use the backbone-syphon library (https://github.com/marionettejs/backbone.syphon) in my sails.js/Gulp/Browserify/Backbone project.
My package.json has this
"browser": {
"backbone.syphon": "./assets/js/lib/backbone-syphon/backbone.syphon.js",
"bootstrap": "./node_modules/bootstrap/dist/js/bootstrap.js",
"jQuery": "./node_modules/jquery/dist/jquery.min.js"
},
"browserify": {
"transform": [
"browserify-shim"
]
},
"browserify-shim": {
"bootstrap": {
"exports": "bootstrap",
"depends": [
"jquery:jQuery"
]
},
"backbone.syphon": {
"exports": "Backbone.Syphon",
"depends": [
//"backbone:Backbone" //Tried only this also
"backbone:Backbone", "jquery:jQuery", "underscore:_"
]
}
},
I do not get any errors during startup. But when I try to use "Backbone.Syphon" anywhere in the code it gives an undefined error.
I have explicitly added require('lib/backbone-syphon/backbone.syphon.js') in my code though I am not sure if that is necessary or not.
What am I missing?
Using the latest syphon library works correctly with browserify. Not sure why the old one was not working.
Should have tried this before posting the question.

Resources