redux-things/redux-actions-assertions

Tape support

adamyonk opened this issue · 6 comments

I can get the tests working with something like this:

test('someThunk', (t) => {
  registerMiddlewares([thunk])
  registerInitialStoreState(buildInitialStoreState(rootReducer))

  const expectedActions = [
    actions...
  ]

  const initialState = {
    ...
  }

  assertions.toDispatchActionsWithState(
    initialState,
    someAction(),
    expectedActions,
    t.end
  )
})

This kind of works, because if something goes wrong in toDispatchActionsWithState, tape catches the thrown error and fails the test run, but it doesn't count the test in the tape output when it's passing, because you never actually assert anything with tape (t.equal, t.deepEqual, etc.). If there was some way to get the output actions of toDispatchActionsWithState, I could do some assertion like this:

  const expectedActions = [
    actions...
  ]

  const initialState = {
    ...
  }

  assertions.toDispatchActionsWithState(
    initialState,
    someAction(),
    expectedActions,
    (actualActions) => {
      t.deepEqual(
         actualActions,
         expectedActions,
         'should run the correct actions'
      )
    }
  )

I saw that you can pass a done and fail function in, but I think that would leave us in the same boat here. I'm not sure that an actual tape integration is necessary, if there is a way to get at the array of the actions that were dispatched and to not have a mismatch throw an error (so it could be handled by equal, deepEqual, and friends.

Can you please try to pass tape.pass and tape.fail as done and error callbacks into assertion? Should work in that case.

As I can see from docs, it will generate assertion:

t.fail(msg)
Generate a failing assertion with a message msg.
t.pass(msg)
Generate a passing assertion with a message msg.

Aha, I didn't know that pass would also increment the count. 👍

Here's what ended up working (don't forget end!):

  assertions.toDispatchActionsWithState(
    { ...state },
    action(),
    [ ...expected ],
    t.pass,
    t.fail
  ).then(t.end)

You can use t.end in then block, or you can use t.plan(n) which is more natural for tape.

After all, would be good to add Tape to list of supported frameworks and add doc for it.

I'd be happy to start a PR, if that would be helpful to you.

@adamyonk Sure!
would be also good to include tape into the project and by analogy, create tests with tape as we have with other frameworks.