Skip to content

Commit

Permalink
fix: improve CJS and ESM exports compatibility with older version (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Dec 8, 2023
1 parent 97cedac commit ac1346d
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ e2e/**
package-e2e/**
*.js
*.mjs
*.d.ts
1 change: 1 addition & 0 deletions debug.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist/debug';
1 change: 1 addition & 0 deletions environment-decorator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './dist/environment-decorator';
1 change: 1 addition & 0 deletions environment-jsdom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './dist/environment-jsdom';
1 change: 1 addition & 0 deletions environment-listener.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './dist/environment-listener';
1 change: 1 addition & 0 deletions environment-node.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './dist/environment-node';
3 changes: 2 additions & 1 deletion package-e2e/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 4 additions & 2 deletions package-e2e/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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));
Expand Down
17 changes: 9 additions & 8 deletions package-e2e/test.ts
Original file line number Diff line number Diff line change
@@ -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<T>(_actual: T): void {
function assertType<T>(_actual: T, _other?: T): void {
// no-op
}

Expand All @@ -20,12 +20,13 @@ assertType<Function>($Unshift);

assertType<object>(events);

assertType<Function>(JestMetadataReporter);
assertType<Function>(JestMetadataReporter.query.globalMetadata);
assertType<Function>(JestMetadataReporter.query.test);
assertType<Function>(JestMetadataReporter.query.filePath);
assertType<Function>(JestMetadataReporter.query.testResult);
assertType<Function>(JestMetadataReporter.query.testCaseResult);
assertType<Function>(JestMetadataReporter, JestMetadataReporterNamed);
assertType<object>(JestMetadataReporter.query, query);
assertType<Function>(query.globalMetadata);
assertType<Function>(query.test);
assertType<Function>(query.filePath);
assertType<Function>(query.testResult);
assertType<Function>(query.testCaseResult);

assertType<Function>(JsdomTestEnvironment);

Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"src",
"dist",
"*.js",
"*.d.ts",
"*.mjs",
"!**/__utils__",
"!**/__tests__",
Expand All @@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions reporter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, JestMetadataReporter, query } from './dist/reporter';
2 changes: 2 additions & 0 deletions reporter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import reporter from './dist/reporter.js';

const { default: JestMetadataReporter } = reporter;

const query = JestMetadataReporter.query;
export { query, JestMetadataReporter };
export default JestMetadataReporter;
14 changes: 13 additions & 1 deletion src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -79,3 +89,5 @@ export default class JestMetadataReporter implements Reporter {
return realm.reporterServer.onRunComplete();
}
}

export default JestMetadataReporter;

0 comments on commit ac1346d

Please sign in to comment.