Skip to content

Commit

Permalink
test(apidom-parser-adapter-asyncapi-yaml-2): run tests in ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Oct 23, 2024
1 parent 0818b0b commit c2a0455
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 145 deletions.
6 changes: 3 additions & 3 deletions packages/apidom-parser-adapter-asyncapi-yaml-2/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**/*.js
/**/*.mjs
/**/*.cjs
/dist
/es
/cjs
/types
/config
/.nyc_output
/node_modules
/**/*.js
5 changes: 3 additions & 2 deletions packages/apidom-parser-adapter-asyncapi-yaml-2/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/src/**/*.mjs
/src/**/*.cjs
/test/**/*.mjs
/dist
/es
/cjs
/types
/NOTICE
/swagger-api-apidom-parser-adapter-asyncapi-yaml-2-*.tgz
5 changes: 3 additions & 2 deletions packages/apidom-parser-adapter-asyncapi-yaml-2/.mocharc.json
Original file line number Diff line number Diff line change
@@ -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"]
}
28 changes: 14 additions & 14 deletions packages/apidom-parser-adapter-asyncapi-yaml-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
"type": "module",
"sideEffects": false,
"unpkg": "./dist/apidom-parser-apdater-asyncapi-yaml-2.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"
},
Expand All @@ -51,9 +51,9 @@
"ramda-adjunct": "^5.0.0"
},
"files": [
"cjs/",
"src/**/*.mjs",
"src/**/*.cjs",
"dist/",
"es/",
"types/dist.d.ts",
"LICENSES",
"NOTICE",
Expand Down
39 changes: 28 additions & 11 deletions packages/apidom-parser-adapter-asyncapi-yaml-2/test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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';
import { isAsyncApi2Element } from '@swagger-api/apidom-ns-asyncapi-2';

import * as adapter from '../src/adapter';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const yamlSpec = fs.readFileSync(path.join(__dirname, 'fixtures', 'sample-api.yaml')).toString();
const jsonSpec = fs.readFileSync(path.join(__dirname, 'fixtures', 'sample-api.json')).toString();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
25 changes: 0 additions & 25 deletions packages/apidom-parser-adapter-asyncapi-yaml-2/test/perf/index.cjs

This file was deleted.

24 changes: 24 additions & 0 deletions packages/apidom-parser-adapter-asyncapi-yaml-2/test/perf/index.ts
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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/asyncapi.yaml');
const source = fs.readFileSync(fixturePath).toString();

Expand All @@ -13,22 +14,22 @@ const options = {
defer: true,
minSamples: 1400,
expected: '1,117 ops/sec ±2.33% (1473 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);
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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/asyncapi.yaml');
const source = fs.readFileSync(fixturePath).toString();

Expand All @@ -14,7 +15,7 @@ const options = {
defer: true,
minSamples: 600,
expected: '8.26 ops/sec ±0.92% (639 runs sampled)',
async fn(deferred) {
async fn(deferred: Deferred) {
await parse(source);
deferred.resolve();
},
Expand All @@ -29,16 +30,16 @@ const options = {
* Refract stage: 2,85 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);
},
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import Benchmark from 'benchmark';
import type { Event } from 'benchmark';
import { ObjectElement, toValue } from '@swagger-api/apidom-core';
import { AsyncApi2Element } from '@swagger-api/apidom-ns-asyncapi-2';

import { parse } from '../../src/adapter';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const fixturePath = path.join(__dirname, 'fixtures/asyncapi.yaml');
const source = fs.readFileSync(fixturePath).toString();
const pojo = toValue((await parse(source)).result);

const genericObjectElement = new ObjectElement(pojo);

const options = {
name: 'refract',
minSamples: 600,
expected: '350 ops/sec ±1.29% (679 runs sampled)',
fn() {
AsyncApi2Element.refract(genericObjectElement);
},
};

export default options;

// we're running as a script
if (import.meta.url === `file://${process.argv[1]}`) {
const bench = new Benchmark({
...options,
onComplete(event: Event) {
console.info(String(event.target));
},
onError(event: Event) {
console.error(event);
},
});
bench.run();
}
Loading

0 comments on commit c2a0455

Please sign in to comment.