Solid session testing

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?

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).

1 Like

Could @jeffz solid-auth-cli help to authen via script ? Solid-auth-cli - browserless persistent login

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

1 Like

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

Thanks @A_A for your comment.
I have seen 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?

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

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.

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.

This is a respective data directory for one of the tests.

3 Likes

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?

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?

1 Like

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, 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:

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

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 is the test in the repo.

image

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

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:

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?)

1 Like

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

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.

2 Likes

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

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?

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

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

…but it seems to obtain friends from its props:

    const {friends, share} = props;

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

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.

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

        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 ;(

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.

2 Likes

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.

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 complements those tests by allowing to test the integration with real data querying.

2 Likes