Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions docs/guides/how-to/stub-esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Sinon correctly raises an error here because, per the ES module spec, namespace

## The solution: use the `esm` package with `mutableNamespace`

The [`esm`](https://github.com/standard-things/esm) package is a fast, production-ready ES module loader for Node.js. It offers a `mutableNamespace` option that makes module namespace objects writable, which is what Sinon needs to install stubs.
The [`esm`](https://github.com/standard-things/esm) package is a fast, production-ready ES module loader for Node.js. It offers a `cjs.mutableNamespace` option that makes module namespace objects writable, which is what Sinon needs to install stubs.

### Step 1: Install the `esm` package

Expand All @@ -63,13 +63,12 @@ npm install --save-dev esm

### Step 2: Create a loader / setup file

Create a file at the root of your project (e.g., `esm-loader.cjs`) that enables the `mutableNamespace` option:
Create a file at the root of your project (e.g., `esm-loader.cjs`) that enables the `cjs.mutableNamespace` option:

```javascript
// esm-loader.cjs
require = require("esm")(module, {
cjs: true,
mutableNamespace: true,
cjs: { mutableNamespace: true },
});
```

Expand Down Expand Up @@ -148,8 +147,7 @@ describe("calculator", () => {

```javascript
require = require("esm")(module, {
cjs: true,
mutableNamespace: true,
cjs: { mutableNamespace: true },
});
```

Expand Down Expand Up @@ -203,7 +201,7 @@ describe("calculator", () => {

## Why does this work?

The `esm` package hooks into Node.js's module loading system. When `mutableNamespace: true` is set, it wraps ES module namespace objects with a `Proxy` that allows property assignment. Sinon's `stub()` function replaces the property on the namespace object; with the proxy in place, this assignment succeeds instead of throwing.
The `esm` package hooks into Node.js's module loading system. When `cjs.mutableNamespace` is set to `true`, it wraps ES module namespace objects with a `Proxy` that allows property assignment. Sinon's `stub()` function replaces the property on the namespace object; with the proxy in place, this assignment succeeds instead of throwing.

## Limitations and caveats

Expand Down