Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha - Running test ReferenceError: regeneratorRuntime is not defined

I am trying to run tests with async/await using mocha. The project architecture was setup before I started working on it and I have been trying to update it's node version to 8.9.4. The project is an isomorphic application and uses babel, gulp and webpack to run. To run the tests we run a gulp task. There are two .bablerc files in the project. One in the root folder of the project and another in the test fodler. Both have the same configuration:

{
  "presets": [
    ["env", {"exclude": ["transform-regenerator"]}],
    "react",
    "stage-1"
  ],
  "plugins": [
    "babel-plugin-root-import"
  ]
}

When I run the app locally there is no error returned anymore. However when I run the tests with gulp test:api I constantly get the error: ReferenceError: regeneratorRuntime is not defined

This is my gulp file in the test folder:

var gulp = require('gulp')
var gutil = require('gulp-util')
var gulpLoadPlugins = require('gulp-load-plugins')
var plugins = gulpLoadPlugins()
var babel = require('gulp-babel')


require('babel-register')({
    presets:["es2015", "react", "stage-1"]
});

// This is a cheap way of getting 'test:browser' to run fully before 'test:api' kicks in.
gulp.task('test', ['test:browser'], function(){
    return gulp.start('test:api')
});

gulp.task('test:api', function () {
    global.env = 'test'
    gulp.src(['test/unit-tests/server/**/*.spec.js'], {read: false})
        .pipe(plugins.mocha({reporter: 'spec'}))
        .once('error', function (error) {
            console.log(error)
            process.exit(1);
        })
        .once('end', function () {
            process.exit(0);
        })
});

gulp.task('default', ['test']);

Any help on why this is happening wouldd be much appreciated.

like image 919
hyprstack Avatar asked Mar 06 '23 13:03

hyprstack


1 Answers

Node version 8 already has support for async/await so you do not need Babel to transform it; indeed, your root .babelrc includes this preset to exclude the regenerator that would transform async/await (and introduce a dependency on regeneratorRuntime):

["env", {"exclude": ["transform-regenerator"]}]

However, in your test file, the configuration does not specify this preset. Instead, it specifies the preset "es2015", which does include the unwanted transform-regenerator (as you can see at https://babeljs.io/docs/plugins/preset-es2015/). If you change this to match the presets in the root .babelrc, you'll get more consistent results.

like image 158
yoz Avatar answered Apr 23 '23 23:04

yoz