Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
How to use es6 moduel with mocha, when running test through mocha.run()?

When running mocha tests in single node project, I have simply added -r ems to the script test command. This works nicely; ems makes sure I can use es6 modules.

However, our tests are being run by a little bit more sophisticated test runner, and I dont know how to configure it to use ems

Here is the code:

const path = require('path');
const process = require('process');

const Mocha = require('mocha');
const R = require('ramda');

require('./setup/mocha.setup');

const all = require('./all');
const runner = require('./lib/runner');

async function runningMochaHere ({ grep, invert, reporter }) { // eslint-disable-line max-statements
  let testCount = 0;
  try {
    const mocha = new Mocha();
    if (grep) {
      mocha.reporter(reporter || 'spec');
      mocha.grep(grep);
      if (invert) mocha.invert();
    } else {
      mocha.reporter(reporter || 'dot');
      mocha.grep(/@slow/).invert();
    }
    const mochaMainPattern = 'test/mocha.main.js';
    const mochaMainList = await all.globbing(mochaMainPattern);
    if (mochaMainList.length === 1) {
      const mochaMainPath = path.join(process.cwd(), mochaMainList[0]);
      require(mochaMainPath);
    }
    const testFiles = await all.globbing('src/**/*.test.js');
    mocha.files.push.apply(mocha.files, testFiles);
    const failureCount = await new Promise(resolve => {
      const mochaRunner = mocha.run(resolve);
      mochaRunner.on(Mocha.Runner.constants.EVENT_TEST_PASS, () => ++testCount);
    });
    if (failureCount > 0) throw new Error('mocha failed');
    return testCount;
  } catch (err) {
    console.log(err);
    throw err;
  }
}

When the tests are being run by this code, I get the usual syntax error as I did from es6 modules before I installed ems:

import { Container as AdminDashboard } from '../page/admin/dashboard';
       ^

SyntaxError: Unexpected token {

I tried looking at https://mochajs.org/api/mocha, but I can't find anything about adding those kinds of options. Any ideas?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda