-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for CLI commands (#799)
* refactor: disable ora in tests * refactor: bubble errors up to bin script this will make it easier for us to test! * refactor: also don't process.exit in uninstall commands * test: first pass at install tests * test: first pass at list tests * refactor: use enum * test: add another list command test we'll see if this snapshot passes in GHA 🤞🏽 * chore: another test TODO * chore: lint * test: first pass at uninstall tests * test: add snapshot tests for help command * refactor: use ora opts in uninstall command * test(list): make sure fetchmock is restored * test: more uninstall command tests * chore: consistent whitespace * fix: lint
- Loading branch information
1 parent
f06ad7f
commit e456d46
Showing
11 changed files
with
372 additions
and
33 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
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 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
24 changes: 24 additions & 0 deletions
24
packages/api/test/commands/__snapshots__/install.test.ts.snap
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,24 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`install command > should print help screen 1`] = ` | ||
"Usage: install [options] <uri> | ||
install an API SDK into your codebase | ||
Arguments: | ||
uri an API to install | ||
Options: | ||
-i, --identifier <identifier> API identifier (eg. \`@api/petstore\`) | ||
-l, --lang <language> SDK language (choices: \\"js\\", default: \\"js\\") | ||
-y, --yes Automatically answer \\"yes\\" to any prompts | ||
printed | ||
-h, --help display help for command | ||
Examples: | ||
$ npx api install @developers/v2.0#nysezql0wwo236 | ||
$ npx api install https://raw.githubusercontent.com/readmeio/oas-examples/main/3.0/json/petstore-simple.json | ||
$ npx api install ./petstore.json | ||
" | ||
`; |
25 changes: 25 additions & 0 deletions
25
packages/api/test/commands/__snapshots__/list.test.ts.snap
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,25 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`install command > should list installed SDKs 1`] = ` | ||
"[33m[4mpetstore[24m[39m | ||
package name ([31mprivate[39m): [90m@api/petstore[39m | ||
language: [90mjs[39m | ||
source: [90m@petstore/v1.0#n6kvf10vakpemvplx[39m | ||
installer version: [90m7.0.0-beta.3[39m | ||
created at: [90m2023-10-25T00:00:00.000Z[39m" | ||
`; | ||
|
||
exports[`install command > should print help screen 1`] = ` | ||
"Usage: list|ls [options] | ||
list any installed API SDKs | ||
Options: | ||
-h, --help display help for command | ||
" | ||
`; |
19 changes: 19 additions & 0 deletions
19
packages/api/test/commands/__snapshots__/uninstall.test.ts.snap
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,19 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`install command > should print help screen 1`] = ` | ||
"Usage: uninstall [options] <identifier> | ||
uninstall an SDK from your codebase | ||
Arguments: | ||
identifier the SDK to uninstall | ||
Options: | ||
-y, --yes Automatically answer \\"yes\\" to any prompts printed | ||
-h, --help display help for command | ||
Examples: | ||
$ npx api uninstall petstore | ||
" | ||
`; |
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,85 @@ | ||
import type { SpyInstance } from 'vitest'; | ||
|
||
import { CommanderError } from 'commander'; | ||
import prompts from 'prompts'; | ||
import uniqueTempDir from 'unique-temp-dir'; | ||
import { describe, beforeEach, it, expect, vi, afterEach } from 'vitest'; | ||
|
||
import { SupportedLanguages } from '../../src/codegen/factory.js'; | ||
import installCmd from '../../src/commands/install.js'; | ||
import Storage from '../../src/storage.js'; | ||
|
||
const baseCommand = ['api', 'install']; | ||
|
||
const cmdError = (msg: string) => new CommanderError(0, '', msg); | ||
|
||
describe('install command', () => { | ||
let stdout: string[]; | ||
let stderr: string[]; | ||
let consoleLogSpy: SpyInstance; | ||
|
||
beforeEach(() => { | ||
stdout = []; | ||
stderr = []; | ||
|
||
installCmd.exitOverride(); | ||
installCmd.configureOutput({ | ||
writeOut: str => stdout.push(str), | ||
writeErr: str => stderr.push(str), | ||
}); | ||
|
||
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); | ||
Storage.setStorageDir(uniqueTempDir()); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleLogSpy.mockRestore(); | ||
Storage.reset(); | ||
}); | ||
|
||
it('should error out if uri is not passed', () => { | ||
return expect(installCmd.parseAsync([...baseCommand])).rejects.toStrictEqual( | ||
cmdError("error: missing required argument 'uri'"), | ||
); | ||
}); | ||
|
||
it('should error out if invalid uri is passed', () => { | ||
return expect(installCmd.parseAsync([...baseCommand, 'petstore.json'])).rejects.toThrow( | ||
/Sorry, we were unable to load an API definition from .*petstore.json. Please either supply a URL or a path on your filesystem./, | ||
); | ||
}); | ||
|
||
it('should accept valid lang parameter but error out if invalid uri is passed', () => { | ||
return expect( | ||
installCmd.parseAsync([...baseCommand, 'petstore.json', '--lang', SupportedLanguages.JS]), | ||
).rejects.toThrow( | ||
/Sorry, we were unable to load an API definition from .*petstore.json. Please either supply a URL or a path on your filesystem./, | ||
); | ||
}); | ||
|
||
it('should error out if invalid lang is passed', () => { | ||
return expect(installCmd.parseAsync([...baseCommand, '--lang', 'javascript'])).rejects.toStrictEqual( | ||
cmdError("error: option '-l, --lang <language>' argument 'javascript' is invalid. Allowed choices are js."), | ||
); | ||
}); | ||
|
||
it('should handle user answering no to package installation confirmation prompt', () => { | ||
prompts.inject(['petstore', false]); | ||
return expect( | ||
installCmd.parseAsync([...baseCommand, '../test-utils/definitions/simple.json']), | ||
).rejects.toStrictEqual(new Error('Installation cancelled.')); | ||
}); | ||
|
||
it('should print help screen', async () => { | ||
await expect(installCmd.parseAsync([...baseCommand, '--help'])).rejects.toStrictEqual(cmdError('(outputHelp)')); | ||
|
||
expect(stdout.join('\n')).toMatchSnapshot(); | ||
}); | ||
|
||
it.todo('should surface generation errors'); | ||
it.todo('should surface file save errors'); | ||
it.todo('should surface package installation errors'); | ||
it.todo('should surface compilation errors'); | ||
it.todo('should successfully generate SDK'); | ||
it.todo('should successfully bypass all prompts with --yes option'); | ||
}); |
Oops, something went wrong.