> For the complete documentation index, see [llms.txt](https://www.serverless-cqrs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.serverless-cqrs.com/testing.md).

# 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>" %}
