Remove jest esm examples (#2517)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent d785d8b · Browse files · 13 files·+0 added−208 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
13 files+0 added−208 removed
- tests:+0 added−18 removed
- other:+0 added−190 removed
Provenance
- Commit
01ce5d0- Tree
dcd28ca- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
d785d8b
13 files changed+0 added−208 removed
deleted:examples/using-esm-from-commonjs/jest-javascript/.gitignore-4 removed
1-node_modules/2-3-# Save space, do not commit package-lock.json with all the Jest dependencies!4-package-lock.jsondeleted:examples/using-esm-from-commonjs/jest-javascript/babel.config.js-3 removed
123module.exports = { presets: [['@babel/preset-env', { targets: { node: 'current' } }]],};deleted:examples/using-esm-from-commonjs/jest-javascript/index.spec.cjs-9 removed
123456789const { Command } = require('commander');const { test, expect } = require('@jest/globals');
test('when set command name then get command name', () => { const program = new Command(); const name = 'my-cli'; program.name(name); expect(program.name()).toBe(name);});deleted:examples/using-esm-from-commonjs/jest-javascript/jest.config.js-6 removed
123456// List the dependencies that are pure esm and need transformation.const esmModules = ['commander'].join('|'); // redundant join in case you have others
module.exports = { transformIgnorePatterns: [`node_modules/(?!(${esmModules})/)`],};deleted:examples/using-esm-from-commonjs/jest-javascript/package.json-17 removed
1234567891011121314151617{ "name": "jest-javascript", "version": "1.0.0", "description": "", "scripts": { "test": "jest" }, "license": "MIT", "type": "commonjs", "dependencies": { "commander": "^15.0.0-0" }, "devDependencies": { "@babel/preset-env": "^7.28.6", "jest": "^30.2.0" }}deleted:examples/using-esm-from-commonjs/jest-javascript/README.md-40 removed
12345678910111213141516171819202122232425262728293031323334353637383940# Example configuration for Jest 30 running CommonJS Javascript
This project shows using Babel to transform Commander ESM to CommonJS.
Example error message when requiring ESM-only Commander in a Jest test with default configuration:
```$ npm run --silent test FAIL ./index.test.js ● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
[more detail omitted, check output for up-to-date suggestions]... SyntaxError: Cannot use import statement outside a module
> 1 | const { Command } = require('commander');...```
Additional install:
```consolenpm install --save-dev @babel/preset-env```
Additional setup:
- add `babel.config.js` to configure babel for smart setup for current node version- add `transformIgnorePatterns` to `jest.config.js` to process Commander from `node_modules`
Running test:
```consolenpm installnpm test```deleted:examples/using-esm-from-commonjs/jest-typescript/.gitignore-4 removed
1-node_modules/2-3-# Save space, do not commit package-lock.json with all the Jest dependencies!4-package-lock.jsondeleted:examples/using-esm-from-commonjs/jest-typescript/index.spec.cts-9 removed
123456789import { Command } from 'commander';import { test, expect } from '@jest/globals';
test('when set command name then get command name', () => { const program = new Command(); const name = 'my-cli'; program.name(name); expect(program.name()).toBe(name);});deleted:examples/using-esm-from-commonjs/jest-typescript/jest.config.js-16 removed
1-// List the dependencies that are pure esm and need transformation.2-const esmModules = ['commander'].join('|'); // redundant join in case you have others3-4-module.exports = {5- // 1. tell ts-jest to (also) handle .js and .mjs files6- transform: {7- '^.+\\.[cm]?[tj]sx?$': [8- 'ts-jest',9- {10- useESM: false, // want CJS for Jest11- },12- ],13- },14- // 2. allow transformation of specific node_modules15- transformIgnorePatterns: [`node_modules/(?!(${esmModules})/)`],16-};deleted:examples/using-esm-from-commonjs/jest-typescript/package.json-18 removed
1-{2- "name": "jest-typescript",3- "version": "1.0.0",4- "description": "",5- "scripts": {6- "test": "jest"7- },8- "license": "MIT",9- "type": "commonjs",10- "dependencies": {11- "commander": "^15.0.0-0"12- },13- "devDependencies": {14- "jest": "^30.2.0",15- "ts-jest": "^29.4.6",16- "typescript": "^5.9.3"17- }18-}deleted:examples/using-esm-from-commonjs/jest-typescript/README.md-34 removed
1-# Example configuration for Jest 30 running CommonJS from TypeScript2-3-This project shows using `ts-jest` to transform Commander ESM to CommonJS.4-5-Example error message when requiring ESM-only Commander in a Jest test with default configuration:6-7-```8-$ npm run --silent test9- FAIL ./index.test.ts10- ● Test suite failed to run11-12- Jest encountered an unexpected token13-14- Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.15-16- [more detail omitted, check output for up-to-date suggestions]17-...18- import { Command } from 'commander';19- ^^^^^^20-21- SyntaxError: Cannot use import statement outside a module22-...23-```24-25-Additional setup:26-27-- see two tweaks in `jest.config.js` to use `ts-jest` to convert Commander ESM to CommonJS28-29-Running test:30-31-```console32-npm install33-npm test34-```deleted:examples/using-esm-from-commonjs/jest-typescript/tsconfig.json-44 removed
1-{2- // Visit https://aka.ms/tsconfig to read more about this file3- "compilerOptions": {4- // File Layout5- // "rootDir": "./src",6- // "outDir": "./dist",7-8- // Environment Settings9- // See also https://aka.ms/tsconfig/module10- "module": "nodenext",11- "target": "esnext",12- "types": [],13- // For nodejs:14- // "lib": ["esnext"],15- // "types": ["node"],16- // and npm install -D @types/node17-18- // Other Outputs19- "sourceMap": true,20- "declaration": true,21- "declarationMap": true,22-23- // Stricter Typechecking Options24- "noUncheckedIndexedAccess": true,25- "exactOptionalPropertyTypes": true,26-27- // Style Options28- // "noImplicitReturns": true,29- // "noImplicitOverride": true,30- // "noUnusedLocals": true,31- // "noUnusedParameters": true,32- // "noFallthroughCasesInSwitch": true,33- // "noPropertyAccessFromIndexSignature": true,34-35- // Recommended Options36- "strict": true,37- "jsx": "react-jsx",38- "verbatimModuleSyntax": false, // we want import to be output as require, not verbatim39- "isolatedModules": true,40- "noUncheckedSideEffectImports": true,41- "moduleDetection": "force",42- "skipLibCheck": true,43- },44-}modified:CHANGELOG.md-4 removed
42 unmodified lines43 44 If you have problems using Commander 15 in your environment, one option is stay on Commander 14 for now. Commander 14 will45 get security updates until May 2027 and things will hopefully improve for your setup in the meantime.46 47-To allow `require(esm)` from CommonJS you may be able to make small changes to your current setup. Some48-examples for Jest are included in [examples/using-esm-from-commonjs](./examples/using-esm-from-commonjs).49-(Commander was using Jest, so we have a special interest.)50-47 ## [14.0.3] (2026-01-31)48 49 ### Added50 1587 unmodified lines