# Testing

Because your domain is described a series of pure functions, testing becomes ultra-simple: given some input, expect a certain output. No mocking calls to external services or a database.&#x20;

### Actions

Let's look at the basic example from the Domain page:

{% code title="actions.js" %}

```javascript
const actions = {
  addTodo: (state, payload) => {
    if (!payload.title) throw new Error('titleMissing')
    
    return [{
      type: 'TodoAdded',
      title: payload.title,
      at: Date.now(),
    }]
  }
}

module.exports = actions
```

{% endcode %}

Now let's write a test for this.

{% code title="**test**/actions.js" %}

```javascript
// stub Date.now
Date.now = () => 123

const test = require('tape')
const actions = require('../actions')

test('addTodo', assert => {
  assert.deepEquals(
    actions.addTodo({}, { title: 'foobar' }),
    [{
      type: 'TodoAdded',
      title: 'foobar',
      at: 123
    }],
    'generates a TodoAdded event'
  )
  
  assert.throws(
    () => actions.addTodo({}, { foo: 'bar' }),
    /titleMissing/,
    'throws if title is missing'
  )
})
```

{% endcode %}

And we can even execute it:

{% embed url="<https://repl.it/@yonahforst/serverless-cqrs-testing-actions>" %}

###

### Reducer

{% code title="reducer.js" %}

```javascript
const initialState = {
  todos: []
}

const reducer = (state, event) => {
  switch (event.type) {
    case 'TodoAdded':
      return {
        todos: [
          ...state.todos,
          { title: event.title },
        ]
      }
      
    default:
      return state
  }
}

module.exports = (events, state=initialState) => events.reduce(reducer, state)
```

{% endcode %}

{% code title="**test**/reducer.js" %}

```javascript
const test = require('tape')
const reducer = require('../reducer')

test('todoReducer', assert => {
  const events = [
    { type: 'TodoAdded', title: 'get milk' },
    { type: 'TodoAdded', title: 'borrow sugar' },
    { type: 'Invalid', title: 'ignore this' },
  ]
  
  const expected = {
    todos: [
      { title: 'get milk' },
      { title: 'borrow sugar' },
    ]
  }    
    
  const result = reducer(events)
  
  assert.deepEquals(result, expected, 'reduces events')
  assert.end()
})
```

{% endcode %}

{% embed url="<https://repl.it/@yonahforst/serverless-cqrs-testing-reducer>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.serverless-cqrs.com/testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
