From 1d81d728999c5edf43bf3e7d515f7e2bcfdce3bb Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Wed, 23 Oct 2024 13:03:56 +0200 Subject: [PATCH] test(apidom-parser-adapter-openapi-yaml3-1): run tests in ESM mode --- .../.eslintignore | 6 +-- .../.gitignore | 5 ++- .../.mocharc.json | 5 ++- .../package.json | 28 ++++++------- .../test/.eslintrc | 39 +++++++++++++------ .../{adapter.ts.snap => adapter.mjs.snap} | 0 .../test/adapter.ts | 2 + .../test/mocha-bootstrap.cjs | 15 ------- .../test/mocha-bootstrap.ts | 11 ++++++ .../test/perf/index.cjs | 25 ------------ .../test/perf/index.ts | 24 ++++++++++++ ...xical-analysis.cjs => lexical-analysis.ts} | 23 +++++------ .../test/perf/{parse.cjs => parse.ts} | 23 +++++------ .../test/perf/{refract.cjs => refract.ts} | 23 +++++------ ...tic-analysis.cjs => syntactic-analysis.ts} | 23 +++++------ 15 files changed, 136 insertions(+), 116 deletions(-) rename packages/apidom-parser-adapter-openapi-yaml-3-1/test/__snapshots__/{adapter.ts.snap => adapter.mjs.snap} (100%) delete mode 100644 packages/apidom-parser-adapter-openapi-yaml-3-1/test/mocha-bootstrap.cjs create mode 100644 packages/apidom-parser-adapter-openapi-yaml-3-1/test/mocha-bootstrap.ts delete mode 100644 packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/index.cjs create mode 100644 packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/index.ts rename packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/{lexical-analysis.cjs => lexical-analysis.ts} (50%) rename packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/{parse.cjs => parse.ts} (59%) rename packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/{refract.cjs => refract.ts} (53%) rename packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/{syntactic-analysis.cjs => syntactic-analysis.ts} (52%) diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/.eslintignore b/packages/apidom-parser-adapter-openapi-yaml-3-1/.eslintignore index 856320e627..23853b909a 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/.eslintignore +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/.eslintignore @@ -1,8 +1,8 @@ +/**/*.js +/**/*.mjs +/**/*.cjs /dist -/es -/cjs /types /config /.nyc_output /node_modules -/**/*.js diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/.gitignore b/packages/apidom-parser-adapter-openapi-yaml-3-1/.gitignore index 2a9144adca..9730127d8b 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/.gitignore +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/.gitignore @@ -1,6 +1,7 @@ +/src/**/*.mjs +/src/**/*.cjs +/test/**/*.mjs /dist -/es -/cjs /types /NOTICE /swagger-api-apidom-parser-adapter-openapi-yaml-3-1-*.tgz diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/.mocharc.json b/packages/apidom-parser-adapter-openapi-yaml-3-1/.mocharc.json index e923c24114..edb4cc79f2 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/.mocharc.json +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/.mocharc.json @@ -1,5 +1,6 @@ { "recursive": true, - "spec": "test/**/*.ts", - "file": ["test/mocha-bootstrap.cjs"] + "spec": "test/**/*.mjs", + "file": ["test/mocha-bootstrap.mjs"], + "ignore": ["test/perf/**/*.mjs"] } diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/package.json b/packages/apidom-parser-adapter-openapi-yaml-3-1/package.json index 98a866f117..82da8bd852 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/package.json +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/package.json @@ -9,29 +9,29 @@ "type": "module", "sideEffects": false, "unpkg": "./dist/apidom-parser-apdater-openapi-yaml-3-1.browser.min.js", - "main": "./cjs/adapter.cjs", + "main": "./src/adapter.cjs", "exports": { "types": "./types/dist.d.ts", - "import": "./es/adapter.mjs", - "require": "./cjs/adapter.cjs" + "import": "./src/adapter.mjs", + "require": "./src/adapter.cjs" }, "types": "./types/dist.d.ts", "scripts": { "build": "npm run clean && run-p --max-parallel ${CPU_CORES:-2} typescript:declaration build:es build:cjs build:umd:browser", - "build:es": "cross-env BABEL_ENV=es babel src --out-dir es --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward'", - "build:cjs": "cross-env BABEL_ENV=cjs babel src --out-dir cjs --extensions '.ts' --out-file-extension '.cjs' --root-mode 'upward'", + "build:es": "cross-env BABEL_ENV=es babel src --out-dir src --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward'", + "build:cjs": "cross-env BABEL_ENV=cjs babel src --out-dir src --extensions '.ts' --out-file-extension '.cjs' --root-mode 'upward'", "build:umd:browser": "cross-env BABEL_ENV=browser webpack --config config/webpack/browser.config.js --progress", "lint": "eslint ./", "lint:fix": "eslint ./ --fix", - "clean": "rimraf ./es ./cjs ./dist ./types", + "clean": "rimraf --glob 'src/**/*.mjs' 'src/**/*.cjs' 'test/**/*.mjs' ./dist ./types", "typescript:check-types": "tsc --noEmit", "typescript:declaration": "tsc -p declaration.tsconfig.json && rollup -c config/rollup/types.dist.js", - "test": "cross-env NODE_ENV=test BABEL_ENV=cjs mocha", - "perf": "cross-env NODE_ENV=test BABEL_ENV=cjs node ./test/perf/index.cjs", - "perf:lexical-analysis": "cross-env NODE_ENV=test BABEL_ENV=cjs node ./test/perf/lexical-analysis.cjs", - "perf:syntactic-analysis": "cross-env NODE_ENV=test BABEL_ENV=cjs node ./test/perf/syntactic-analysis.cjs", - "perf:refract": "cross-env NODE_ENV=test BABEL_ENV=cjs node ./test/perf/refract.cjs", - "perf:parse": "cross-env NODE_ENV=test BABEL_ENV=cjs node ./test/perf/parse.cjs", + "test": "npm run build:es && cross-env BABEL_ENV=es babel test --out-dir test --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward' && cross-env NODE_ENV=test mocha", + "perf": "cross-env BABEL_ENV=es babel ./test/perf/index.ts --out-file ./test/perf/index.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/index.mjs", + "perf:lexical-analysis": "cross-env BABEL_ENV=es babel ./test/perf/lexical-analysis.ts --out-file ./test/perf/lexical-analysis.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/lexical-analysis.mjs", + "perf:syntactic-analysis": "cross-env BABEL_ENV=es babel ./test/perf/syntactic-analysis.ts --out-file ./test/perf/syntactic-analysis.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/syntactic-analysis.mjs", + "perf:refract": "cross-env BABEL_ENV=es babel ./test/perf/refract.ts --out-file ./test/perf/refract.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/refract.mjs", + "perf:parse": "cross-env BABEL_ENV=es babel ./test/perf/parse.ts --out-file ./test/perf/parse.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/parse.mjs", "prepack": "copyfiles -u 3 ../../LICENSES/* LICENSES && copyfiles -u 2 ../../NOTICE .", "postpack": "rimraf NOTICE LICENSES" }, @@ -51,9 +51,9 @@ "ramda-adjunct": "^5.0.0" }, "files": [ - "cjs/", + "src/**/*.mjs", + "src/**/*.cjs", "dist/", - "es/", "types/dist.d.ts", "LICENSES", "NOTICE", diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/.eslintrc b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/.eslintrc index 85759a803c..c47eea4f48 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/.eslintrc +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/.eslintrc @@ -24,15 +24,32 @@ "mocha/no-top-level-hooks": 2, "mocha/no-identical-title": 2, "mocha/no-nested-tests": 2, - "mocha/no-exclusive-tests": 2 - }, - "overrides": [{ - "files": ["mocha-bootstrap.cjs"], - "parserOptions": { - "sourceType": "script" - }, - "rules": { - "@typescript-eslint/no-var-requires": 0 - } - }] + "mocha/no-exclusive-tests": 2, + "no-underscore-dangle": 0, + "import/no-relative-packages": 0, + "@typescript-eslint/naming-convention": [ + "error", + { + "selector": "variable", + "format": ["camelCase", "PascalCase", "UPPER_CASE"], + "leadingUnderscore": "forbid" + }, + { + "selector": "variable", + "format": null, + "filter": { + "regex": "^__dirname$", + "match": true + } + }, + { + "selector": "variable", + "format": null, + "filter": { + "regex": "^__filename$", + "match": true + } + } + ] + } } diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/__snapshots__/adapter.ts.snap b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/__snapshots__/adapter.mjs.snap similarity index 100% rename from packages/apidom-parser-adapter-openapi-yaml-3-1/test/__snapshots__/adapter.ts.snap rename to packages/apidom-parser-adapter-openapi-yaml-3-1/test/__snapshots__/adapter.mjs.snap diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/adapter.ts b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/adapter.ts index bf19ea3e60..1b49ad0904 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/adapter.ts +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/adapter.ts @@ -1,5 +1,6 @@ import fs from 'node:fs'; import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import { assert, expect } from 'chai'; import dedent from 'dedent'; import { isParseResultElement, SourceMapElement, sexprs } from '@swagger-api/apidom-core'; @@ -7,6 +8,7 @@ import { isOpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1'; import * as adapter from '../src/adapter'; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const jsonSpec = fs.readFileSync(path.join(__dirname, 'fixtures', 'sample-api.json')).toString(); const yamlSpec = fs.readFileSync(path.join(__dirname, 'fixtures', 'sample-api.yaml')).toString(); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/mocha-bootstrap.cjs b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/mocha-bootstrap.cjs deleted file mode 100644 index bc8ce3c8fc..0000000000 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/mocha-bootstrap.cjs +++ /dev/null @@ -1,15 +0,0 @@ -/* eslint-disable */ -require('@babel/register')({ extensions: ['.js', '.ts'], rootMode: 'upward' }); - -/** - * Configure snapshot testing. - */ -const chai = require('chai'); -const { jestSnapshotPlugin, addSerializer } = require('mocha-chai-jest-snapshot'); - -const jestApiDOMSerializer = require('../../../scripts/jest-serializer-apidom.cjs'); -const jestStringSerializer = require('../../../scripts/jest-serializer-string.cjs'); - -chai.use(jestSnapshotPlugin()); -addSerializer(jestApiDOMSerializer); -addSerializer(jestStringSerializer); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/mocha-bootstrap.ts b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/mocha-bootstrap.ts new file mode 100644 index 0000000000..4f8d8adf2e --- /dev/null +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/mocha-bootstrap.ts @@ -0,0 +1,11 @@ +import * as chai from 'chai'; +import { jestSnapshotPlugin, addSerializer } from 'mocha-chai-jest-snapshot'; + +// @ts-ignore +import * as jestApiDOMSerializer from '../../../scripts/jest-serializer-apidom'; +// @ts-ignore +import * as jestStringSerializer from '../../../scripts/jest-serializer-string'; + +chai.use(jestSnapshotPlugin()); +addSerializer(jestApiDOMSerializer); +addSerializer(jestStringSerializer); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/index.cjs b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/index.cjs deleted file mode 100644 index dd90fdf235..0000000000 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/index.cjs +++ /dev/null @@ -1,25 +0,0 @@ -require('@babel/register')({ extensions: ['.js', '.ts'], rootMode: 'upward' }); - -const Benchmark = require('benchmark'); - -const lexicalAnalysisBench = require('./lexical-analysis.cjs'); -const syntacticAnalysisBench = require('./syntactic-analysis.cjs'); -const refractBench = require('./refract.cjs'); -const parseBench = require('./parse.cjs'); - -const suite = new Benchmark.Suite(); - -suite - .add(lexicalAnalysisBench) - .add(syntacticAnalysisBench) - .add(refractBench) - .add(parseBench) - // add listeners - .on('cycle', function (event) { - console.info(String(event.target)); - }) - .on('complete', function () { - console.info('\nAll benchmarks have completed'); - }) - // run - .run(); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/index.ts b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/index.ts new file mode 100644 index 0000000000..159fc5ac0c --- /dev/null +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/index.ts @@ -0,0 +1,24 @@ +import Benchmark from 'benchmark'; +import type { Event } from 'benchmark'; + +import lexicalAnalysisBench from './lexical-analysis'; +import syntacticAnalysisBench from './syntactic-analysis'; +import refractBench from './refract'; +import parseBench from './parse'; + +const suite = new Benchmark.Suite(); + +suite + .add(lexicalAnalysisBench) + .add(syntacticAnalysisBench) + .add(refractBench) + .add(parseBench) + // add listeners + .on('cycle', function (event: Event) { + console.info(String(event.target)); + }) + .on('complete', function () { + console.info('\nAll benchmarks have completed'); + }) + // run + .run(); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/lexical-analysis.cjs b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/lexical-analysis.ts similarity index 50% rename from packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/lexical-analysis.cjs rename to packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/lexical-analysis.ts index c36cefbafa..aa0f373299 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/lexical-analysis.cjs +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/lexical-analysis.ts @@ -1,10 +1,11 @@ -require('@babel/register')({ extensions: ['.js', '.ts'], rootMode: 'upward' }); - -const fs = require('node:fs'); -const path = require('node:path'); -const Benchmark = require('benchmark'); -const { lexicalAnalysis } = require('@swagger-api/apidom-parser-adapter-yaml-1-2'); +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import Benchmark from 'benchmark'; +import type { Deferred, Event } from 'benchmark'; +import { lexicalAnalysis } from '@swagger-api/apidom-parser-adapter-yaml-1-2'; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const fixturePath = path.join(__dirname, 'fixtures/openapi.yaml'); const source = fs.readFileSync(fixturePath).toString(); @@ -13,22 +14,22 @@ const options = { defer: true, minSamples: 1400, expected: '670 ops/sec ±2.63% (1469 runs sampled)', - async fn(deferred) { + async fn(deferred: Deferred) { await lexicalAnalysis(source); deferred.resolve(); }, }; -module.exports = options; +export default options; // we're running as a script -if (module.parent === null) { +if (import.meta.url === `file://${process.argv[1]}`) { const bench = new Benchmark({ ...options, - onComplete(event) { + onComplete(event: Event) { console.info(String(event.target)); }, - onError(event) { + onError(event: Event) { console.error(event); }, }); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/parse.cjs b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/parse.ts similarity index 59% rename from packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/parse.cjs rename to packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/parse.ts index 9756af5180..976e280eb1 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/parse.cjs +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/parse.ts @@ -1,11 +1,12 @@ -require('@babel/register')({ extensions: ['.js', '.ts'], rootMode: 'upward' }); +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import Benchmark from 'benchmark'; +import type { Deferred, Event } from 'benchmark'; -const fs = require('node:fs'); -const path = require('node:path'); -const Benchmark = require('benchmark'); - -const { parse } = require('../../src/adapter'); +import { parse } from '../../src/adapter'; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const fixturePath = path.join(__dirname, 'fixtures/openapi.yaml'); const source = fs.readFileSync(fixturePath).toString(); @@ -14,7 +15,7 @@ const options = { defer: true, minSamples: 600, expected: '4.25 ops/sec ±0.72% (620 runs sampled)', - async fn(deferred) { + async fn(deferred: Deferred) { await parse(source); deferred.resolve(); }, @@ -29,16 +30,16 @@ const options = { * Refract stage: 18,18 ms */ -module.exports = options; +export default options; // we're running as a script -if (module.parent === null) { +if (import.meta.url === `file://${process.argv[1]}`) { const bench = new Benchmark({ ...options, - onComplete(event) { + onComplete(event: Event) { console.info(String(event.target)); }, - onError(event) { + onError(event: Event) { console.error(event); }, }); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/refract.cjs b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/refract.ts similarity index 53% rename from packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/refract.cjs rename to packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/refract.ts index d554f6eaa0..59f4f1969a 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/refract.cjs +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/refract.ts @@ -1,11 +1,12 @@ -require('@babel/register')({ extensions: ['.js', '.ts'], rootMode: 'upward' }); - -const fs = require('node:fs'); -const path = require('node:path'); -const Benchmark = require('benchmark'); -const { ObjectElement } = require('@swagger-api/apidom-core'); -const { OpenApi3_1Element } = require('@swagger-api/apidom-ns-openapi-3-1'); +import fs from 'node:fs'; +import path from 'node:path'; +import Benchmark from 'benchmark'; +import type { Event } from 'benchmark'; +import { ObjectElement } from '@swagger-api/apidom-core'; +import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1'; +import { fileURLToPath } from 'node:url'; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const fixturePath = path.join(__dirname, 'fixtures/openapi.json'); const source = fs.readFileSync(fixturePath).toString(); const pojo = JSON.parse(source); @@ -20,16 +21,16 @@ const options = { }, }; -module.exports = options; +export default options; // we're running as a script -if (module.parent === null) { +if (import.meta.url === `file://${process.argv[1]}`) { const bench = new Benchmark({ ...options, - onComplete(event) { + onComplete(event: Event) { console.info(String(event.target)); }, - onError(event) { + onError(event: Event) { console.error(event); }, }); diff --git a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/syntactic-analysis.cjs b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/syntactic-analysis.ts similarity index 52% rename from packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/syntactic-analysis.cjs rename to packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/syntactic-analysis.ts index f231b96cc0..fb67760e2f 100644 --- a/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/syntactic-analysis.cjs +++ b/packages/apidom-parser-adapter-openapi-yaml-3-1/test/perf/syntactic-analysis.ts @@ -1,10 +1,11 @@ -require('@babel/register')({ extensions: ['.js', '.ts'], rootMode: 'upward' }); - -const fs = require('node:fs'); -const path = require('node:path'); -const Benchmark = require('benchmark'); -const { lexicalAnalysis, syntacticAnalysis } = require('@swagger-api/apidom-parser-adapter-yaml-1-2'); +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import Benchmark from 'benchmark'; +import type { Deferred, Event } from 'benchmark'; +import { lexicalAnalysis, syntacticAnalysis } from '@swagger-api/apidom-parser-adapter-yaml-1-2'; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const fixturePath = path.join(__dirname, 'fixtures/openapi.yaml'); const source = fs.readFileSync(fixturePath).toString(); const cstP = lexicalAnalysis(source); @@ -14,23 +15,23 @@ const options = { defer: true, minSamples: 600, expected: '4.63 ops/sec ±0.95% (622 runs sampled)', - async fn(deferred) { + async fn(deferred: Deferred) { const cst = await cstP; syntacticAnalysis(cst); deferred.resolve(); }, }; -module.exports = options; +export default options; // we're running as a script -if (module.parent === null) { +if (import.meta.url === `file://${process.argv[1]}`) { const bench = new Benchmark({ ...options, - onComplete(event) { + onComplete(event: Event) { console.info(String(event.target)); }, - onError(event) { + onError(event: Event) { console.error(event); }, });