While creating new E2E tests for a SharePoint Framework solution, I wanted to test the solution with different permission sets. Usually, when you run your tests, you start these up with a user with the required permissions to run the tests. Instead of creating a new user with different permissions or different sites/pages to perform the tests, I looked into the possibility of overriding the context properties of SPFx.
By overriding the context properties, you can perform the tests against the same page and, in my case, the same user but with different permissions.
How to override the context properties
You first need access to the context
object to override the context properties. When working in an SPFx solution, you can access it from this.context
, but during an E2E test, you need to get access to it differently.
There is a moduleLoaderPromise
property on the window
object, a promise that resolves to the moduleLoader
object together with the spClientSidePageContext
object.
To gain access, you can use the following code:
|
|
With the context
object, you can override the properties. For example, in my case, I want to override the permissions of the web
object to mock a user with owner, member, or visitor permissions.
|
|
With the overridden permissions, you can perform your tests against the same page. You can use the overridePermissions
function to switch between permission sets.
|
|
Conclusion
By overriding the context properties of SPFx, you can perform your E2E tests against the same page but with mocked properties, allowing you to test your solution quicker without creating new users, pages, or sites for each test case.
Of course, for in-depth testing of your solution, it would be better to use a user with the correct permission set, but for simple cases like when your solution needs to show or hide certain elements, the current solution is a perfect fit.