
Security News
US Government Forces Anthropic to Pull Claude Fable Days After Launch
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.
bqb-msal-react-tester
Advanced tools
A npm package to simplify testing react application using msal-react
The MSAL React Tester is a NPM package to allows you creating unit tests for any of your components that need to be authenticated (or not) using the msal-react package and Azure AD.
The MSAL React Tester package allows you to test your components in any of these scenario:
@azure/msal-react to authenticate your users on Azure AD@testing-library/react package to create and run your tests (you are basically using the built in "test": "react-scripts test" script to execute your tests).The MSAL React Tester package is available on NPM.
npm install --save-dev bqb-msal-react-tester
Creates a [component].test.tsx and import the bqb-msal-react-tester package:
import { MsalReactTester } from 'bqb-msal-react-tester';
MsalReactTester before each test.
spyMsal() method to mock all the MSAL React required methods & events.resetSpyMsal after each test.let msalTester: MsalReactTester;
beforeEach(() => {
// new instance of msal tester for each test:
msalTester = new MsalReactTester();
// or new MsalReactTester("Redirect") / new MsalReactTester("Popup")
// Ask bqb-msal-react-tester to handle and mock all msal-react processes:
msalTester.spyMsal();
});
afterEach(() => {
// reset bqb-msal-react-tester
msalTester.resetSpyMsal();
});
In each of your test, you can now:
msalTester.isNotLogged().msalTester.isLogged().<MsalProvider /> using the msalTester.client as the IPublicClientApplication instance.msalTester.waitForRedirect() to let MSAL React Tester handling MSAL React processesHere is an example where we are testing a <HomePage /> component during a first visit by an authenticated user:
test('Home page render correctly when user is not logged', async () => {
// Mock a guest user, not yet authenticated:
msalTester.isNotLogged();
// Render the <HomePage /> component using a <MsalProvider />
// with the mock IPublicClientApplication instance:
render(
<MsalProvider instance={msalTester.client}>
<MemoryRouter>
<Layout>
<HomePage />
</Layout>
</MemoryRouter>
</MsalProvider>,
);
// Wait for bqb-msal-react-tester to handle events from msal-react:
await msalTester.waitForRedirect();
// Test your <HomePage /> component:
expect(screen.getByText(/Please sign-in/)).toBeInTheDocument();
});
On the other side, you can test the same component assuming your user is "already logged", using msalTester.isLogged().
If you want to test a component during the authentication process, you can use msalTester.waitForLogin():
test('Home page render correctly when user logs in', async () => {
// Mock a guest user, not yet authenticated:
msalTester.isNotLogged();
// Render the <HomePage /> component using a <MsalProvider />
// with the mock IPublicClientApplication instance:
render(
<MsalProvider instance={msalTester.client}>
<MemoryRouter>
<Layout>
<HomePage />
</Layout>
</MemoryRouter>
</MsalProvider>,
);
// Wait for bqb-msal-react-tester to handle events from msal-react:
await msalTester.waitForRedirect();
// Getting the log in button.
// Mock a user click to launch the log in process:
const signin = screen.getByRole('button', { name: 'Sign In' });
userEvent.click(signin);
// Wait for bqb-msal-react-tester to handle the login process from msal-react:
await msalTester.waitForLogin();
// From here, your user is supposed to be logged in the component:
expect(screen.getByRole('button',
{ name: msalTester.activeAccount.name })).toBeInTheDocument();
});
On the other side, if you want to test component during a log out process, use msalTester.waitForLogout()
You will find a full example in the ../example folder.
Take a look on the pages tests:
root
├── example
├──── src
├────── pages
├──────── HomePage.test.tsx
├──────── SearchPage.test.tsx
Licensed under the MIT License.
Feel free to contact me through twitter : @sebpertus
FAQs
A npm package to simplify testing react application using msal-react
We found that bqb-msal-react-tester demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.

Security News
A network of 152 Chrome live wallpaper extensions hid ad tracking and made extension-driven traffic look like Google search clicks.

Company News
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.