| authors | brunocroh |
|---|
Updates mock.module() calls to use the new exports object API, merging the deprecated defaultExport and namedExports options into a single exports object where the default export is keyed as default.
Run this codemod with:
npx codemod @nodejs/mock-module-exportsBoth defaultExport and namedExports present with inline object:
mock.module('example', {
- defaultExport: 'bar',
- namedExports: {
- foo: 'foo',
- baz: 'baz',
- },
+ exports: {
+ default: 'bar',
+ foo: 'foo',
+ baz: 'baz',
+ },
});namedExports given as a variable reference — the spread is wrapped with a || {} fallback:
mock.module('example2', {
- defaultExport: 'bar',
- namedExports: namedExports,
+ exports: {
+ default: 'bar',
+ ...(namedExports || {}),
+ },
});Only defaultExport, no named exports:
mock.module('example', {
- defaultExport: 'bar',
+ exports: {
+ default: 'bar',
+ },
});Only namedExports, no default export:
mock.module('example', {
- namedExports: {
- foo: 'foo',
- },
+ exports: {
+ foo: 'foo',
+ },
});When namedExports is a variable (identifier) rather than an inline object, the codemod emits a spread with a || {} fallback (...(obj || {})) to preserve safe behaviour when the variable may be undefined or null.