setup-factories
a setup factory that arranges through anything but its own parameters
Agent checkadvisory by defaultbasetests
The argument
Setup Factories
const setupAvatar = ({
profile = null,
gravatar = null,
}: { profile?: string | null; gravatar?: string | null } = {}) => {
mockGetAvatarFromProfile.mockReturnValue(profile);
mockGetAvatarFromGravatar.mockReturnValue(gravatar);
const userProfile = new UserProfile({ profileData: { email: 'user@example.com' } });
const appSettings = new AppSettings({ defaultPreferences: {} });
return { userProfile, appSettings };
};
The proof
failsrc/profile/getAvatarUrl.unit.test.ts
import { expect, describe, test } from '@jest/globals';
import { getAvatarUrl } from './getAvatarUrl';
// The arrangement lives in module state the factory mutates, so what a test
// gets depends on what ran before it.
let currentProfile: string | null = null;
const setupAvatar = () => {
return { userProfile: { avatar: currentProfile } };
};
describe('getAvatarUrl', () => {
test('returns the profile avatar when one exists', () => {
currentProfile = 'p.png';
const { userProfile } = setupAvatar();
const avatarUrl = getAvatarUrl({ userProfile });
expect(avatarUrl).toBe('p.png');
});
});
passsrc/profile/getAvatarUrl.unit.test.ts
import { expect, describe, test } from '@jest/globals';
import { getAvatarUrl } from './getAvatarUrl';
const setupAvatar = ({ profile = null }: { profile?: string | null } = {}) => {
const userProfile = { avatar: profile };
return { userProfile };
};
const setupGuestAvatar = () => {
const userProfile = { avatar: null, isGuest: true };
return { userProfile };
};
describe('getAvatarUrl', () => {
test('returns the profile avatar when one exists', () => {
const { userProfile } = setupAvatar({ profile: 'p.png' });
const avatarUrl = getAvatarUrl({ userProfile });
expect(avatarUrl).toBe('p.png');
});
});
Turn it down
Both lines go in your lightsout.config.json.
"standards-checks": { "setup-factories": "advisory" }"standards-checks": { "setup-factories": "off" }