Skip to content

Commit

Permalink
add info about Jest in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Oct 7, 2022
1 parent d19ced8 commit c2195f2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ Use this package to validate and parse AsyncAPI documents —either YAML or JSON
- [Circular references](#circular-references)
- [Stringify](#stringify)
- [Convert to the old API](#convert-to-the-old-api)
- [Bundler configuration](#bundler-configuration)
* [Webpack](#webpack)
- [Notes](#notes)
* [Using with Webpack](#using-with-webpack)
* [Testing with [Jest](https://jestjs.io/)](#testing-with-jesthttpsjestjsio)
- [Develop](#develop)
- [Contributing](#contributing)
- [Contributors](#contributors)
Expand Down Expand Up @@ -385,9 +386,9 @@ const oldAsyncAPIDocument = convertToOldAPI(document);
> **Warning**
> The old api will be supported only for a certain period of time. The target date for turning off support of the old API is around the end of January 2023.

## Bundler configuration
## Notes

### Webpack
### Using with Webpack

Versions `<5` of Webpack should handle bundling without problems. Due to the fact that Webpack 5 no longer does fallbacks to native NodeJS modules by default we need to install `buffer` package and add fallbacks:

Expand All @@ -404,6 +405,23 @@ Versions `<5` of Webpack should handle bundling without problems. Due to the fac
}
```

### Testing with [Jest](https://jestjs.io/)

Using a Parser in an application that is tested using [Jest](https://jestjs.io/), there will probably an error like:

```bash
Cannot find module 'nimma/legacy' from 'node_modules/@stoplight/spectral-core/dist/runner/runner.js
```

It's a problem with Jest, which cannot understand [NodeJS's package exports](https://nodejs.org/api/packages.html). To fix that, should be [enabled ESM support in Jest](https://jestjs.io/docs/ecmascript-modules) or set an appropriate Jest's `moduleNameMapper` config:

```js
moduleNameMapper: {
'^nimma/legacy$': '<rootDir>/node_modules/nimma/dist/legacy/cjs/index.js',
'^nimma/(.*)': '<rootDir>/node_modules/nimma/dist/cjs/$1',
},
```

## Develop

1. Write code and tests in the `test` folder.
Expand Down
33 changes: 26 additions & 7 deletions test/models/v2/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,28 @@ describe('Channel model', function() {
const d = new Schema(doc);
expect(d.additionalItems()).toEqual(true);
});

it('should return true when not defined', function() {
const doc: any = {};
const d = new Schema(doc);
expect(d.additionalItems()).toEqual(true);
});

it('should return false when null', function() {
const doc: any = { additionalItems: null };
const d = new Schema(doc);
expect(d.additionalItems()).toEqual(false);
});
});

describe('.additionalProperties()', function() {
it('should return the value schema object', function() {
const doc = { additionalProperties: {} };
describe('additionalProperties()', function() {
it('should return a Schema object', function() {
const doc: any = { additionalProperties: { type: 'string' } };
const d = new Schema(doc);
expect(d.additionalProperties()).toBeInstanceOf(Schema);
expect((d.additionalProperties() as Schema).json()).toEqual(doc.additionalProperties);
});

it('should return the true when there is no value', function() {
const doc = {};
const d = new Schema(doc);
Expand All @@ -112,12 +125,18 @@ describe('Channel model', function() {
const d = new Schema(doc);
expect(d.additionalProperties()).toEqual(false);
});

it('should return the false where there is true value', function() {
const doc = { additionalProperties: true };
it('should return true when not defined', function() {
const doc: any = {};
const d = new Schema(doc);
expect(d.additionalProperties()).toEqual(true);
});

it('should return false when null', function() {
const doc: any = { additionalProperties: null };
const d = new Schema(doc);
expect(d.additionalProperties()).toEqual(false);
});
});

describe('.allOf()', function() {
Expand Down

0 comments on commit c2195f2

Please sign in to comment.