Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda