Skip to content

Commit 4bcfdf9

Browse files
committed
Add a pnpm example that replaces a third-party ES module
td.replaceEsm('some-package') resolves the package through quibble, which under pnpm can't see packages that only the app depends on unless pnpm hoisting happens to make them visible (testdouble/quibble#84). The new examples/node-esm-pnpm project is a pnpm workspace with hoisting off whose packages/app test replaces is-number, a dependency of that package only. It has to be a workspace: in a single pnpm project the app's own node_modules is still within reach of quibble's. It fails with quibble 0.11.x (the real is-number runs, because quibble replaces a different copy) and with 0.12.0 on Node 24+ (the generated stub for a CommonJS module is a SyntaxError), so it's expected to fail until quibble is bumped to 0.12.1. script/run-examples installs examples that have a pnpm-workspace.yaml with pnpm, and CI installs pnpm (8 on Node 16, 9 on Node 18, latest otherwise). The example sets hoisting off in both pnpm-workspace.yaml (pnpm 10+) and .npmrc (pnpm 8/9), and minimumReleaseAge: 0 so pnpm doesn't add exclusions to pnpm-workspace.yaml for just-released dependencies. Its lockfile isn't committed, so run-examples installs with --no-frozen-lockfile.
1 parent bc8cb18 commit 4bcfdf9

10 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,13 @@ jobs:
2626
with:
2727
node-version: ${{ matrix.node-version }}
2828
cache: 'npm'
29+
- name: Install pnpm (for the pnpm example project)
30+
shell: bash
31+
run: |
32+
case "$(node -p 'process.versions.node.split(".")[0]')" in
33+
16) npm install --global pnpm@8 ;;
34+
18) npm install --global pnpm@9 ;;
35+
*) npm install --global pnpm ;;
36+
esac
2937
- run: npm ci
3038
- run: npm test

‎examples/node-esm-pnpm/.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
pnpm-lock.yaml

‎examples/node-esm-pnpm/.npmrc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hoist=false
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "testdouble-node-esm-pnpm-example",
3+
"private": true,
4+
"scripts": {
5+
"test": "pnpm --recursive test"
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import isNumber from 'is-number'
2+
3+
export default function (thing) {
4+
if (!isNumber(thing)) {
5+
throw new Error('numbers only!')
6+
}
7+
return true
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "testdouble-node-esm-pnpm-example-app",
3+
"private": true,
4+
"scripts": {
5+
"test": "node --loader=testdouble --test ./test/numbers-only-test.mjs"
6+
},
7+
"devDependencies": {
8+
"is-number": "^7.0.0",
9+
"testdouble": "file:../../../.."
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import assert from 'node:assert/strict'
2+
import { describe, it, afterEach } from 'node:test'
3+
import * as td from 'testdouble'
4+
5+
// is-number is a dependency of this package only, so pnpm installs it where
6+
// this file can see it but testdouble (in the workspace root's node_modules)
7+
// can't. td.replaceEsm() has to resolve it the way this file would.
8+
describe('numbers-only', function () {
9+
afterEach(function () {
10+
td.reset()
11+
})
12+
13+
it('replaces a third-party module only this package depends on', async function () {
14+
const isNumber = (await td.replaceEsm('is-number')).default
15+
const numbersOnly = (await import('../lib/numbers-only.mjs')).default
16+
td.when(isNumber('seven')).thenReturn(true)
17+
18+
assert.strictEqual(numbersOnly('seven'), true)
19+
})
20+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packages:
2+
- packages/*
3+
4+
# Keep each package's dependencies out of reach of the packages installed in
5+
# the workspace root, so td.replaceEsm() has to resolve bare specifiers from
6+
# the test file (pnpm 10+ reads this here, pnpm 8 and 9 from .npmrc)
7+
hoist: false
8+
9+
# Install testdouble's dependencies even when they were just released,
10+
# instead of pnpm adding exclusions to this file for them
11+
minimumReleaseAge: 0

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"test:esm-auto-loader": "node test/support/supports-auto-load.js not || cross-env TS_NODE_IGNORE=\"node_modules,notypescript\" ./test/safe-esm/teenytest-proxy.js --helper test/helper.js test/safe-esm/replace.test.js",
4747
"test:no-loader-esm": "node test/support/supports-auto-load.js || cross-env TS_NODE_IGNORE=\"node_modules,notypescript\" teenytest test/helper.js './test/safe-esm/*.no-loader-test.{mjs,js}'",
4848
"test:ci": "npm run compile && run-p style test && echo \"All done!\"",
49-
"test:example": "run-s test:example:babel test:example:jest test:example:jest-broken test:example:node test:example:node-ava test:example:node-esm test:example-esm-auto-loader",
49+
"test:example": "run-s test:example:babel test:example:jest test:example:jest-broken test:example:node test:example:node-ava test:example:node-esm test:example:node-esm-pnpm test:example-esm-auto-loader",
5050
"test:example-esm-auto-loader": "node test/support/supports-auto-load.js not || npm run test:example:node-esm-auto-loader",
5151
"test:example:babel": "bash ./script/run-examples babel",
5252
"test:example:jest": "bash ./script/run-examples jest",
@@ -55,6 +55,7 @@
5555
"test:example:node": "bash ./script/run-examples node",
5656
"test:example:node-ava": "bash ./script/run-examples node-ava",
5757
"test:example:node-esm": "bash ./script/run-examples node-esm",
58+
"test:example:node-esm-pnpm": "bash ./script/run-examples node-esm-pnpm",
5859
"test:example:node-esm-auto-loader": "bash ./script/run-examples node-esm-auto-loader",
5960
"test:example:webpack": "bash ./script/run-examples webpack",
6061
"version:write": "echo \"export default '$npm_package_version'\" > src/version.js",

‎script/run-examples‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ cd examples
99
for D in $projects; do
1010
echo "<------ Running tests for example project '${D}'"
1111
cd "${D}"
12-
npm install
12+
if [ -f pnpm-workspace.yaml ]; then
13+
if ! command -v pnpm > /dev/null; then
14+
echo "pnpm is required to run the example project '${D}' (npm install --global pnpm)"
15+
exit 1
16+
fi
17+
# Its lockfile isn't committed, which pnpm refuses by default on CI
18+
pnpm install --no-frozen-lockfile
19+
else
20+
npm install
21+
fi
1322
npm test
1423
cd ..
1524
done

0 commit comments

Comments
 (0)