Table of contents
1.
Introduction
2.
Current Limitations
3.
Implementation
3.1.
Node.js Native ESM Support
3.1.1.
Program:
3.1.2.
Program:
3.1.3.
Output:
3.2.
The esm Library
3.2.1.
Program:
3.2.2.
Program:
3.2.3.
Output:
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Native ES Modules in Mocha

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

While working on a particular project, you may want to switch to using ES modules in the browser. Emigrating the existing code to use ES modules is easy, but maintaining the current unit tests can get tricky. That is because most Javascript testing frameworks do not support native ES modules yet.

Import and Export statements are both used to refer to ES modules, and Node can not import other file types with these statements. If you want to understand ES modules in more detail, check out our article on the Difference Between Node.js Require and ES6 Import and Export.

Using ES modules with testing can be done with the help of the esm library. esm is a fast, production-ready, and zero-dependency ES module loader for Node.js. Using the esm library with Mocha is straightforward. You can also use ES modules directly through native ESM support in Node.js. In this article, we will learn about the project configuration and implementation of the ES modules in the Mocha framework with the help of examples.

Current Limitations

Although using Native ES Modules with Mocha is pretty elementary to implement. There are some limitations with the Native ESM support in Mocha. Before going on with the usage, you should be aware of the following restrictions:

  • Watch Mode, which reruns tests when a file is changed (--watch-w), does not support ES Module test files.
  • Custom Reporters and Custom Interfaces files can only be CommonJS.
  • Configuration file can only in the form CommonJS file like .mocharc.js or .mocharc.cjs.
  • When using module-level mocks via libraries like proxyquirerewiremock, or rewire, you should hold off on using ES modules for your test files. You can instead switch to using testdouble, which does support ESM.

Implementation

As we discussed at the beginning of this article, there are two primary ways we can use Native ES Modules in the Mocha framework. We will now try out both of the implementations with the help of examples.

Node.js Native ESM Support

Mocha supports writing your tests as ES modules instead of just using CommonJS. You don't need to do anything out of the ordinary to enable tests as ES Modules.

You can write your test file as an ES Module. In Node.js, you can accomplish that by ending the file with the .mjs extension or adding "type": "module" into your package.json file if you want to use the .js extension. Here is an example of this method:

Program:

//sum.mjs
export default function sum(a, b) {
  return a + b;
}
You can also try this code with Online Javascript Compiler
Run Code

Program:

//nesm.test.mjs
import sum from "./sum.mjs";
import assert from "assert";

it("should add to numbers from an es module", () => {
  assert.equal(sum(3, 5), 8);
});
You can also try this code with Online Javascript Compiler
Run Code

Output:

The esm Library

Using the esm library to load ES modules in your Node project with Mocha framework is also simple. 

To start, you need to make sure that you have the esm library installed in your npm project like this:

npm i esm 

Below is an elementary example that demonstrates how we can use this library.

Program:

//sum.js
export default function sum(a, b) {
  return a + b;
}
You can also try this code with Online Javascript Compiler
Run Code

Program:

import sum from "./sum.js";
import assert from "assert";

describe("sum", function () {
  it("returns the sum of two arguments", function () {
     assert.equal(sum(1, 2), 3);
  });
});
You can also try this code with Online Javascript Compiler
Run Code

To ensure that Node understands the ES import syntax, you need to pass esm as a requirement to our mocha command in the terminal.

npx mocha 'nesm.test.js' --require esm

Output:

FAQs

1. What are native ES modules? 

Ans: ES Modules is the standard ECMAScript for working with modules. While Node.js has used the CommonJS standard for years, the browser has no module system. Every major decision like the module system must first be standardized by ECMAScript and implemented.

 

2. How are ES Modules different from CommonJS Modules?

Ans: The ES module is the official standard format to package JavaScript code for reuse, and most modern web browsers support the modules natively. But Node.js supports the CommonJS module format by default. Starting with version 13.2.0, Node.js provides stable support of ES modules.

 

3. What are the advantages of ES6 Modules?

Ans: There are multiple advantages of ES Modules:

  • Having a single conducive declarative pattern makes class patterns painless and boosts interoperability in ES6. 
  • They are a simple prototype-based OO pattern. 
  • Classes support inheritance, instance, and static methods, making ES6 a more friendly version of Javascript.

Key Takeaways

In this article, we have extensively discussed Node.js Native ESM Support and testing Native ESM with the esm library and their implementation in Mocha with the help of Examples. Native ESM Support is one of many vital features provided by Mocha, and there are a lot more of these features you can explore.

If you want to widen your knowledge about unit testing in javascript, you should explore the Jest framework with articles like Getting Started with Jest and Writing first Unit Test with Jest.

We hope that this blog has helped you enhance your knowledge regarding Native ES Modules in Mocha. If you would like to learn more, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more. Till then, Happy Learning!

Live masterclass