Skip to content

chore(clerk-js): Refactor snapshot tests #6094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/gentle-crews-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
45 changes: 43 additions & 2 deletions packages/clerk-js/src/core/resources/__tests__/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,27 @@ describe('Client Singleton', () => {
// @ts-expect-error We cannot mess with the singleton when tests are running in parallel
const client = new Client(clientJSON);

expect(client).toMatchSnapshot();
expect(client).toMatchObject({
id: 'client_DUMMY_ID',
sessions: [],
signUp: expect.objectContaining({
status: null,
missingFields: [],
unverifiedFields: [],
verifications: expect.objectContaining({
emailAddress: expect.objectContaining({
status: null,
}),
}),
}),
signIn: expect.objectContaining({
status: null,
identifier: null,
}),
lastActiveSessionId: null,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
});
});

it('__internal_toSnapshot()', () => {
Expand Down Expand Up @@ -404,6 +424,27 @@ describe('Client Singleton', () => {
// @ts-expect-error We cannot mess with the singleton when tests are running in parallel
const client = new Client(clientJSON);

expect(client.__internal_toSnapshot()).toMatchSnapshot();
expect(client.__internal_toSnapshot()).toMatchObject({
object: 'client',
id: 'client_DUMMY_ID',
sessions: [],
sign_up: expect.objectContaining({
status: null,
missing_fields: [],
unverified_fields: [],
verifications: expect.objectContaining({
email_address: expect.objectContaining({
status: null,
}),
}),
}),
sign_in: expect.objectContaining({
status: null,
identifier: null,
}),
last_active_session_id: null,
created_at: expect.any(Number),
updated_at: expect.any(Number),
});
});
});
122 changes: 119 additions & 3 deletions packages/clerk-js/src/core/resources/__tests__/Environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,41 @@ describe('Environment', () => {
it('defaults values when instantiated without arguments', () => {
const environment = new Environment();

expect(environment).toMatchSnapshot();
expect(environment).toMatchObject({
authConfig: expect.objectContaining({
singleSessionMode: false,
reverification: false,
}),
displayConfig: expect.objectContaining({
instanceEnvironmentType: '',
preferredSignInStrategy: 'password',
branded: false,
showDevModeWarning: false,
}),
userSettings: expect.objectContaining({
attributes: expect.objectContaining({
email_address: expect.objectContaining({
enabled: true,
required: true,
used_for_first_factor: true,
verify_at_sign_up: true,
}),
password: expect.objectContaining({
enabled: true,
required: true,
}),
}),
actions: expect.objectContaining({
delete_self: false,
create_organization: false,
}),
}),
organizationSettings: expect.objectContaining({
enabled: false,
maxAllowedMemberships: 1,
}),
maintenanceMode: false,
});
});

it('has the same initial properties', () => {
Expand Down Expand Up @@ -246,7 +280,53 @@ describe('Environment', () => {

const environment = new Environment(environmentJSON);

expect(environment).toMatchSnapshot();
expect(environment).toMatchObject({
authConfig: expect.objectContaining({
singleSessionMode: true,
reverification: true,
}),
displayConfig: expect.objectContaining({
instanceEnvironmentType: 'development',
applicationName: '',
branded: true,
showDevModeWarning: true,
preferredSignInStrategy: 'password',
theme: expect.objectContaining({
general: expect.objectContaining({
color: '#6c47ff',
}),
}),
}),
userSettings: expect.objectContaining({
attributes: expect.objectContaining({
email_address: expect.objectContaining({
enabled: true,
required: true,
used_for_first_factor: true,
verify_at_sign_up: true,
}),
password: expect.objectContaining({
enabled: true,
required: true,
}),
}),
actions: expect.objectContaining({
delete_self: true,
create_organization: true,
}),
social: expect.objectContaining({
oauth_google: expect.objectContaining({
enabled: true,
authenticatable: true,
}),
}),
}),
organizationSettings: expect.objectContaining({
enabled: false,
maxAllowedMemberships: 5,
}),
maintenanceMode: false,
});
});

it('__internal_toSnapshot()', () => {
Expand Down Expand Up @@ -485,6 +565,42 @@ describe('Environment', () => {

const environment = new Environment(environmentJSON);

expect(environment.__internal_toSnapshot()).toMatchSnapshot();
expect(environment.__internal_toSnapshot()).toMatchObject({
object: 'environment',
auth_config: expect.objectContaining({
single_session_mode: true,
reverification: true,
}),
display_config: expect.objectContaining({
instance_environment_type: 'development',
application_name: '',
branded: true,
show_devmode_warning: true,
preferred_sign_in_strategy: 'password',
}),
user_settings: expect.objectContaining({
attributes: expect.objectContaining({
email_address: expect.objectContaining({
enabled: true,
required: true,
used_for_first_factor: true,
verify_at_sign_up: true,
}),
password: expect.objectContaining({
enabled: true,
required: true,
}),
}),
actions: expect.objectContaining({
delete_self: true,
create_organization: true,
}),
}),
organization_settings: expect.objectContaining({
enabled: false,
max_allowed_memberships: 5,
}),
maintenance_mode: false,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ describe('Organization', () => {
has_image: true,
});

expect(organization).toMatchSnapshot();
expect(organization).toMatchObject({
id: 'test_id',
name: 'test_name',
slug: 'test_slug',
hasImage: true,
imageUrl:
'https://img.clerk.com/eyJ0eXBlIjoiZGVmYXVsdCIsImlpZCI6Imluc18xbHlXRFppb2JyNjAwQUtVZVFEb1NsckVtb00iLCJyaWQiOiJ1c2VyXzJKbElJQTN2VXNjWXh1N2VUMnhINmFrTGgxOCIsImluaXRpYWxzIjoiREsifQ?width=160',
membersCount: 1,
pendingInvitationsCount: 10,
maxAllowedMemberships: 3,
adminDeleteEnabled: true,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
publicMetadata: {
public: 'metadata',
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ describe('OrganizationDomain', () => {
total_pending_suggestions: 0,
});

expect(organization).toMatchSnapshot();
expect(organization).toMatchObject({
id: 'test_domain_id',
name: 'clerk.com',
organizationId: 'test_org_id',
enrollmentMode: 'manual_invitation',
affiliationEmailAddress: '[email protected]',
totalPendingInvitations: 0,
totalPendingSuggestions: 0,
verification: {
attempts: 1,
expiresAt: expect.any(Date),
status: 'verified',
strategy: 'email_code',
},
});
});

it('has the same initial nullable properties', () => {
Expand All @@ -39,6 +53,15 @@ describe('OrganizationDomain', () => {
total_pending_suggestions: 0,
});

expect(organization).toMatchSnapshot();
expect(organization).toMatchObject({
id: 'test_domain_id',
name: 'clerk.com',
organizationId: 'test_org_id',
enrollmentMode: 'manual_invitation',
affiliationEmailAddress: null,
totalPendingInvitations: 0,
totalPendingSuggestions: 0,
verification: null,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ describe('OrganizationInvitation', () => {
status: 'pending',
});

expect(organizationInvitation).toMatchSnapshot();
expect(organizationInvitation).toMatchObject({
id: 'test_id',
emailAddress: 'test_email',
organizationId: 'test_organization_id',
role: 'basic_member',
roleName: undefined,
status: 'pending',
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
publicMetadata: {
public: 'metadata',
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OrganizationMembership } from '../internal';

describe('OrganizationMembership', () => {
it('has the same initial properties', () => {
const organizationMemberShip = new OrganizationMembership({
const organizationMembership = new OrganizationMembership({
object: 'organization_membership',
id: 'test_id',
created_at: 12345,
Expand Down Expand Up @@ -40,6 +40,35 @@ describe('OrganizationMembership', () => {
},
});

expect(organizationMemberShip).toMatchSnapshot();
expect(organizationMembership).toMatchObject({
id: 'test_id',
role: 'admin',
roleName: undefined,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
permissions: [],
publicMetadata: {
foo: 'bar',
},
organization: expect.objectContaining({
id: 'test_org_id',
name: 'test_name',
slug: 'test_slug',
hasImage: true,
membersCount: 1,
pendingInvitationsCount: 10,
maxAllowedMemberships: 3,
adminDeleteEnabled: true,
publicMetadata: {
public: 'metadata',
},
}),
publicUserData: expect.objectContaining({
firstName: 'test_first_name',
lastName: 'test_last_name',
hasImage: true,
identifier: '[email protected]',
}),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ describe('OrganizationMembership', () => {
},
});

expect(organizationMembershipRequest).toMatchSnapshot();
expect(organizationMembershipRequest).toMatchObject({
id: 'test_id',
organizationId: 'test_org_id',
status: 'pending',
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
publicUserData: expect.objectContaining({
firstName: 'test_first_name',
lastName: 'test_last_name',
hasImage: true,
identifier: '[email protected]',
imageUrl: 'https://clerk.com',
}),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ describe('OrganizationSuggestion', () => {
updated_at: 5678,
});

expect(organizationSuggestion).toMatchSnapshot();
expect(organizationSuggestion).toMatchObject({
id: 'test_id',
status: 'pending',
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
publicOrganizationData: {
hasImage: true,
id: 'test_org_id',
imageUrl: 'test_image_url',
name: 'Test org',
slug: 'test-org',
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ describe('PublicUserData', () => {
user_id: '123',
});

expect(JSON.stringify(pud)).toMatchSnapshot();
expect(pud).toMatchObject({
firstName: 'John',
lastName: 'Doe',
imageUrl: 'https://example.com/image.jpg',
hasImage: true,
identifier: 'john-doe',
userId: '123',
});
});
});
Loading
Loading