enhancement: prevent committing tests with only (#21141)

---------

Co-authored-by: Alexandre BODIN <alexandrebodin@users.noreply.github.com>
This commit is contained in:
Ben Irvin 2024-09-04 18:04:56 +02:00 committed by GitHub
parent e0397985ec
commit d773bfffcd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 22 deletions

View File

@ -1,4 +1,11 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
# Check for .only in the staged changes of test files
if git --no-pager diff --cached | grep -E '^\+\s*(test|it|describe)\.only'; then
echo "Error: .only found in staged changes. Please remove it before committing."
exit 1
fi
# Run lint-staged if no .only is found
yarn lint-staged

View File

@ -20,31 +20,12 @@ const LoginPage = () => {
};
describe('PrivateRoute', () => {
// TODO: fix this, it doesn't actually clear the login between tests
beforeEach(() => {
window.localStorage.clear();
});
it('Authenticated users should be able to access protected routes', async () => {
// Login
window.localStorage.setItem('jwtToken', JSON.stringify('access-token'));
render(
<>
<Route path="/auth/login" component={LoginPage} />
<PrivateRoute path="/">
<ProtectedPage />
</PrivateRoute>
</>,
{
initialEntries: ['/protected'],
}
);
// Should see the protected route
expect(await screen.findByText('You are authenticated'));
});
it.only('Unauthenticated users should not be able to access protected routes and get redirected', async () => {
it('Unauthenticated users should not be able to access protected routes and get redirected', async () => {
let testLocation: Location = null!;
let testHistory: History = null!;
@ -85,4 +66,24 @@ describe('PrivateRoute', () => {
expect(screen.getByText('Please login')).toBeInTheDocument();
});
it('Authenticated users should be able to access protected routes', async () => {
// Login
window.localStorage.setItem('jwtToken', JSON.stringify('access-token'));
render(
<>
<Route path="/auth/login" component={LoginPage} />
<PrivateRoute path="/">
<ProtectedPage />
</PrivateRoute>
</>,
{
initialEntries: ['/protected'],
}
);
// Should see the protected route
expect(await screen.findByText('You are authenticated'));
});
});

View File

@ -20,7 +20,7 @@ const render = (props) => ({
describe('AssetCardBase', () => {
describe('Interaction', () => {
it.only('should call onSelect when the checkbox is clicked', async () => {
it('should call onSelect when the checkbox is clicked', async () => {
const onSelect = jest.fn();
const { getByRole, user } = render({
onSelect,