-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add jest * refactor tests to match linting suggestions * refactor cli test * migrate test for openapi compatibility * migrate test for openapi compatibility * refactor test for example app v2 * migrate helpers * reorganize addDataToSwaggerObject * move lib-specific test to the right place * rename * rename fixtures to files * migrate open api spec * remove unnecessary line * move v2 example files * update .editorconfig file * remove mocha * attempt to respect nvmrc * attempt to respect nvmrc * attempt to respect nvmrc * set node version * set 10 * try a matrix instead * try snapshot update * update tests * update github workflow
- Loading branch information
1 parent
a0ad333
commit 0bfe3dc
Showing
48 changed files
with
3,382 additions
and
1,567 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,10 @@ | ||
# EditorConfig helps developers define and maintain consistent | ||
# coding styles between different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
|
||
[*] | ||
|
||
# Change these settings to your own preference | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
# We recommend you to keep these unchanged | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
end_of_line = lf | ||
indent_size = 2 | ||
indent_style = space | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
max_line_length = 80 | ||
trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
test/fixtures/wrong_syntax.json | ||
test/files/v2/wrong_syntax.json | ||
test/files/v2/wrong_syntax.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const request = require('supertest'); | ||
const { app, server } = require('./app'); | ||
const swaggerSpec = require('./swagger-spec.json'); | ||
|
||
describe('Example application written in swagger specification (v2)', () => { | ||
it('should be healthy', async () => { | ||
const response = await request(app).get('/'); | ||
expect(response.status).toBe(200); | ||
}); | ||
|
||
it('should return the expected specification', async () => { | ||
const response = await request(app).get('/api-docs.json'); | ||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual(swaggerSpec); | ||
}); | ||
|
||
afterAll(() => { | ||
server.close(); | ||
}); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`command line interface help menu is default fallback when no arguments 1`] = ` | ||
"Usage: swagger-jsdoc [options] <path ...> | ||
Options: | ||
-V, --version output the version number | ||
-d, --definition <swaggerDef.js> Input swagger definition. | ||
-o, --output [swaggerSpec.json] Output swagger specification. | ||
-h, --help display help for command | ||
" | ||
`; | ||
exports[`command line interface help menu works 1`] = ` | ||
"Usage: swagger-jsdoc [options] <path ...> | ||
Options: | ||
-V, --version output the version number | ||
-d, --definition <swaggerDef.js> Input swagger definition. | ||
-o, --output [swaggerSpec.json] Output swagger specification. | ||
-h, --help display help for command | ||
" | ||
`; | ||
exports[`command line interface should reject definition file with invalid JSON syntax 1`] = ` | ||
"Error while loading definition file 'test/files/v2/wrong_syntax.json': | ||
Unexpected token t in JSON at position 18 | ||
" | ||
`; | ||
exports[`command line interface should reject definition file with invalid YAML syntax 1`] = ` | ||
"Error while loading definition file 'test/files/v2/wrong_syntax.yaml': | ||
tag suffix cannot contain exclamation marks at line 2, column 5: | ||
!!!title: Hello World | ||
^ | ||
" | ||
`; | ||
exports[`command line interface should reject definition file with non-JSON compatible YAML syntax 1`] = ` | ||
"Error while loading definition file 'test/files/v2/non_json_compatible.yaml': | ||
unknown tag !<tag:yaml.org,2002:js/undefined> at line 3, column 3: | ||
version: 1.0.0 | ||
^ | ||
" | ||
`; | ||
exports[`command line interface should require a definition file 1`] = ` | ||
"Definition file is required. | ||
You can do that, for example: | ||
$ swag-jsdoc -d swaggerDef.js wrongDefinition | ||
Usage: swagger-jsdoc [options] <path ...> | ||
Options: | ||
-V, --version output the version number | ||
-d, --definition <swaggerDef.js> Input swagger definition. | ||
-o, --output [swaggerSpec.json] Output swagger specification. | ||
-h, --help display help for command | ||
" | ||
`; | ||
exports[`command line interface should require an info object in the definition 1`] = ` | ||
"Definition file should contain an info object! | ||
More at http://swagger.io/specification/#infoObject | ||
" | ||
`; | ||
exports[`command line interface should require arguments with jsDoc data about an API 1`] = ` | ||
"You must provide sources for reading API files. | ||
Either add filenames as arguments, or add an \\"apis\\" key in your definitions file. | ||
" | ||
`; | ||
exports[`command line interface should require title and version in the info object 1`] = ` | ||
"The title and version properties are required! | ||
More at http://swagger.io/specification/#infoObject | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
const fs = require('fs'); | ||
const { promisify } = require('util'); | ||
const { exec } = require('child_process'); | ||
|
||
const sh = promisify(exec); | ||
const dir = process.env.PWD; | ||
const bin = `${dir}/bin/swagger-jsdoc.js`; | ||
|
||
describe('command line interface', () => { | ||
it('help menu is default fallback when no arguments', async () => { | ||
const result = await sh(`${bin}`); | ||
expect(result.stdout).toMatchSnapshot(); | ||
}); | ||
|
||
it('help menu works', async () => { | ||
const result = await sh(`${bin} -h`); | ||
expect(result.stdout).toMatchSnapshot(); | ||
}); | ||
|
||
it('should require a definition file', async () => { | ||
const result = await sh(`${bin} wrongDefinition`); | ||
expect(result.stdout).toMatchSnapshot(); | ||
}); | ||
|
||
it('should require an info object in the definition', async () => { | ||
const result = await sh(`${bin} -d test/files/v2/empty_definition.js`); | ||
expect(result.stdout).toMatchSnapshot(); | ||
}); | ||
|
||
it('should require title and version in the info object', async () => { | ||
const result = await sh(`${bin} -d test/files/v2/wrong_definition.js`); | ||
expect(result.stdout).toMatchSnapshot(); | ||
}); | ||
|
||
it('should require arguments with jsDoc data about an API', async () => { | ||
const result = await sh(`${bin} -d example/v2/swaggerDef.js`); | ||
expect(result.stdout).toMatchSnapshot(); | ||
}); | ||
|
||
it('should create swagger.json by default when the API input is good', async () => { | ||
const result = await sh( | ||
`${bin} -d example/v2/swaggerDef.js example/v2/routes.js` | ||
); | ||
expect(result.stdout).toBe('Swagger specification is ready.\n'); | ||
const specification = fs.statSync('swagger.json'); | ||
expect(specification.nlink).not.toBe(0); | ||
}); | ||
|
||
it('should create swagger.json by default when the API input is from definition file', async () => { | ||
const result = await sh(`${bin} -d test/files/v2/api_definition.js`); | ||
expect(result.stdout).toBe('Swagger specification is ready.\n'); | ||
const specification = fs.statSync('swagger.json'); | ||
expect(specification.nlink).not.toBe(0); | ||
}); | ||
|
||
it('should accept custom configuration for output specification', async () => { | ||
const result = await sh( | ||
`${bin} -d example/v2/swaggerDef.js -o customSpec.json example/v2/routes.js` | ||
); | ||
expect(result.stdout).toBe('Swagger specification is ready.\n'); | ||
const specification = fs.statSync('customSpec.json'); | ||
expect(specification.nlink).not.toBe(0); | ||
}); | ||
|
||
it('should create a YAML swagger spec when a custom output configuration with a .yaml extension is used', async () => { | ||
const result = await sh( | ||
`${bin} -d example/v2/swaggerDef.js -o customSpec.yaml example/v2/routes.js` | ||
); | ||
expect(result.stdout).toBe('Swagger specification is ready.\n'); | ||
const specification = fs.statSync('customSpec.yaml'); | ||
expect(specification.nlink).not.toBe(0); | ||
}); | ||
|
||
it('should allow a JavaScript definition file', async () => { | ||
const result = await sh(`${bin} -d test/files/v2/api_definition.js`); | ||
expect(result.stdout).toBe('Swagger specification is ready.\n'); | ||
const specification = fs.statSync('swagger.json'); | ||
expect(specification.nlink).not.toBe(0); | ||
}); | ||
|
||
it('should allow a JSON definition file', async () => { | ||
const result = await sh(`${bin} -d test/files/v2/api_definition.json`); | ||
expect(result.stdout).toBe('Swagger specification is ready.\n'); | ||
const specification = fs.statSync('swagger.json'); | ||
expect(specification.nlink).not.toBe(0); | ||
}); | ||
|
||
it('should allow a YAML definition file', async () => { | ||
const result = await sh(`${bin} -d test/files/v2/api_definition.yaml`); | ||
expect(result.stdout).toBe('Swagger specification is ready.\n'); | ||
const specification = fs.statSync('swagger.json'); | ||
expect(specification.nlink).not.toBe(0); | ||
}); | ||
|
||
it('should reject definition file with invalid YAML syntax', async () => { | ||
const result = await sh(`${bin} -d test/files/v2/wrong_syntax.yaml`); | ||
expect(result.stdout).toMatchSnapshot(); | ||
}); | ||
|
||
it('should reject definition file with non-JSON compatible YAML syntax', async () => { | ||
const result = await sh(`${bin} -d test/files/v2/non_json_compatible.yaml`); | ||
expect(result.stdout).toMatchSnapshot(); | ||
}); | ||
|
||
it('should reject definition file with invalid JSON syntax', async () => { | ||
const result = await sh(`${bin} -d test/files/v2/wrong_syntax.json`); | ||
expect(result.stdout).toMatchSnapshot(); | ||
}); | ||
|
||
it('should reject bad YAML identation with feedback: upper line', async () => { | ||
await expect( | ||
sh( | ||
`${bin} -d example/v2/swaggerDef.js test/files/v2/wrong-yaml-identation1.js` | ||
) | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it('should reject bad YAML identation with feedback: same line', async () => { | ||
await expect( | ||
sh( | ||
`${bin} -d example/v2/swaggerDef.js test/files/v2/wrong-yaml-identation2.js` | ||
) | ||
).rejects.toThrow(); | ||
}); | ||
|
||
afterAll(() => { | ||
fs.unlinkSync(`${dir}/swagger.json`); | ||
fs.unlinkSync(`${dir}/customSpec.json`); | ||
fs.unlinkSync(`${dir}/customSpec.yaml`); | ||
fs.unlinkSync(`${dir}/customSpec.yml`); | ||
}); | ||
}); |
Oops, something went wrong.