Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing endless loops #862

Closed
codepunkt opened this issue Mar 13, 2017 · 6 comments

Comments

@codepunkt
Copy link

commented Mar 13, 2017

I've been doing a lot of work with both redux-observable and redux-saga and so far i like the testability of sagas a lot!

What i don't get is the testing of endless loops/watchers. e.g.

import { call, take } from 'redux-saga/effects'

import logout from './logout'
import LOGOUT from '../constants'

export function* logoutWatcher () {
  while (true) {
    yield take(LOGOUT)
    yield call(logout)
  }
}

so far i'm testing this (with jest) using something along the lines of

import { call, take } from 'redux-saga/effects'

import logout from '../logout'
import LOGOUT from '../../constants'
import logoutWatcher from '../logoutWatcher'

describe('logoutWatcher', () => {
  const generator = logoutWatcher()

  test('takes a LOGOUT action', () => {
    expect(generator.next().value).toEqual(take(LOGOUT))
  })

  test('takes a LOGOUT action', () => {
    expect(generator.next().value).toEqual(call(logout))
  })
})

This verifies that the logoutWatcher watches for and takes LOGOUT actions and calls the logout method afterwards. This does not verify that this is actually running in an endless loop.

How do i verify that the watcher is running in an endless loop?

@beldur

This comment has been minimized.

Copy link

commented Mar 14, 2017

In your specific case you could use the redux-saga helper takeEvery (See Docs).
The code would look like this:

import { takeEvery } from 'redux-saga/effects'

import logout from './logout'
import LOGOUT from '../constants'

export function* logoutWatcher () {
  yield takeEvery(LOGOUT, logout)
}

This way you leave the responsibility of running the loop to redux-saga.

takeEvery's loop is tested here with a simple for loop: /test/sagaHelpers/takeEvery.js

@codepunkt

This comment has been minimized.

Copy link
Author

commented Mar 16, 2017

That doesn't verify the endless loop, though. It only verifies the first x runs - which is probably enough ;-)

@aikoven

This comment has been minimized.

Copy link
Collaborator

commented Mar 16, 2017

Isn't it the same as trying to solve the Halting problem?

@codepunkt

This comment has been minimized.

Copy link
Author

commented Mar 16, 2017

@aikoven Most likely is.

Do you

  • test the loop x times?
  • test the loop once?

If you test it more than once - how large is your x?

@aikoven

This comment has been minimized.

Copy link
Collaborator

commented Mar 18, 2017

I think it's enough to test all the states where the process can possibly be, and that it then comes back to the first state.

@codepunkt

This comment has been minimized.

Copy link
Author

commented Mar 19, 2017

Thanks for everyone's opinions! I'm going to close this.

If anyone manages this in a different way, feel free to continue the discussion 👍

@codepunkt codepunkt closed this Mar 19, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.