# Solid session testing

**URL:** <https://forum.solidproject.org/t/solid-session-testing/2858>\
**Category:** App Testing\
**Created:** [April 5, 2020, 4:44pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858 "2020-04-05T16:44:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdrianPerezManso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/adrianperezmanso/32/1504_2.png) [@AdrianPerezManso](https://forum.solidproject.org/u/AdrianPerezManso)\
**Post date:** [April 5, 2020, 4:44pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/1 "2020-04-05T16:44:27Z")

</div>

Hi there,  
I am in a team developing a Solid application, and we are facing some issues regarding tests. We don’t know how to get a fake session in a test, so we can obtain the information of the POD. Any idea?

---

<div class="post-metadata">

**Author:** ![megoth](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/megoth/32/8_2.png) [@megoth](https://forum.solidproject.org/u/megoth)\
**Post date:** [April 5, 2020, 6:18pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/2 "2020-04-05T18:18:33Z")

</div>

You’re trying to do scenario testing or unit testing?

I don’t have that much experience with the former, but suspect you’ll have to specify steps that goes through the authentication steps via the browser.

If you’re trying to do the latter, you should mock the responses you expect to work with, and inject that where necessary (ie you should not rely on network dependencies in your unit tests).

---

<div class="post-metadata">

**Author:** ![Smag0](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/smag0/32/310_2.png) [@Smag0](https://forum.solidproject.org/u/Smag0)\
**Post date:** [April 5, 2020, 7:17pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/3 "2020-04-05T19:17:12Z")

</div>

Could @jeffz solid-auth-cli help to authen via script ? [Solid-auth-cli - browserless persistent login](http://forum.solidproject.org/t/solid-auth-cli-browserless-persistent-login/1016)

---

<div class="post-metadata">

**Author:** ![AdrianPerezManso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/adrianperezmanso/32/1504_2.png) [@AdrianPerezManso](https://forum.solidproject.org/u/AdrianPerezManso)\
**Post date:** [April 6, 2020, 10:39am UTC](https://forum.solidproject.org/t/solid-session-testing/2858/4 "2020-04-06T10:39:11Z")

</div>

We are trying to do unit testing, and what @megoth has commented in the second part is the thing, we don’t actually know how to inyect the mock responses so we can have a fake session

---

<div class="post-metadata">

**Author:** ![A\_A](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/a_a/32/786_2.png) [@A\_A](https://forum.solidproject.org/u/A_A)\
**Post date:** [April 6, 2020, 11:55am UTC](https://forum.solidproject.org/t/solid-session-testing/2858/5 "2020-04-06T11:55:37Z")

</div>

For unit testing, there are already some threads in this forum:

> [@Molid - A mock solid server for local testing](http://forum.solidproject.org/t/molid-a-mock-solid-server-for-local-testing/2447):
>
> When building Solid apps I was in need of a dummy server that just returns some data that I can easily adjust while developing an app - without setting up a real pod and without having to rely on a internet connection. That’s why I built Molid, a very simple mock server that is able to serve fake linked data for local app development. Quick start rocketStart using npx, no installation needed npx molid Molid - mock Solid server listening on port 3333! Molid provides some Solid data, t…

> [@Solid App testing](http://forum.solidproject.org/t/solid-app-testing/2700):
>
> Hi! I’m new to the community of Solid. I’m a software engineering student currently developing [Viade](https://labra.solid.community/public/SoftwareArchitecture/AssignmentDescription/), a route management system based on the Solid specifications, in a team. As of now, we do not have anything implemented, we will be working on the skeleton of the project in the following weeks. The app is implemented using React. My development team and myself are new to React and Solid, and thus many questions arise because we do not know much about the inner workings of both. As for my questi…

---

<div class="post-metadata">

**Author:** ![AdrianPerezManso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/adrianperezmanso/32/1504_2.png) [@AdrianPerezManso](https://forum.solidproject.org/u/AdrianPerezManso)\
**Post date:** [April 6, 2020, 1:51pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/6 "2020-04-06T13:51:43Z")

</div>

Thanks @A_A for your comment.  
I have seen [Molid](https://www.npmjs.com/package/molid) when researching on how to do unit testing on Solid. As far as I have read in the documentation, in order to make a test block, you need a **loadProfile()** function, but I am not sure about how this function is implemented, and should return.

Also, I would want to know how I can add friends to the mock profile in this Molid server. Is it just a matter of adding information to the “profile/card” file?

---

<div class="post-metadata">

**Author:** ![A\_A](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/a_a/32/786_2.png) [@A\_A](https://forum.solidproject.org/u/A_A)\
**Post date:** [April 6, 2020, 2:42pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/7 "2020-04-06T14:42:52Z")

</div>

I haven’t used Molid so I can’t help you with this. But @aveltens probably can help.

---

<div class="post-metadata">

**Author:** ![aveltens](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/aveltens/32/3987_2.png) [@aveltens](https://forum.solidproject.org/u/aveltens)\
**Post date:** [April 6, 2020, 4:21pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/8 "2020-04-06T16:21:50Z")

</div>

Hi @AdrianPerezManso, no you do not need a **loadProfile()** function, this is just a example function called in the test. Just call whatever function you want to test.

> [@AdrianPerezManso](#):
>
> Also, I would want to know how I can add friends to the mock profile in this Molid server. Is it just a matter of adding information to the “profile/card” file?

Yes, you can just add any triples to `profile/card` or create new files as needed just like in a real Solid Pod.

You can find real usage examples of Molid in my pod-homepage project, [e.g. in this test](https://gitlab.com/angelo-v/pod-homepage/-/blob/master/src/articles/__integrationtests/loadPostsFromAllBlogs.integration.spec.js).

[This is a respective data directory](https://gitlab.com/angelo-v/pod-homepage/-/tree/master/src/articles/__integrationtests/with%20a%20blog%20and%20three%20posts.molid) for one of the tests.

---

<div class="post-metadata">

**Author:** ![AdrianPerezManso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/adrianperezmanso/32/1504_2.png) [@AdrianPerezManso](https://forum.solidproject.org/u/AdrianPerezManso)\
**Post date:** [April 6, 2020, 7:13pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/9 "2020-04-06T19:13:10Z")

</div>

OK, I’m trying to test the default test of the documentation of Molid and I’m getting this error. Is something I am misssing?

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/solidproject/original/2X/4/4f1496b96c94778cb1a86014648c6b945f30ce20.png)

---

<div class="post-metadata">

**Author:** ![aveltens](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/aveltens/32/3987_2.png) [@aveltens](https://forum.solidproject.org/u/aveltens)\
**Post date:** [April 7, 2020, 6:04am UTC](https://forum.solidproject.org/t/solid-session-testing/2858/10 "2020-04-07T06:04:35Z")

</div>

Can’t really tell from the screenshot. Can you post a link to the code repo? Did you install Molid as described in [Install for usage within your Solid app project](https://molid.readthedocs.io/en/latest/installation.html#install-for-usage-within-your-solid-app-project)?

---

<div class="post-metadata">

**Author:** ![Vincent](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Vincent](https://forum.solidproject.org/u/Vincent)\
**Post date:** [April 7, 2020, 10:02am UTC](https://forum.solidproject.org/t/solid-session-testing/2858/11 "2020-04-07T10:02:41Z")

</div>

We could probably be of more help with some more information - and ideally a link to your source code, if possible.

How is your application logging in people? Are you using `solid-auth-client` directly, or perhaps e.g. a React Component that does it for you, e.g. when using Inrupt’s React generator? (If the latter, note that you can also get support from Inrupt directly through [its service desk](https://inrupt.atlassian.net/servicedesk/customer/portal/1), though via the forum others can jump in as well.)

Additionally, which unit testing framework are you using? Judging by your screenshot, I would guess Jest?

In case you happen to be using Jest and solid-auth-client directly, you can mock it at the top of your unit test file using something like the following:

```javascript
jest.mock('solid-auth-client', () => ({
  currentSession: jest.fn(() => Promise.resolve({ webId: 'https://arbitrary.pod/profile#me' })),
}));

```

---

<div class="post-metadata">

**Author:** ![AdrianPerezManso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/adrianperezmanso/32/1504_2.png) [@AdrianPerezManso](https://forum.solidproject.org/u/AdrianPerezManso)\
**Post date:** [April 7, 2020, 1:31pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/12 "2020-04-07T13:31:21Z")

</div>

I will focus on @Vincent comment, because I tried the snippet code you have posted, which I thought it was what I initially wanted. As far as I know, this snippet is mocking a session, right? I get the following error, and I don’t understand why. I have removed the Molid code just to test that I have a session, Definitely I am doing something wroing. [This](https://github.com/Arquisoft/viade_en1a/blob/adrian-test/src/ __tests__ /share-routes.test.js) is the test in the repo.

![image](https://europe1.discourse-cdn.com/flex017/uploads/solidproject/original/2X/8/83429c6a31b5c6c12dbfde324b2ffc5c84d44424.png)

This corresponds to the constructor of the `ShareRoutesComponent` class, and it is like `auth` were undefined. Any idea?

---

<div class="post-metadata">

**Author:** ![Vincent](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Vincent](https://forum.solidproject.org/u/Vincent)\
**Post date:** [April 7, 2020, 3:19pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/13 "2020-04-07T15:19:53Z")

</div>

Ah OK, thanks for the source code link. So you’re passing the entirety of solid-auth-client to solid-file-client, which expects it to be fully instantiated - so mocking just a single method does not work.

Try something like this:

```auto
jest.mock('solid-auth-client', () => {
  const auth = jest.requireActual('solid-auth-client');
  auth.currentSession = jest.fn(() => Promise.resolve({ webId: "https://adrianperezmanso.solid.community/profile/card#me" }));
  return auth;
});

```

The line that starts with `auth.currentSession` shows you how to replace a single method of solid-auth-client with a mock; you can do something similar if you need to mock more methods. When I run your tests now it seems they’re failing on something else though, so I think this should be sufficient.

(Molid looks great too btw, but I thought it was more suited to integration tests than for unit tests?)

---

<div class="post-metadata">

**Author:** ![aveltens](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/aveltens/32/3987_2.png) [@aveltens](https://forum.solidproject.org/u/aveltens)\
**Post date:** [April 7, 2020, 6:16pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/14 "2020-04-07T18:16:03Z")

</div>

> [@AdrianPerezManso](#):
>
> I will focus on @Vincent comment, because I tried the snippet code you have posted, which I thought it was what I initially wanted.

Ok, if you need any further help with Molid, let me know, I am glad to help.

> [@Vincent](#):
>
> (Molid looks great too btw, but I thought it was more suited to integration tests than for unit tests?)

Yes, you should not use Molid in unit test. Use it whenever you want to test your function in integration doing actual requests to get data from a controlled environment. But when using Molid you still need to mock solid-auth-client, since Molid is not able to do authentication.

---

<div class="post-metadata">

**Author:** ![AdrianPerezManso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/adrianperezmanso/32/1504_2.png) [@AdrianPerezManso](https://forum.solidproject.org/u/AdrianPerezManso)\
**Post date:** [April 8, 2020, 11:27am UTC](https://forum.solidproject.org/t/solid-session-testing/2858/15 "2020-04-08T11:27:30Z")

</div>

OK, I’ve added the new snippet code, and now the error is thrown in the `ShareRoutesPageContent` component.

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/solidproject/original/2X/b/b0be97e5cb2bcb276515f213450853511e719ac3.png)

I would say it is caused because `friends` attribute contains nothing. This component is rendered in the `ShareRoutesComponent` and I think it still has to do with authentication issues. Am I right?

---

<div class="post-metadata">

**Author:** ![Vincent](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Vincent](https://forum.solidproject.org/u/Vincent)\
**Post date:** [April 8, 2020, 1:53pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/16 "2020-04-08T13:53:39Z")

</div>

That doesn’t appear to have anything to do with authentication issues. You’re calling `<ShareRoutesComponent>` as such:

```javascript
 const component = <ShareRoutesComponent
  {...{
      webId: "https://adrianperezmanso.solid.community/profile/card#me",
  }}

```

…but it seems to obtain `friends` from its props:

```javascript
    const {friends, share} = props;

```

So since you’re not passing a `friends` prop, it will be `undefined`.

---

<div class="post-metadata">

**Author:** ![AdrianPerezManso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/adrianperezmanso/32/1504_2.png) [@AdrianPerezManso](https://forum.solidproject.org/u/AdrianPerezManso)\
**Post date:** [April 8, 2020, 2:18pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/17 "2020-04-08T14:18:04Z")

</div>

But the `ShareRoutesPageContent` (that has `friends` as `props`) is child of `ShareRoutesComponent` (which has `webId` as `props`). The later is the one fetching friends from the POD in the `getProfileData()` method and passing it as `props` to the former. It seems like it is not fetching them, that’s why I said it may be because authentication issues. I know that names are a little bit misleading, but I think it is not because the `props`.

---

<div class="post-metadata">

**Author:** ![Vincent](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Vincent](https://forum.solidproject.org/u/Vincent)\
**Post date:** [April 8, 2020, 2:52pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/18 "2020-04-08T14:52:48Z")

</div>

Whoops, you’re absolutely correct, the second code sample was from the wrong component. That said, `ShareRoutesComponent` _also_ takes `friends` as props:

```javascript
        this.state = {
            friends: props.friends,
            route: null
        };

```

So maybe that will work for the test. Otherwise you’re going to have to mock `@solid/query-ldflex`, which I haven’t done before, but which looks like it’ll be very painful, so I can’t really be of help there unfortunately ;(

---

<div class="post-metadata">

**Author:** ![AdrianPerezManso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/adrianperezmanso/32/1504_2.png) [@AdrianPerezManso](https://forum.solidproject.org/u/AdrianPerezManso)\
**Post date:** [April 8, 2020, 4:19pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/19 "2020-04-08T16:19:11Z")

</div>

It worked pretty well and I’m having good test results!! Thank you so much @Vincent.

Now back to Molid, is there any alternative that is better for unit testing? Since @aveltens remarked it is not optimal for unit testing, but for integration testing.

---

<div class="post-metadata">

**Author:** ![aveltens](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/aveltens/32/3987_2.png) [@aveltens](https://forum.solidproject.org/u/aveltens)\
**Post date:** [April 8, 2020, 4:32pm UTC](https://forum.solidproject.org/t/solid-session-testing/2858/20 "2020-04-08T16:32:41Z")

</div>

You just do not need any server for unit testing, since you only test your unit of code there. In pod-homepage project for example I mocked ldflex in unit test, no network call needed. [You can see an example here](https://gitlab.com/angelo-v/pod-homepage/-/blob/master/src/bookmarks/loadBookmarks.spec.js).

You can also use a fake rdflib store for unit testing, or mock whatever solid lib you are using to handle data. Which one is it? I might be able to help.

[Molid](https://molid.readthedocs.io/en/latest/) complements those tests by allowing to test the integration with real data querying.

[Next page](https://forum.solidproject.org/t/solid-session-testing/2858.md?page=2)
