diff --git a/.eslintignore b/.eslintignore index 02ffb98..4615618 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,6 @@ coverage/** dist/** e2e/** +package-e2e/** *.js +*.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b3a4f7..a20fb7f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,16 +37,27 @@ jobs: - name: Build package for E2E run: npm run build:e2e - - uses: bahmutov/npm-install@v1 + - name: Packaging tests - Install + uses: bahmutov/npm-install@v1 + with: + useLockFile: false + working-directory: package-e2e + + - name: Packaging tests - Run + run: npm test + working-directory: package-e2e + + - name: E2E tests - Install + uses: bahmutov/npm-install@v1 with: useLockFile: false working-directory: e2e - - name: Record fixtures + - name: E2E tests - Record fixtures run: npm run build working-directory: e2e - - name: Assert no Git changes + - name: E2E tests - Assert no Git changes run: git diff --exit-code e2e/__fixtures__ - name: Upload artifacts @@ -73,7 +84,8 @@ jobs: with: node-version-file: '.nvmrc' - - uses: bahmutov/npm-install@v1 + - name: Install dependencies + uses: bahmutov/npm-install@v1 with: useLockFile: false @@ -84,12 +96,12 @@ jobs: with: node-version-file: 'e2e/.nvmrc' - - name: Update fixtures + - name: E2E tests - Update fixtures run: npm run update-fixtures env: JEST_VERSION: ${{ matrix.jest }} - - name: Assert no Git changes + - name: E2E tests - Assert no Git changes run: git diff --exit-code e2e/__fixtures__ - name: Upload artifacts diff --git a/debug.js b/debug.js index f998187..baedf7d 100644 --- a/debug.js +++ b/debug.js @@ -1,2 +1 @@ -/* Jest 27 fallback */ module.exports = require('./dist/debug'); diff --git a/debug.mjs b/debug.mjs new file mode 100644 index 0000000..2f0cd7c --- /dev/null +++ b/debug.mjs @@ -0,0 +1 @@ +export * from './dist/debug.js'; diff --git a/e2e/reporters/recorder.js b/e2e/reporters/recorder.js index 468f5c8..db16d67 100644 --- a/e2e/reporters/recorder.js +++ b/e2e/reporters/recorder.js @@ -4,7 +4,7 @@ const path = require('path'); const { satisfies: semverSatisfies } = require('semver'); const { version: jestVersion } = require('jest/package.json'); const debugUtils = require('jest-metadata/debug'); -const { JestMetadataReporter } = require('jest-metadata/reporter'); +const JestMetadataReporter = require('jest-metadata/reporter'); const PRESET = process.env.PRESET || ''; const cwd = process.cwd(); diff --git a/environment-decorator.js b/environment-decorator.js index 721ceaf..bc6d9dc 100644 --- a/environment-decorator.js +++ b/environment-decorator.js @@ -1,2 +1 @@ -/* Jest 27 fallback */ -module.exports = require('./dist/environment-decorator'); +module.exports = require('./dist/environment-decorator').default; diff --git a/environment-decorator.mjs b/environment-decorator.mjs new file mode 100644 index 0000000..b7db5bf --- /dev/null +++ b/environment-decorator.mjs @@ -0,0 +1,5 @@ +import environmentDecorator from './dist/environment-decorator.js'; + +const { default: WithMetadata } = environmentDecorator; + +export default WithMetadata; diff --git a/environment-jsdom.js b/environment-jsdom.js index 6568fd3..fa41b5f 100644 --- a/environment-jsdom.js +++ b/environment-jsdom.js @@ -1,2 +1 @@ -/* Jest 27 fallback */ -module.exports = require('./dist/environment-jsdom'); +module.exports = require('./dist/environment-jsdom').default; diff --git a/environment-jsdom.mjs b/environment-jsdom.mjs new file mode 100644 index 0000000..1d95a8a --- /dev/null +++ b/environment-jsdom.mjs @@ -0,0 +1,5 @@ +import environmentJsdom from './dist/environment-jsdom.js'; + +const { default: TestEnvironment } = environmentJsdom; + +export default TestEnvironment; diff --git a/environment-listener.js b/environment-listener.js index 3089cdc..ed62c07 100644 --- a/environment-listener.js +++ b/environment-listener.js @@ -1,2 +1 @@ -/* Jest 27 fallback */ -module.exports = require('./dist/environment-listener'); +module.exports = require('./dist/environment-listener').default; diff --git a/environment-listener.mjs b/environment-listener.mjs new file mode 100644 index 0000000..43d7275 --- /dev/null +++ b/environment-listener.mjs @@ -0,0 +1,5 @@ +import environmentListener from './dist/environment-listener.js'; + +const { default: listener } = environmentListener; + +export default listener; diff --git a/environment-node.js b/environment-node.js index 6e7521a..486e269 100644 --- a/environment-node.js +++ b/environment-node.js @@ -1,2 +1 @@ -/* Jest 27 fallback */ -module.exports = require('./dist/environment-node'); +module.exports = require('./dist/environment-node').default; diff --git a/environment-node.mjs b/environment-node.mjs new file mode 100644 index 0000000..a0826ab --- /dev/null +++ b/environment-node.mjs @@ -0,0 +1,5 @@ +import environmentNode from './dist/environment-node.js'; + +const { default: TestEnvironment } = environmentNode; + +export default TestEnvironment; diff --git a/index.js b/index.js new file mode 100644 index 0000000..f4f0e99 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./dist/index'); diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..2b8395c --- /dev/null +++ b/index.mjs @@ -0,0 +1 @@ +export * from './dist/index.js'; diff --git a/package-e2e/.npmrc b/package-e2e/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/package-e2e/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/package-e2e/package.json b/package-e2e/package.json new file mode 100644 index 0000000..ae01c5d --- /dev/null +++ b/package-e2e/package.json @@ -0,0 +1,15 @@ +{ + "name": "@jest-metadata/package-e2e", + "private": true, + "version": "1.0.0", + "description": "", + "scripts": { + "test": "node test.cjs && node test.mjs && tsc", + "test:cjs": "node test.cjs", + "test:mjs": "node test.mjs", + "test:ts": "tsc" + }, + "dependencies": { + "jest-metadata": "file:../package.tar.gz" + } +} diff --git a/package-e2e/test.cjs b/package-e2e/test.cjs new file mode 100644 index 0000000..9e9010b --- /dev/null +++ b/package-e2e/test.cjs @@ -0,0 +1,30 @@ +const assert = require('assert'); + +const { metadata, state, $Set, $Push, $Merge, $Assign, $Unshift } = require('jest-metadata'); +assert(typeof metadata === 'object', 'jest-metadata should export `metadata` object'); +assert(typeof state === 'object', 'jest-metadata should export `state` object'); +assert(typeof $Set === 'function', 'jest-metadata should export $Set function as a named export'); +assert(typeof $Push === 'function', 'jest-metadata should export $Push function as a named export'); +assert(typeof $Merge === 'function', 'jest-metadata should export $Merge function as a named export'); +assert(typeof $Assign === 'function', 'jest-metadata should export $Assign function as a named export'); +assert(typeof $Unshift === 'function', 'jest-metadata should export $Unshift function as a named export'); + +const { events } = require('jest-metadata/debug'); +assert(typeof events === 'object', 'jest-metadata/debug should export `events` object'); + +const environmentListener = require('jest-metadata/environment-listener'); +assert(typeof environmentListener === 'function', 'jest-metadata/environment-listener should export a class as its default export'); + +const JsdomTestEnvironment = require('jest-metadata/environment-jsdom'); +assert(isClass(JsdomTestEnvironment), 'jest-metadata/environment-jsdom should export a class as its default export'); + +const NodeTestEnvironment = require('jest-metadata/environment-node'); +assert(isClass(NodeTestEnvironment), 'jest-metadata/environment-node should export a class as its default export'); + +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'); + +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 new file mode 100644 index 0000000..add5626 --- /dev/null +++ b/package-e2e/test.mjs @@ -0,0 +1,30 @@ +import assert from 'assert'; +import { $Set, $Push, $Merge, $Assign, $Unshift, state, metadata } from 'jest-metadata'; +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'; + +assert(typeof metadata === 'object', 'jest-metadata should export `metadata` object'); +assert(typeof state === 'object', 'jest-metadata should export `state` object'); +assert(typeof $Set === 'function', 'jest-metadata should export `$Set` function as a named export'); +assert(typeof $Push === 'function', 'jest-metadata should export `$Push` function as a named export'); +assert(typeof $Merge === 'function', 'jest-metadata should export `$Merge` function as a named export'); +assert(typeof $Assign === 'function', 'jest-metadata should export `$Assign` function as a named export'); +assert(typeof $Unshift === 'function', 'jest-metadata should export `$Unshift` function as a named export'); + +assert(typeof events === 'object', 'jest-metadata/debug should export `events` object'); + +assert(typeof environmentListener === 'function', 'jest-metadata/environment-listener should export a function as its default export'); + +assert(isClass(JsdomTestEnvironment), 'jest-metadata/environment-jsdom should export a class as its default export'); + +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'); + +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 new file mode 100644 index 0000000..6e6f9e1 --- /dev/null +++ b/package-e2e/test.ts @@ -0,0 +1,34 @@ +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 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 { + // no-op +} + +assertType(state); +assertType(metadata); +assertType($Set); +assertType($Push); +assertType($Merge); +assertType($Assign); +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(JsdomTestEnvironment); + +assertType(NodeTestEnvironment); + +assertType(environmentListener); diff --git a/package-e2e/tsconfig.json b/package-e2e/tsconfig.json new file mode 100644 index 0000000..6222a7c --- /dev/null +++ b/package-e2e/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "Node16", + "moduleResolution": "Node16", + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "strict": true, + "noEmit": true + }, + "files": [ + "test.ts" + ], +} diff --git a/package.json b/package.json index ee832fa..16e7984 100644 --- a/package.json +++ b/package.json @@ -10,44 +10,45 @@ "src", "dist", "*.js", + "*.mjs", "!**/__utils__", "!**/__tests__", "!**/*.test.*" ], "exports": { ".": { - "import": "./dist/index.js", - "require": "./dist/index.js", + "import": "./index.mjs", + "require": "./index.js", "types": "./dist/index.d.ts" }, "./debug": { - "import": "./dist/debug.js", - "require": "./dist/debug.js", + "import": "./debug.mjs", + "require": "./debug.js", "types": "./dist/debug.d.ts" }, "./environment-decorator": { - "import": "./dist/environment-decorator.js", - "require": "./dist/environment-decorator.js", + "import": "./environment-decorator.mjs", + "require": "./environment-decorator.js", "types": "./dist/environment-decorator.d.ts" }, "./environment-listener": { - "import": "./dist/environment-listener.js", - "require": "./dist/environment-listener.js", + "import": "./environment-listener.mjs", + "require": "./environment-listener.js", "types": "./dist/environment-listener.d.ts" }, "./environment-jsdom": { - "import": "./dist/environment-jsdom.js", - "require": "./dist/environment-jsdom.js", + "import": "./environment-jsdom.mjs", + "require": "./environment-jsdom.js", "types": "./dist/environment-jsdom.d.ts" }, "./environment-node": { - "import": "./dist/environment-node.js", - "require": "./dist/environment-node.js", + "import": "./environment-node.mjs", + "require": "./environment-node.js", "types": "./dist/environment-node.d.ts" }, "./reporter": { - "import": "./dist/reporter.js", - "require": "./dist/reporter.js", + "import": "./reporter.mjs", + "require": "./reporter.js", "types": "./dist/reporter.d.ts" }, "./package.json": "./package.json" diff --git a/reporter.js b/reporter.js index 920d4a3..0378218 100644 --- a/reporter.js +++ b/reporter.js @@ -1,2 +1 @@ -/* Jest 27 fallback */ -module.exports = require('./dist/reporter'); +module.exports = require('./dist/reporter').default; diff --git a/reporter.mjs b/reporter.mjs new file mode 100644 index 0000000..c2d37c8 --- /dev/null +++ b/reporter.mjs @@ -0,0 +1,5 @@ +import reporter from './dist/reporter.js'; + +const { default: JestMetadataReporter } = reporter; + +export default JestMetadataReporter; diff --git a/src/environment-decorator.ts b/src/environment-decorator.ts index 2dde039..c91c382 100644 --- a/src/environment-decorator.ts +++ b/src/environment-decorator.ts @@ -2,6 +2,8 @@ import type { JestEnvironment } from '@jest/environment'; import WithEmitter from 'jest-environment-emit'; import listener from './environment-listener'; -export const WithMetadata = ( +const WithMetadata = ( JestEnvironmentClass: new (...args: any[]) => E, ) => WithEmitter(JestEnvironmentClass, listener, 'WithMetadata'); + +export default WithMetadata; diff --git a/src/environment-jsdom.ts b/src/environment-jsdom.ts index 7c839e0..5d7c74c 100644 --- a/src/environment-jsdom.ts +++ b/src/environment-jsdom.ts @@ -1,5 +1,4 @@ import JestEnvironmentJsdom from 'jest-environment-jsdom'; -import { WithMetadata } from './environment-decorator'; +import WithMetadata from './environment-decorator'; -export const TestEnvironment = WithMetadata(JestEnvironmentJsdom); -export default TestEnvironment; +export default WithMetadata(JestEnvironmentJsdom); diff --git a/src/environment-node.ts b/src/environment-node.ts index e6f2ccc..d10e878 100644 --- a/src/environment-node.ts +++ b/src/environment-node.ts @@ -1,5 +1,4 @@ import JestEnvironmentNode from 'jest-environment-node'; -import { WithMetadata } from './environment-decorator'; +import WithMetadata from './environment-decorator'; -export const TestEnvironment = WithMetadata(JestEnvironmentNode); -export default TestEnvironment; +export default WithMetadata(JestEnvironmentNode); diff --git a/src/reporter.ts b/src/reporter.ts index 280f7a4..1ede14f 100644 --- a/src/reporter.ts +++ b/src/reporter.ts @@ -17,12 +17,12 @@ const realm = unknownRealm as ParentProcessRealm; detectDuplicateRealms(true); -export const query = realm.query; - /** * @implements {import('@jest/reporters').Reporter} */ -export class JestMetadataReporter implements Reporter { +export default class JestMetadataReporter implements Reporter { + static readonly query = realm.query; + constructor(_globalConfig: Config.GlobalConfig) { if (realm.type !== 'parent_process') { throw new JestMetadataError(`JestMetadataReporter can be used only in the parent process`); @@ -79,5 +79,3 @@ export class JestMetadataReporter implements Reporter { return realm.reporterServer.onRunComplete(); } } - -export default JestMetadataReporter;