Skip to content

Commit

Permalink
feat(test): moving unit testing over to vitest (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored May 25, 2024
1 parent ff9523c commit d43f6e5
Show file tree
Hide file tree
Showing 141 changed files with 3,256 additions and 3,889 deletions.
49 changes: 49 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"root": true,
"extends": [
"@readme/eslint-config",
"@readme/eslint-config/typescript",
],
"env": {
"browser": true,
"node": true
},
"rules": {
"no-plusplus": "off",
"no-restricted-syntax": "off",
"no-underscore-dangle": "off",
"no-use-before-define": "off",
"prefer-rest-params": "off",
"prefer-spread": "off"
},
"overrides": [
{
// The typings in this file are pretty bad right now, when we have native types we can
// remove this.
"files": ["lib/index.d.ts"],
"rules": {
"@typescript-eslint/consistent-indexed-object-style": "off",
"@typescript-eslint/consistent-type-imports": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/sort-type-constituents": "off",
"eslint-comments/no-unused-disable": "off",
"lines-between-class-members": "off",
"max-classes-per-file": "off",
"quotes": "off",
"typescript-sort-keys/interface": "off"
}
},
{
// These can all get removed when the library is moved over to native TS.
"files": ["*.js"],
"rules": {
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-var-requires": "off",
"eslint-comments/no-unused-disable": "off",
"func-names": "off"
}
}
]
}
13 changes: 0 additions & 13 deletions .eslintrc.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*.js text eol=lf
*.jsx text eol=lf
*.ts text eol=lf
*.mts text eol=lf
*.tsx text eol=lf
*.json text eol=lf
*.yml text eol=lf
Expand Down
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
directory: '/'
schedule:
interval: monthly
reviewers:
Expand All @@ -13,7 +13,7 @@ updates:
prefix-development: chore(deps-dev)

- package-ecosystem: npm
directory: "/"
directory: '/'
schedule:
interval: monthly
open-pull-requests-limit: 10
Expand Down
14 changes: 4 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,11 @@ jobs:
node-version: ${{ matrix.node }}
cache: npm

- name: Install dependencies
run: npm ci
- run: npm ci
- run: npm run lint

- name: Run linter
run: npm run lint

- name: Run TypeScript tests
run: npm run test:typescript

- name: Run Node tests
run: npm run coverage
- name: Run tests
run: npm run test --ignore-scripts
env:
# `chalk` has troubles with color detection while on CI and also in how it's used within our tests.
# https://github.com/chalk/supports-color/issues/106
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ nbproject
# IDEs & Text Editors
.idea
.sublime-*
.vscode/settings.json
.netbeans
nbproject

Expand Down
10 changes: 0 additions & 10 deletions .mocharc.yml

This file was deleted.

6 changes: 1 addition & 5 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
.github/
.husky/
.nyc_output/
coverage/
.eslint*
.mocha*
.nyc*
debug.js
vitest.*
10 changes: 0 additions & 10 deletions .nycrc.yml

This file was deleted.

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/specs/large-file-memory-leak/cloudflare-stringified.json
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
}
}
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

