From ac1346dd7a8c21fde555d2d904e8818e5501a837 Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Fri, 8 Dec 2023 22:03:58 +0200 Subject: [PATCH] fix: improve CJS and ESM exports compatibility with older version (#69) --- .eslintignore | 1 + debug.d.ts | 1 + environment-decorator.d.ts | 1 + environment-jsdom.d.ts | 1 + environment-listener.d.ts | 1 + environment-node.d.ts | 1 + package-e2e/test.cjs | 3 ++- package-e2e/test.mjs | 6 ++++-- package-e2e/test.ts | 17 +++++++++-------- package.json | 13 +++++++------ reporter.d.ts | 1 + reporter.mjs | 2 ++ src/reporter.ts | 14 +++++++++++++- 13 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 debug.d.ts create mode 100644 environment-decorator.d.ts create mode 100644 environment-jsdom.d.ts create mode 100644 environment-listener.d.ts create mode 100644 environment-node.d.ts create mode 100644 reporter.d.ts diff --git a/.eslintignore b/.eslintignore index 4615618..b6459f0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,3 +4,4 @@ e2e/** package-e2e/** *.js *.mjs +*.d.ts diff --git a/debug.d.ts b/debug.d.ts new file mode 100644 index 0000000..9307518 --- /dev/null +++ b/debug.d.ts @@ -0,0 +1 @@ +export * from './dist/debug'; diff --git a/environment-decorator.d.ts b/environment-decorator.d.ts new file mode 100644 index 0000000..038e8c3 --- /dev/null +++ b/environment-decorator.d.ts @@ -0,0 +1 @@ +export { default } from './dist/environment-decorator'; diff --git a/environment-jsdom.d.ts b/environment-jsdom.d.ts new file mode 100644 index 0000000..e2ccd92 --- /dev/null +++ b/environment-jsdom.d.ts @@ -0,0 +1 @@ +export { default } from './dist/environment-jsdom'; diff --git a/environment-listener.d.ts b/environment-listener.d.ts new file mode 100644 index 0000000..30deb3f --- /dev/null +++ b/environment-listener.d.ts @@ -0,0 +1 @@ +export { default } from './dist/environment-listener'; diff --git a/environment-node.d.ts b/environment-node.d.ts new file mode 100644 index 0000000..879fdfe --- /dev/null +++ b/environment-node.d.ts @@ -0,0 +1 @@ +export { default } from './dist/environment-node'; diff --git a/package-e2e/test.cjs b/package-e2e/test.cjs index 9e9010b..90987a1 100644 --- a/package-e2e/test.cjs +++ b/package-e2e/test.cjs @@ -23,7 +23,8 @@ assert(isClass(NodeTestEnvironment), 'jest-metadata/environment-node should expo const JestMetadataReporter = require('jest-metadata/reporter'); assert(isClass(JestMetadataReporter), 'jest-metadata/reporter should export a class as its default export'); -assert(typeof JestMetadataReporter.query === 'object', 'jest-metadata/reporter class should export .query helper'); +assert(JestMetadataReporter.JestMetadataReporter, 'jest-metadata/reporter should export `JestMetadataReporter` class as a named export'); +assert(typeof JestMetadataReporter.query === 'object', 'jest-metadata/reporter should export query helper'); function isClass(obj) { return typeof obj === 'function' && /^class\s/.test(Function.prototype.toString.call(obj)); diff --git a/package-e2e/test.mjs b/package-e2e/test.mjs index add5626..5a94f95 100644 --- a/package-e2e/test.mjs +++ b/package-e2e/test.mjs @@ -4,7 +4,7 @@ import { events } from 'jest-metadata/debug'; import JsdomTestEnvironment from 'jest-metadata/environment-jsdom'; import NodeTestEnvironment from 'jest-metadata/environment-node'; import environmentListener from 'jest-metadata/environment-listener'; -import JestMetadataReporter from 'jest-metadata/reporter'; +import JestMetadataReporter, { query, JestMetadataReporter as JestMetadataReporterNamed } from 'jest-metadata/reporter'; assert(typeof metadata === 'object', 'jest-metadata should export `metadata` object'); assert(typeof state === 'object', 'jest-metadata should export `state` object'); @@ -23,7 +23,9 @@ assert(isClass(JsdomTestEnvironment), 'jest-metadata/environment-jsdom should ex assert(isClass(NodeTestEnvironment), 'jest-metadata/environment-node should export a class as its default export'); assert(isClass(JestMetadataReporter), 'jest-metadata/reporter should export a class as its default export'); -assert(typeof JestMetadataReporter.query === 'object', 'jest-metadata/reporter class should export .query helper'); +assert(isClass(JestMetadataReporterNamed), 'jest-metadata/reporter should export `JestMetadataReporter` class as a named export'); +assert(typeof query === 'object', 'jest-metadata/reporter should export query helper'); +assert(query === JestMetadataReporter.query, 'jest-metadata/reporter class should have query helper as a static property'); function isClass(obj) { return typeof obj === 'function' && /^class\s/.test(Function.prototype.toString.call(obj)); diff --git a/package-e2e/test.ts b/package-e2e/test.ts index 6e6f9e1..2bb8542 100644 --- a/package-e2e/test.ts +++ b/package-e2e/test.ts @@ -1,12 +1,12 @@ import { $Set, $Push, $Merge, $Assign, $Unshift, state, metadata } from 'jest-metadata'; import { events } from 'jest-metadata/debug'; import type { GlobalMetadata, Metadata } from 'jest-metadata'; -import JestMetadataReporter from 'jest-metadata/reporter'; +import JestMetadataReporter, { query, JestMetadataReporter as JestMetadataReporterNamed } from 'jest-metadata/reporter'; import JsdomTestEnvironment from 'jest-metadata/environment-jsdom'; import NodeTestEnvironment from 'jest-metadata/environment-node'; import environmentListener from 'jest-metadata/environment-listener'; -function assertType(_actual: T): void { +function assertType(_actual: T, _other?: T): void { // no-op } @@ -20,12 +20,13 @@ assertType($Unshift); assertType(events); -assertType(JestMetadataReporter); -assertType(JestMetadataReporter.query.globalMetadata); -assertType(JestMetadataReporter.query.test); -assertType(JestMetadataReporter.query.filePath); -assertType(JestMetadataReporter.query.testResult); -assertType(JestMetadataReporter.query.testCaseResult); +assertType(JestMetadataReporter, JestMetadataReporterNamed); +assertType(JestMetadataReporter.query, query); +assertType(query.globalMetadata); +assertType(query.test); +assertType(query.filePath); +assertType(query.testResult); +assertType(query.testCaseResult); assertType(JsdomTestEnvironment); diff --git a/package.json b/package.json index 16e7984..4eccdf3 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "src", "dist", "*.js", + "*.d.ts", "*.mjs", "!**/__utils__", "!**/__tests__", @@ -24,32 +25,32 @@ "./debug": { "import": "./debug.mjs", "require": "./debug.js", - "types": "./dist/debug.d.ts" + "types": "./debug.d.ts" }, "./environment-decorator": { "import": "./environment-decorator.mjs", "require": "./environment-decorator.js", - "types": "./dist/environment-decorator.d.ts" + "types": "./environment-decorator.d.ts" }, "./environment-listener": { "import": "./environment-listener.mjs", "require": "./environment-listener.js", - "types": "./dist/environment-listener.d.ts" + "types": "./environment-listener.d.ts" }, "./environment-jsdom": { "import": "./environment-jsdom.mjs", "require": "./environment-jsdom.js", - "types": "./dist/environment-jsdom.d.ts" + "types": "./environment-jsdom.d.ts" }, "./environment-node": { "import": "./environment-node.mjs", "require": "./environment-node.js", - "types": "./dist/environment-node.d.ts" + "types": "./environment-node.d.ts" }, "./reporter": { "import": "./reporter.mjs", "require": "./reporter.js", - "types": "./dist/reporter.d.ts" + "types": "./reporter.d.ts" }, "./package.json": "./package.json" }, diff --git a/reporter.d.ts b/reporter.d.ts new file mode 100644 index 0000000..cd2fc60 --- /dev/null +++ b/reporter.d.ts @@ -0,0 +1 @@ +export { default, JestMetadataReporter, query } from './dist/reporter'; diff --git a/reporter.mjs b/reporter.mjs index c2d37c8..804da45 100644 --- a/reporter.mjs +++ b/reporter.mjs @@ -2,4 +2,6 @@ import reporter from './dist/reporter.js'; const { default: JestMetadataReporter } = reporter; +const query = JestMetadataReporter.query; +export { query, JestMetadataReporter }; export default JestMetadataReporter; diff --git a/src/reporter.ts b/src/reporter.ts index 1ede14f..7162e9e 100644 --- a/src/reporter.ts +++ b/src/reporter.ts @@ -12,15 +12,18 @@ import type { import { JestMetadataError } from './errors'; import { detectDuplicateRealms, realm as unknownRealm } from './realms'; import type { ParentProcessRealm } from './realms'; +import { logger } from './utils'; const realm = unknownRealm as ParentProcessRealm; detectDuplicateRealms(true); +export const query = realm.query; + /** * @implements {import('@jest/reporters').Reporter} */ -export default class JestMetadataReporter implements Reporter { +export class JestMetadataReporter implements Reporter { static readonly query = realm.query; constructor(_globalConfig: Config.GlobalConfig) { @@ -29,6 +32,13 @@ export default class JestMetadataReporter implements Reporter { } } + static get JestMetadataReporter() { + logger.warn( + `Don't use named export 'JestMetadataReporter' from 'jest-metadata/reporter'. Use default export instead.`, + ); + return JestMetadataReporter; + } + getLastError(): Error | void { return undefined; } @@ -79,3 +89,5 @@ export default class JestMetadataReporter implements Reporter { return realm.reporterServer.onRunComplete(); } } + +export default JestMetadataReporter;