-
Notifications
You must be signed in to change notification settings - Fork 185
6. How to Test Your Code
In this document, you will learn how to test if your new command is registered successfully and returns an expected result.
To test a command-line test string
that returns a string response 'hello world'
, you can use the TestStringResponse
api (source). Take the following code as an example, the TestStringResponse
api will use spectron
and Mocha
test suite to launch Kui, then paste the command-line test string
to repl input, and check if the repl result is 'hello wolrd'
.
import { TestStringResponse } from '@kui-shell/test'
new TestStringResponse({
command: 'test string',
expect: 'hello world',
exact: true // set to false if 'hello world' should be a substring of the response
}).string()
To test a command-line test mmr name
that returns a MultiModalResponse
with name this is the name part
, you can use the TestMMR
api (source).Take the following code as an example, the TestMMR
api will use spectron
and Mocha
to launch Kui, then paste the command-line 'test mmr name'
to repl input, and check if TopNavSidecar
is open and the name
part of sidecar header is 'this is the name part'
.
const testMetadataName = new TestMMR({
command: 'test mmr name',
metadata: {
name: 'this is the name part'
}
})
You may also find the following test examples useful for testing other parts of a MultiModalResponse
:
- Test TopNavSidecar Namespace
- Test TopNavSidecar Kind
- Test TopNavSidecar Tabs
- Test TopNavSidecar Tab Registration
To test a command test nav
that returns a NavResponse with menu Test Nav
and menu item with mode table
, a Home Page
href link and a switch
command link, you can use TestNavResponse
api (source). Take the following code as an example, the TestNavResponse
api will use spectron
and Mocha
to launch Kui, then paste the command-line 'test nav'
, and check if the leftnav sidecar is open with 'Test Nav'
menu expanded, has a 'table' menu item, and the Home Page
link and switch
command link. It will also clck the switch
link, and check a new LeftNavSidecar
is open with menu Test Nav 2
.
import { TestNavResponse } from '@kui-shell/test'
const testFullNavResponse = new TestNavResponse({
command: 'test nav',
showing: 'Test Nav',
modes: ['table'],
commandLinks: [{ label: 'switch', expect: { type: 'NavResponse', showing: 'Test Nav 2' } }],
hrefLinks: [{ label: 'Home Page', href: 'http://kui.tools' }]
})
testFullNavResponse.run()
You may find more test examples of NavResponse
helpful here.
To run tests for you plugin, place the code into [your-plugin-directory]/src/test/[layer]
, and run the following command in the top-level of your Kui project. Then, you should see all the tests under the layer directory run sequentially.
npm run testv2 [layer]
For example, you can run all the tests under plugin-k8s/src/test/k8s1
by npm run testv2 k8s1
.
If your test has a distinguishable mocha test title, you can use TEST_FILTER
environment variable to run a specific test suite:
For example:
TEST_FILTER="string response for command=test string" npm run testv2