typesafe-actions icon indicating copy to clipboard operation
typesafe-actions copied to clipboard

[5.1] handleAction throws error in test environment using AVA

Open Ethella opened this issue 4 years ago • 0 comments

Description

Mandatory info

  • [ ] Are you extending internal types to enable type-free syntax with createReducer?
// If yes PASTE HERE your types.d.ts

How to Reproduce

CodeSandbox Link

(PASTE HERE your codesandbox link)

No CodeSandbox Link

If no codesandbox, then please provide a full working code example below including actions, reducers and your custom types used in the example.

import test from 'ava';
import * as actions from '~/app/store/auth/auth.actions';
import { store } from '~/app/store';

test('initAuthState', async t => {

  await store.dispatch(
   actions.initAuthState()
});
// auth.actions.ts
export const initAuthState = createAction(actionTypes.INIT_AUTH_STATE)();

// reducer.ts
const AuthReducer = createReducer<AuthState, AuthActions>(initialState)
  .handleAction(initAuthState, () => ({ ...initialState }));

Error

  Uncaught exception in test/spec/app/store/auth/auth.actions.spec.ts

  /node_modules/typesafe-actions/src/utils/validation.ts:79

  Error: Argument 1 is invalid, it should be an action-creator instance from "typesafe-actions" or action type of type: string | symbol

  throwInvalidActionTypeOrActionCreator (node_modules/typesafe-actions/src/utils/validation.ts:79:9)
  map (node_modules/typesafe-actions/src/create-reducer.ts:120:15)
  Function.reducerHandler (node_modules/typesafe-actions/src/create-reducer.ts:111:8)
  Object.<anonymous> (src/app/store/auth/auth.reducer.ts:2:2138)
  Module.m._compile (node_modules/ts-node/src/index.ts:858:23)
  module.exports (node_modules/default-require-extensions/js.js:7:9)

Error trace https://github.com/piotrwitek/typesafe-actions/blob/cbed746b3e997942cc93c3b4fa5abb876194ee16/src/utils/validation.ts#L79 https://github.com/piotrwitek/typesafe-actions/blob/master/src/create-reducer.ts#L120

Expected behavior

Production seems to be working just fine, but not sure why the error is thrown when creating the reducer in a test environment.

Suggested solution(s)

An example of how to write tests would be helpful.

Project Dependencies

  • Typesafe-Actions Version: ^5.1.0
  • TypeScript Version: ~3.8.3
  • tsconfig.json:
{
  "compilerOptions": {
    "lib": ["esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "target": "es5",
    "jsx": "react",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitReturns": false,
    "noImplicitThis": true,
    "noImplicitAny": false,
    "esModuleInterop": true,
    "downlevelIteration": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowJs": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "outDir": "../build",
    "baseUrl": "..",
    "paths": {
      "~/*": ["src/*"],
    },
  },

  "include": [
    "../src/**/*.ts",
    "../src/**/*.tsx",
    "../test/**/*.ts",
    "../test/**/*.tsx"
  ],
}

Environment (optional)

  • Browser and Version: XXX
  • OS: XXX
  • Node Version: XXX
  • Package Manager and Version: XXX

Ethella avatar Nov 30 '20 09:11 Ethella