Skip to content

Commit

Permalink
fix: add test cases when import a module from a cjs with circular
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingbirdhub committed Dec 12, 2022
1 parent a52aba0 commit 33ad873
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 31 deletions.
3 changes: 2 additions & 1 deletion packages/commonjs/package.json
Expand Up @@ -36,7 +36,8 @@
"pretest": "pnpm build",
"release": "pnpm --workspace-root plugin:release --pkg $npm_package_name",
"test": "ava",
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit",
"test:update": "ava --update-snapshots --match esm-import-cjs-circular-dependency"
},
"files": [
"dist",
Expand Down
@@ -0,0 +1,3 @@
module.exports = {
description: 'esm import cjs circular dependency',
};
@@ -0,0 +1,3 @@
import cjs from 'cjs-circular';

t.is(cjs, 'foo');
10 changes: 10 additions & 0 deletions packages/commonjs/test/node_modules/cjs-circular/circular.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/commonjs/test/node_modules/cjs-circular/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/commonjs/test/node_modules/cjs-circular/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 89 additions & 30 deletions packages/commonjs/test/snapshots/function.js.md
Expand Up @@ -1638,19 +1638,24 @@ Generated by [AVA](https://avajs.dev).
{
'main.js': `'use strict';␊
␊
var submodule;␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
var submodule$1;␊
var hasRequiredSubmodule;␊
␊
function requireSubmodule () {␊
if (hasRequiredSubmodule) return submodule;␊
if (hasRequiredSubmodule) return submodule$1;␊
hasRequiredSubmodule = 1;␊
submodule = 'submodule';␊
return submodule;␊
submodule$1 = 'submodule';␊
return submodule$1;␊
}␊
␊
var submoduleExports = requireSubmodule();␊
var submodule = /*@__PURE__*/getDefaultExportFromCjs(submoduleExports);␊
␊
t.is(submoduleExports, 'submodule');␊
t.is(submodule, 'submodule');␊
`,
}

Expand Down Expand Up @@ -3718,6 +3723,23 @@ Generated by [AVA](https://avajs.dev).
`,
}

## esm-import-cjs-circular-dependency

> Snapshot 1
{
'main.js': `'use strict';␊
␊
var cjs = require('cjs-circular');␊
␊
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }␊
␊
var cjs__default = /*#__PURE__*/_interopDefaultCompat(cjs);␊
␊
t.is(cjs__default.default, 'foo');␊
`,
}

## esm-mixed-exports-function-default

> Snapshot 1
Expand Down Expand Up @@ -5690,15 +5712,22 @@ Generated by [AVA](https://avajs.dev).
␊
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
exports.commonjsGlobal = commonjsGlobal;␊
exports.getDefaultExportFromCjs = getDefaultExportFromCjs;␊
`,
'main.js': `'use strict';␊
␊
var main = require('./main2.js');␊
var _commonjsHelpers = require('./_virtual/_commonjsHelpers.js');␊
var main$1 = require('./main2.js');␊
␊
var mainExports = main.__require();␊
var mainExports = main$1.__require();␊
var main = /*@__PURE__*/_commonjsHelpers.getDefaultExportFromCjs(mainExports);␊
␊
module.exports = mainExports;␊
module.exports = main;␊
`,
'main2.js': `'use strict';␊
␊
Expand Down Expand Up @@ -6332,7 +6361,11 @@ Generated by [AVA](https://avajs.dev).
{
'main.js': `'use strict';␊
␊
var main = {};␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
var main$1 = {};␊
␊
var other = {};␊
␊
Expand All @@ -6348,16 +6381,17 @@ Generated by [AVA](https://avajs.dev).
var hasRequiredMain;␊
␊
function requireMain () {␊
if (hasRequiredMain) return main;␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
main.foo = 'foo';␊
main$1.foo = 'foo';␊
t.is(requireOther().foo, 'foo');␊
return main;␊
return main$1;␊
}␊
␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
␊
module.exports = mainExports;␊
module.exports = main;␊
`,
}

Expand Down Expand Up @@ -6550,20 +6584,25 @@ Generated by [AVA](https://avajs.dev).
{
'main.js': `'use strict';␊
␊
var main = {};␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
var main$1 = {};␊
␊
var hasRequiredMain;␊
␊
function requireMain () {␊
if (hasRequiredMain) return main;␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
main.foo = 'foo';␊
return main;␊
main$1.foo = 'foo';␊
return main$1;␊
}␊
␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
␊
module.exports = mainExports;␊
module.exports = main;␊
`,
}

Expand Down Expand Up @@ -6764,20 +6803,25 @@ Generated by [AVA](https://avajs.dev).
␊
var require$$0__default = /*#__PURE__*/_interopDefaultCompat(require$$0);␊
␊
var main = {};␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
var main$1 = {};␊
␊
var hasRequiredMain;␊
␊
function requireMain () {␊
if (hasRequiredMain) return main;␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
t.is(require$$0__default.default.message, 'it works');␊
return main;␊
return main$1;␊
}␊
␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
␊
module.exports = mainExports;␊
module.exports = main;␊
`,
}

Expand All @@ -6790,7 +6834,11 @@ Generated by [AVA](https://avajs.dev).
␊
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
␊
var main = {};␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
var main$1 = {};␊
␊
var error = {};␊
␊
Expand All @@ -6805,18 +6853,19 @@ Generated by [AVA](https://avajs.dev).
var hasRequiredMain;␊
␊
function requireMain () {␊
if (hasRequiredMain) return main;␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
commonjsGlobal.null = 0;␊
␊
// eslint-disable-next-line global-require␊
t.is(commonjsGlobal.null && requireError(), 0);␊
return main;␊
return main$1;␊
}␊
␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
␊
module.exports = mainExports;␊
module.exports = main;␊
`,
}

Expand Down Expand Up @@ -6853,6 +6902,10 @@ Generated by [AVA](https://avajs.dev).
{
'main.js': `'use strict';␊
␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
var require$1 = {};␊
␊
var hasRequiredRequire;␊
Expand All @@ -6876,10 +6929,11 @@ Generated by [AVA](https://avajs.dev).
}␊
␊
var _importExports = require_import();␊
var esm = /*@__PURE__*/getDefaultExportFromCjs(_importExports);␊
␊
const cjs = requireRequire();␊
␊
t.is(_importExports.foo, 'foo');␊
t.is(esm.foo, 'foo');␊
t.is(cjs.foo, 'foo');␊
`,
}
Expand All @@ -6906,11 +6960,16 @@ Generated by [AVA](https://avajs.dev).
`,
'main.js': `'use strict';␊
␊
var main = require('./generated-main.js');␊
var main$1 = require('./generated-main.js');␊
␊
var mainExports = main.requireMain();␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
module.exports = mainExports;␊
var mainExports = main$1.requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
␊
module.exports = main;␊
`,
'other.js': `'use strict';␊
␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 33ad873

Please sign in to comment.