> For the complete documentation index, see [llms.txt](https://docs.newbot.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.newbot.io/unit-tests/test-a-scenario.md).

# Test a scenario

Type `npm test` in the terminal to run the unit tests

### Perform a scenario test

Let's initialize the scenario

```javascript
import { ConverseTesting } from 'newbot/testing'
import mainSkill from './main'

describe('My own test', () => {
    let converse, userConverse

    beforeEach(() => {
        converse = new ConverseTesting(mainSkill)
        userConverse = converse.createUser()
    })
})
```

The content of `main.converse` is as follows:

```typescript
@Event('start') 
start() {
    > Hello
}
```

The goal is to test if the message sent is `Hello` when the user interacts for the first time.

So let's add a test:

```javascript
import assert from 'assert'
import { ConverseTesting } from 'newbot/testing'
import mainSkill from './main'

describe('My own test', () => {
    let converse, userConverse

    beforeEach(() => {
        converse = new ConverseTesting(mainSkill)
        userConverse = converse.createUser()
    })

    test('Chatbot says "Hello"', () => {
        return userConverse
            .start(testing => {
                assert.equal(testing.output(0), 'Hello')
            })
            .end()
    })
})
```

1. Let's use the `start()` method to start the scenario. The function in parameter contains the argument `testing`. It will be used to perform several tests.
2. Here, we use the `output()` function to retrieve the first message sent by the script

> Note that the `end()` method returns a promise. With `mocha`, we can return the promise to `it()` function
>
> It is possible to recover all messages sent by the script. For that, do not put parameters to the method.

```javascript
const array = testing.output() // Tableau de messages
```

### Prompt test

The content of `main.converse` is now the following:

```typescript
@Event('start') 
start() {
    > Hello
    Prompt()
    > Welcome { :text }
}
```

The test :

```javascript
import assert from 'assert'
import { ConverseTesting } from 'newbot/testing'
import mainSkill from './main'

describe('My own test', () => {
    let converse, userConverse

    beforeEach(() => {
        converse = new ConverseTesting(mainSkill)
        userConverse = converse.createUser()
    })

    test('Test Prompt', () => {
        return userConverse
            .start(testing => {
                assert.equal(testing.output(0), 'Hello')
            })
            .prompt('Sam', testing => {
                assert.equal(testing.output(0), 'Welcome Sam')
            })
            .end()
    })
})
```

Note that the `start()` method performs all tests up to the input request. Then we use `prompt()` to enter a text (first parameter) and the scenario continues.

### Perform a quick test of the conversation

It is possible to write a unit test more quickly if you want to test a simple conversation:

The scenario is always the following:

```typescript
@Event('start') 
start() {
    > Hello
    Prompt()
    > Welcome { :text }
}
```

```javascript
import { ConverseTesting, user, bot } from 'newbot/testing'
import mainSkill from './main'

describe('My own test', () => {
    let converse, userConverse

    beforeEach(() => {
        converse = new ConverseTesting(mainSkill)
        userConverse = converse.createUser()
    })

    test('Test conversation', () => {
        return userConverse
            .conversation(
                bot `Hello`,
                user `Sam`,
                bot `Welcome Sam`
            )
    })
})
```
