# \[SOLVED\] Testing action dispatch

**URL:** <https://discourse.aurelia.io/t/solved-testing-action-dispatch/2860>\
**Category:** Help Requests\
**Created:** [August 22, 2019, 5:45pm UTC](https://discourse.aurelia.io/t/solved-testing-action-dispatch/2860 "2019-08-22T17:45:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jenselme](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/jenselme/32/39_2.png) [@Jenselme](https://discourse.aurelia.io/u/Jenselme)\
**Post date:** [August 22, 2019, 5:45pm UTC](https://discourse.aurelia.io/t/solved-testing-action-dispatch/2860/1 "2019-08-22T17:45:47Z")

</div>

Hi,

I’d like to test a component. This component dispatches an action in its activation method. Sadly, when I call the `activate` method, I get this error:

```auto
    TypeError: Cannot read property 'mark' of undefined

      at Store.<anonymous> (node_modules/aurelia-store/dist/commonjs/aurelia-store.js:375:57)
      at step (node_modules/aurelia-store/dist/commonjs/aurelia-store.js:94:23)
      at Object.next (node_modules/aurelia-store/dist/commonjs/aurelia-store.js:75:53)
      at node_modules/aurelia-store/dist/commonjs/aurelia-store.js:68:71
      at __awaiter (node_modules/aurelia-store/dist/commonjs/aurelia-store.js:64:12)
      at Store.Object.<anonymous>.Store.internalDispatch (node_modules/aurelia-store/dist/commonjs/aurelia-store.js:365:16)
      at Store.<anonymous> (node_modules/aurelia-store/dist/commonjs/aurelia-store.js:346:51)
      at step (node_modules/aurelia-store/dist/commonjs/aurelia-store.js:94:23)
      at Object.next (node_modules/aurelia-store/dist/commonjs/aurelia-store.js:75:53)

```

From what I see, it comes from `aureliaPal.PLATFORM.performance.mark("dispatch-start");` in the code of the plugin. I am using Jest as a test runner and Webpack as a bundler. Any idea how to solve this? I’d really like to fully test this.

My test file:

```auto
import {bootstrap} from 'aurelia-bootstrapper';
import {Container, PLATFORM} from 'aurelia-framework';
import {Store} from 'aurelia-store';
import {StageComponent} from 'aurelia-testing';
import {registerRssBackendsActions} from '../../../src/store/rss-backends-actions';
import {createInitialState} from '../../ __fixtures__ /state-fixtures';

describe('Route: DisplayArticlesFromCategory', () => {
    describe('Render tests', () => {
        let container;
        let component;
        let store;
        let state;

        beforeEach(async() => {
            container = new Container();
            container.makeGlobal();
            state = createInitialState();
            state.rss.isFetching = true;
            store = new Store(state);
            // FIXME: This is required to make the test pass.
            store.dispatch = jest.fn().mockImplementation(() => {});
            registerRssBackendsActions(store);
            container.registerInstance(Store, store);

            component = StageComponent.withResources(PLATFORM.moduleName('../../src/routes/display-articles-from-category'))
                .inView('<display-articles-from-category></display-articles-from-category>');

            await component.create(bootstrap);
            component.viewModel.store = store;
            component.viewModel.activate();

            return component;
        });

        it('should render and match snapshot fetching', async() => {
            await component.waitForElement('.fa-spinner');

            expect(document.body.outerHTML).toMatchSnapshot();

            component.dispose();
        });
    });
});

```

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [August 23, 2019, 5:35am UTC](https://discourse.aurelia.io/t/solved-testing-action-dispatch/2860/2 "2019-08-23T05:35:43Z")

</div>

Most likely you are using an older version of the pal for nodejs. Take a [look at this PR](https://github.com/aurelia/store/commit/35d6db60358da11dc4f2cc614f32b9597ca5db7a) when it got introduced. Its also tested using jest.

---

<div class="post-metadata">

**Author:** ![Jenselme](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/jenselme/32/39_2.png) [@Jenselme](https://discourse.aurelia.io/u/Jenselme)\
**Post date:** [August 23, 2019, 1:07pm UTC](https://discourse.aurelia.io/t/solved-testing-action-dispatch/2860/3 "2019-08-23T13:07:51Z")

</div>

I am using `aurelia-pal-nodejs` version 1.2.0 so it should be good. I tried to add some `console.log` in `node_modules/aurelia-store/dist/commonjs/aurelia-store.js` and `PLATFORM.performance` is indeed undefined there but `PLATFORM.global.performance` is. If I update the code in aurelia-store to use `PLATFORM.global.performance`, it works as expected.

What is really strange is that in `jest-pretest.ts` and my test file, `PLATFORM.performance` is correctly defined.

Any idea where this can come from?

Note: you can clone the project with `git clone https://gitlab.com/Jenselme/aurss.git --branch test/display-articles-from-category`. To run tests, run `yarn test`. It may help you understand the problem.

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [August 24, 2019, 4:56pm UTC](https://discourse.aurelia.io/t/solved-testing-action-dispatch/2860/4 "2019-08-24T16:56:38Z")

</div>

I have no idea why that is, maybe an issue with pal 1.2. anyways I’ll give your repro a try next week to see what I can find out

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [August 26, 2019, 8:02am UTC](https://discourse.aurelia.io/t/solved-testing-action-dispatch/2860/5 "2019-08-26T08:02:42Z")

</div>

So I’ve:

- pulled your sample
- npm installed it
- npm run test

and all tests are passing.

```auto
Test Suites: 8 passed, 8 total
Tests: 34 passed, 34 total
Snapshots: 6 passed, 6 total

```

Is there already a workaround added to the current code base? Can you point me to the exact issue?

I’ve also tried it with `yarn test` and the only difference is that the tests take 19,98s instead of 5,32s but aside from that ok. Perhaps `yarn install` is the difference?

In npm you have the command `npm list aurelia-pal-nodejs` which shows you the used versions and whether there are multiple of them used. Dunno what the equivalent of yarn is but try checking it. Also try truncating the whole node\_modules folder and delete and package-lock (yarn equivalent) and do a fresh install.

---

<div class="post-metadata">

**Author:** ![Jenselme](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/jenselme/32/39_2.png) [@Jenselme](https://discourse.aurelia.io/u/Jenselme)\
**Post date:** [August 26, 2019, 10:28am UTC](https://discourse.aurelia.io/t/solved-testing-action-dispatch/2860/6 "2019-08-26T10:28:20Z")

</div>

Removing `yarn.lock` and reinstalling did fix it. I should have though about that! Anyway, thanks for the help!

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [August 26, 2019, 12:54pm UTC](https://discourse.aurelia.io/t/solved-testing-action-dispatch/2860/7 "2019-08-26T12:54:51Z")

</div>

I stopped counting how many times ive got bitten by this 😆
