Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!