# Jest server API testing

**URL:** <https://discuss.elastic.co/t/jest-server-api-testing/275943>\
**Category:** Kibana\
**Created:** [June 15, 2021, 6:25am UTC](https://discuss.elastic.co/t/jest-server-api-testing/275943 "2021-06-15T06:25:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)\
**Post date:** [June 15, 2021, 6:25am UTC](https://discuss.elastic.co/t/jest-server-api-testing/275943/1 "2021-06-15T06:25:09Z")

</div>

Hello Team,

I am new to the Javascript and Kibana.  
Based on the discussion on [Sonarqube](https://discuss.elastic.co/t/sonarqube-code-coverage-for-custom-plugins/272624/4) topic, i am trying to write a test case for basic hello-world plugin (generated by plugin generator)  
I want to achieve something like call the API and test the resposne code or response

created index.js in `hello_world/server/ __test__ ` folder

```auto
describe('Test API',()=>{
   it('calling API',async()=>{
      await supertest
      .post('/api/hello_world/example')
     .send()
     .expect(200);

   });
});

```

here is the example.js generated by plugin

```auto
export default function (server) {

serevr.route({
    path: 'api/hello_world/example',
    method: 'GET',
    handler () {
        return { time : (new Date()).toISOString() };
    }

});

}

```

Few queries here,

1. how to get supertest in this file.
2. is Kibana and elasticsearch expected to run in background
3. How can i execute this single test.

Could you please help me with the pointers to achieve this API testing.  
Thank you for your help and support!

---

<div class="post-metadata">

**Author:** ![lukeelmers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukeelmers/32/35230_2.png) [@lukeelmers](https://discuss.elastic.co/u/lukeelmers)\
**Post date:** [June 16, 2021, 5:56pm UTC](https://discuss.elastic.co/t/jest-server-api-testing/275943/2 "2021-06-16T17:56:47Z")

</div>

> [@adityaPsl](#):
>
> how to get supertest in this file

You should be able to import it directly as it's already a dependency of Kibana.

> [@adityaPsl](#):
>
> is Kibana and elasticsearch expected to run in background

Yes, but we have some utilities to help with this using our integration test framework, which is [mentioned in our docs here](https://www.elastic.co/guide/en/kibana/master/development-tests.html).

Here's an example jest integration test which might be a helpful reference: [https://github.com/elastic/kibana/blob/master/src/plugins/security\_oss/server/routes/integration\_tests/anonymous\_access\_capabilities.test.ts](https://github.com/elastic/kibana/blob/master/src/plugins/security_oss/server/routes/integration_tests/anonymous_access_capabilities.test.ts)

Another option is writing your own functional test (not in jest), which is [explained here](https://www.elastic.co/guide/en/kibana/master/external-plugin-functional-tests.html).

Functional test example: [https://github.com/elastic/kibana/blob/master/test/api\_integration/apis/status/status.js](https://github.com/elastic/kibana/blob/master/test/api_integration/apis/status/status.js)

Overall the integration tests are more lightweight, so if you just want to test your API and don't have extensive needs for a bunch of sample test data in Elasticsearch, I'd go with that option. Only use functional tests if you know you need them.

> [@adityaPsl](#):
>
> How can i execute this single test

To run a jest integration test: `yarn test:jest_integration plugins/my-plugin/server/integration_tests/my_test_file.ts`

To run an API functional test: `node scripts/functional_tests_server --config [directory]/config.js node`, and then once the server fires up: `scripts/functional_test_runner --config [directory]/config.js`

More on that [in our docs](https://www.elastic.co/guide/en/kibana/master/development-tests.html) as well.

Hope this helps a bit! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 14, 2021, 5:57pm UTC](https://discuss.elastic.co/t/jest-server-api-testing/275943/3 "2021-07-14T17:57:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