[![Online Demo](https://apitools.dev/swagger-parser/online/img/demo.svg)](https://apitools.dev/swagger-parser/online/)


## Features

- Parses Swagger specs in **JSON** or **YAML** format
- Validates against the [Swagger 2.0 schema](https://github.com/OAI/OpenAPI-Specification/blob/main/schemas/v2.0/schema.json), [OpenAPI 3.0 Schema](https://github.com/OAI/OpenAPI-Specification/blob/main/schemas/v3.0/schema.json), or [OpenAPI 3.1 Schema](https://github.com/OAI/OpenAPI-Specification/blob/main/schemas/v3.1/schema.json)
- [Resolves](https://apitools.dev/swagger-parser/docs/swagger-parser.html#resolveapi-options-callback) all `$ref` pointers, including external files and URLs
Expand All @@ -22,16 +22,14 @@
- Supports [circular references](https://apitools.dev/swagger-parser/docs/#circular-refs), nested references, back-references, and cross-references
- Maintains object reference equality — `$ref` pointers to the same value always resolve to the same object instance


## Example

```javascript
OpenAPIParser.validate(myAPI, (err, api) => {
if (err) {
console.error(err);
}
else {
console.log("API name: %s, Version: %s", api.info.title, api.info.version);
} else {
console.log('API name: %s, Version: %s', api.info.title, api.info.version);
}
});
```
Expand All @@ -41,39 +39,38 @@ Or use `async`/`await` or [Promise](http://javascriptplayground.com/blog/2015/02
```javascript
try {
let api = await OpenAPIParser.validate(myAPI);
console.log("API name: %s, Version: %s", api.info.title, api.info.version);
}
catch(err) {
console.log('API name: %s, Version: %s', api.info.title, api.info.version);
} catch (err) {
console.error(err);
}
```

For more detailed examples, please see the [API Documentation](https://apitools.dev/swagger-parser/docs/)


## Installation

Install using [npm](https://docs.npmjs.com/about-npm/):

```bash
npm install @readme/openapi-parser
```


## Usage

When using Swagger Parser in Node.js apps, you'll probably want to use **CommonJS** syntax:

```javascript
const OpenAPIParser = require("@readme/openapi-parser");
const OpenAPIParser = require('@readme/openapi-parser');
```

When using a transpiler such as [Babel](https://babeljs.io/) or [TypeScript](https://www.typescriptlang.org/), or a bundler such as [Webpack](https://webpack.js.org/) or [Rollup](https://rollupjs.org/), you can use **ECMAScript modules** syntax instead:

```javascript
import OpenAPIParser from "@readme/openapi-parser";
import OpenAPIParser from '@readme/openapi-parser';
```


## Differences from `@apidevtools/swagger-parser`

`@apidevtools/swagger-parser` returns schema validation errors as the raw error stack from Ajv. For example:

<img src="https://user-images.githubusercontent.com/33762/137796620-cd7de717-6492-4cff-b291-8629ed5dcd6e.png" width="600" />
Expand All @@ -84,12 +81,12 @@ To reduce the amount of potentially unnecessary noise that these JSON pointer er

Additionally with these error reporting differences, this library ships with a `validation.colorizeErrors` option that will disable colorization within these prettified errors.


## Browser support

Swagger Parser supports recent versions of every major web browser. Older browsers may require [Babel](https://babeljs.io/) and/or [polyfills](https://babeljs.io/docs/en/next/babel-polyfill).

To use Swagger Parser in a browser, you'll need to use a bundling tool such as [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/), [Parcel](https://parceljs.org/), or [Browserify](http://browserify.org/). Some bundlers may require a bit of configuration, such as setting `browser: true` in [rollup-plugin-resolve](https://github.com/rollup/rollup-plugin-node-resolve).


## API Documentation

Full API documentation is available [right here](https://apitools.dev/swagger-parser/docs/)
5 changes: 3 additions & 2 deletions bin/refresh-real-world-apis
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#! /usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-param-reassign */
/**
* This script regenerates the list of real world API definitions from APIs.guru that the test suite uses for parsing
* and validating integration tests.
*
* @link https://github.com/APIs-guru/openapi-directory
*/
/* eslint-disable no-param-reassign */
const fs = require('fs');

fetch('https://api.apis.guru/v2/list.json')
Expand Down Expand Up @@ -53,5 +54,5 @@ fetch('https://api.apis.guru/v2/list.json')
return apisMap;
})
.then(apis => {
fs.writeFileSync(`${__dirname}/../test/fixtures/real-world-apis.json`, JSON.stringify(apis, null, 2));
fs.writeFileSync(`${__dirname}/../test/fixtures/real-world-apis.json`, `${JSON.stringify(apis, null, 2)}\n`);
});
32 changes: 16 additions & 16 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# OpenAPI Parser API

## Things to Know

- [Class methods vs. Instance methods](#class-methods-vs-instance-methods)
- [Callbacks vs. Promises](#callbacks-vs-promises)
- [Circular references](#circular-refs)


## Classes & Methods

#### [The `OpenAPIParser` class](openapi-parser.md)

- [`api` property](openapi-parser.md#api)
- [`$refs` property](openapi-parser.md#refs)
- [`validate()` method](openapi-parser.md#validateapi-options-callback)
Expand All @@ -18,6 +19,7 @@
- [`resolve()` method](openapi-parser.md#resolveapi-options-callback)

#### [The `$Refs` class](refs.md)

- [`circular` property](refs.md#circular)
- [`paths()` method](refs.md#pathstypes)
- [`values()` method](refs.md#valuestypes)
Expand All @@ -27,28 +29,28 @@

#### [The `Options` object](options.md)


### Class methods vs. Instance methods
All of Swagger Parser's methods are available as static (class) methods, and as instance methods. The static methods simply create a new [`OpenAPIParser`](openapi-parser.md) instance and then call the corresponding instance method. Thus, the following line...

All of Swagger Parser's methods are available as static (class) methods, and as instance methods. The static methods simply create a new [`OpenAPIParser`](openapi-parser.md) instance and then call the corresponding instance method. Thus, the following line...

```javascript
OpenAPIParser.validate("my-api.yaml");
OpenAPIParser.validate('my-api.yaml');
```

... is the same as this:

```javascript
let parser = new OpenAPIParser();
parser.validate("my-api.yaml");
parser.validate('my-api.yaml');
```

The difference is that in the second example you now have a reference to `parser`, which means you can access the results ([`parser.api`](openapi-parser.md#api-object) and [`parser.$refs`](openapi-parser.md#refs)) anytime you want, rather than just in the callback function.


### Callbacks vs. Promises
Many people prefer `async`/`await` or [Promise](http://javascriptplayground.com/blog/2015/02/promises/) syntax instead of callbacks. Swagger Parser allows you to use whichever one you prefer.

If you pass a callback function to any method, then the method will call the callback using the Node.js error-first convention. If you do _not_ pass a callback function, then the method will return a Promise.
Many people prefer `async`/`await` or [Promise](http://javascriptplayground.com/blog/2015/02/promises/) syntax instead of callbacks. Swagger Parser allows you to use whichever one you prefer.

If you pass a callback function to any method, then the method will call the callback using the Node.js error-first convention. If you do _not_ pass a callback function, then the method will return a Promise.

The following two examples are equivalent:

Expand All @@ -57,8 +59,7 @@ The following two examples are equivalent:
OpenAPIParser.validate(mySchema, (err, api) => {
if (err) {
// Error
}
else {
} else {
// Success
}
});
Expand All @@ -70,21 +71,20 @@ try {
let api = await OpenAPIParser.validate(mySchema);

// Success
}
catch(err) {
} catch (err) {
// Error
}
```


### Circular $Refs
Swagger APIs can contain [circular $ref pointers](https://gist.github.com/JamesMessinger/d18278935fc73e3a0ee1), and Swagger Parser fully supports them. Circular references can be resolved and dereferenced just like any other reference. However, if you intend to serialize the dereferenced api as JSON, then you should be aware that [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) does not support circular references by default, so you will need to [use a custom replacer function](https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json).

Swagger APIs can contain [circular $ref pointers](https://gist.github.com/JamesMessinger/d18278935fc73e3a0ee1), and Swagger Parser fully supports them. Circular references can be resolved and dereferenced just like any other reference. However, if you intend to serialize the dereferenced api as JSON, then you should be aware that [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) does not support circular references by default, so you will need to [use a custom replacer function](https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json).

You can disable circular references by setting the [`dereference.circular`](options.md) option to `false`. Then, if a circular reference is found, a `ReferenceError` will be thrown.

Or you can choose to just ignore circular references altogether by setting the [`dereference.circular`](options.md) option to `"ignore"`. In this case, all non-circular references will still be dereferenced as normal, but any circular references will remain in the schema.
Or you can choose to just ignore circular references altogether by setting the [`dereference.circular`](options.md) option to `"ignore"`. In this case, all non-circular references will still be dereferenced as normal, but any circular references will remain in the schema.

Another option is to use the [`bundle`](openapi-parser.md#bundleapi-options-callback) method rather than the [`dereference`](openapi-parser.md#dereferenceapi-options-callback) method. Bundling does _not_ result in circular references, because it simply converts _external_ `$ref` pointers to _internal_ ones.
Another option is to use the [`bundle`](openapi-parser.md#bundleapi-options-callback) method rather than the [`dereference`](openapi-parser.md#dereferenceapi-options-callback) method. Bundling does _not_ result in circular references, because it simply converts _external_ `$ref` pointers to _internal_ ones.

```javascript
"person": {
Expand Down
Loading

0 comments on commit d43f6e5

Please sign in to comment.