diff --git a/README.md b/README.md index 82ea56edb..c9abb01da 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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: @@ -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$': '/node_modules/nimma/dist/legacy/cjs/index.js', + '^nimma/(.*)': '/node_modules/nimma/dist/cjs/$1', +}, +``` + ## Develop 1. Write code and tests in the `test` folder. diff --git a/test/models/v2/schema.spec.ts b/test/models/v2/schema.spec.ts index 7a511f0bc..4e48b3366 100644 --- a/test/models/v2/schema.spec.ts +++ b/test/models/v2/schema.spec.ts @@ -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); @@ -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() {