Skip to content

Commit

Permalink
test(logging): replace jest with vitest (#5765)
Browse files Browse the repository at this point in the history
  • Loading branch information
eventualbuddha committed Dec 19, 2024
1 parent 7ce7460 commit f800bb9
Show file tree
Hide file tree
Showing 74 changed files with 290 additions and 215 deletions.
2 changes: 1 addition & 1 deletion apps/admin/backend/src/adjudication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test('adjudicateVote', () => {

test('adjudicateWriteIn', async () => {
const store = Store.memoryStore();
const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });
const electionData = electionTwoPartyPrimaryFixtures.electionJson.asText();
const electionId = store.addElection({
electionData,
Expand Down
20 changes: 16 additions & 4 deletions apps/admin/backend/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ beforeEach(() => {

test('starts with default logger and port', async () => {
const auth = buildMockDippedSmartCardAuth();
const workspace = createWorkspace(dirSync().name, mockBaseLogger());
const workspace = createWorkspace(
dirSync().name,
mockBaseLogger({ fn: jest.fn })
);
const logger = buildMockLogger(auth, workspace);
const { usbDrive } = createMockUsbDrive();
const { printer } = createMockPrinterHandler();
Expand All @@ -43,7 +46,10 @@ test('starts with default logger and port', async () => {

test('start with config options', async () => {
const auth = buildMockDippedSmartCardAuth();
const workspace = createWorkspace(dirSync().name, mockBaseLogger());
const workspace = createWorkspace(
dirSync().name,
mockBaseLogger({ fn: jest.fn })
);
const logger = buildMockLogger(auth, workspace);
const { usbDrive } = createMockUsbDrive();
const { printer } = createMockPrinterHandler();
Expand All @@ -65,7 +71,10 @@ test('start with config options', async () => {

test('errors on start with no workspace', async () => {
const auth = buildMockDippedSmartCardAuth();
const workspace = createWorkspace(dirSync().name, mockBaseLogger());
const workspace = createWorkspace(
dirSync().name,
mockBaseLogger({ fn: jest.fn })
);
const logger = buildMockLogger(auth, workspace);
const { usbDrive } = createMockUsbDrive();
const { printer } = createMockPrinterHandler();
Expand Down Expand Up @@ -98,7 +107,10 @@ test('errors on start with no workspace', async () => {

test('logs device attach/un-attach events', async () => {
const auth = buildMockDippedSmartCardAuth();
const workspace = createWorkspace(dirSync().name, mockBaseLogger());
const workspace = createWorkspace(
dirSync().name,
mockBaseLogger({ fn: jest.fn })
);
const logger = buildMockLogger(auth, workspace);
const { usbDrive } = createMockUsbDrive();
const { printer } = createMockPrinterHandler();
Expand Down
4 changes: 2 additions & 2 deletions apps/admin/backend/src/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('create a file store', async () => {
const tmpDir = tmpNameSync();
await fs.mkdir(tmpDir);
const tmpDbPath = join(tmpDir, 'ballots.db');
const store = Store.fileStore(tmpDbPath, mockBaseLogger());
const store = Store.fileStore(tmpDbPath, mockBaseLogger({ fn: jest.fn }));

expect(store).toBeInstanceOf(Store);
expect(store.getDbPath()).toEqual(tmpDbPath);
Expand Down Expand Up @@ -604,7 +604,7 @@ test('deleteElection reclaims disk space (vacuums the database)', async () => {
const tmpDir = tmpNameSync();
await fs.mkdir(tmpDir);
const tmpDbPath = join(tmpDir, 'data.db');
const store = Store.fileStore(tmpDbPath, mockBaseLogger());
const store = Store.fileStore(tmpDbPath, mockBaseLogger({ fn: jest.fn }));

const electionId = store.addElection({
electionData: electionTwoPartyPrimaryFixtures.electionJson.asText(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ test('reads and parses an Election Results Reporting file', async () => {
const filepath = tmpNameSync();
await writeFile(filepath, JSON.stringify(errContents));

const logger = mockLogger();
const logger = mockLogger({ fn: jest.fn });

const result = await parseElectionResultsReportingFile(filepath, logger);
assert(result.isOk(), 'Unexpected error in test when parsing ERR file');
expect(result.ok()).toEqual(errContents);
});

test('logs on file reading error', async () => {
const logger = mockLogger();
const logger = mockLogger({ fn: jest.fn });
const result = await parseElectionResultsReportingFile(
'./not/a/real/filepath',
logger
Expand Down
4 changes: 2 additions & 2 deletions apps/admin/backend/src/tabulation/full_results.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ const candidateContestId =

test('tabulateElectionResults - write-in handling', async () => {
const store = Store.memoryStore();
const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

const electionDefinition =
electionGridLayoutNewHampshireTestBallotFixtures.readElectionDefinition();
Expand Down Expand Up @@ -704,7 +704,7 @@ test('tabulateElectionResults - write-in handling', async () => {

test('tabulateElectionResults - group and filter by voting method', async () => {
const store = Store.memoryStore();
const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });
const electionDefinition =
electionGridLayoutNewHampshireTestBallotFixtures.readElectionDefinition();
const { castVoteRecordExport } =
Expand Down
4 changes: 2 additions & 2 deletions apps/admin/backend/src/util/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const initializeGetWorkspaceDiskSpaceSummaryMock = mockOf(

test('createWorkspace', () => {
const dir = tmp.dirSync();
const workspace = createWorkspace(dir.name, mockBaseLogger());
const workspace = createWorkspace(dir.name, mockBaseLogger({ fn: jest.fn }));
expect(workspace.path).toEqual(dir.name);
expect(workspace.store).toBeInstanceOf(Store);
});
Expand All @@ -30,7 +30,7 @@ test('disk space tracking setup', () => {
initializeGetWorkspaceDiskSpaceSummaryMock.mockReturnValueOnce(
getWorkspaceDiskSpaceSummary
);
const workspace = createWorkspace(dir.name, mockBaseLogger());
const workspace = createWorkspace(dir.name, mockBaseLogger({ fn: jest.fn }));
expect(initializeGetWorkspaceDiskSpaceSummaryMock).toHaveBeenCalledTimes(1);
expect(initializeGetWorkspaceDiskSpaceSummaryMock).toHaveBeenCalledWith(
workspace.store,
Expand Down
6 changes: 5 additions & 1 deletion apps/admin/backend/test/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export function buildMockLogger(
return mockLogger({
source: LogSource.VxAdminService,
getCurrentRole: () => getUserRole(auth, workspace),
fn: jest.fn,
});
}

Expand All @@ -148,7 +149,10 @@ export function buildTestEnvironment(workspaceRoot?: string) {
deleteTmpFileAfterTestSuiteCompletes(defaultWorkspaceRoot);
return defaultWorkspaceRoot;
})();
const workspace = createWorkspace(resolvedWorkspaceRoot, mockBaseLogger());
const workspace = createWorkspace(
resolvedWorkspaceRoot,
mockBaseLogger({ fn: jest.fn })
);
const logger = buildMockLogger(auth, workspace);
const mockUsbDrive = createMockUsbDrive();
const mockPrinterHandler = createMockPrinterHandler();
Expand Down
2 changes: 1 addition & 1 deletion apps/central-scan/backend/src/app.deprecated_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let mockUsbDrive: MockUsbDrive;

beforeEach(() => {
auth = buildMockDippedSmartCardAuth();
workspace = createWorkspace(dirSync().name, mockBaseLogger());
workspace = createWorkspace(dirSync().name, mockBaseLogger({ fn: jest.fn }));
workspace.store.setElectionAndJurisdiction({
electionData:
electionGridLayoutNewHampshireTestBallotFixtures.electionJson.asText(),
Expand Down
2 changes: 1 addition & 1 deletion apps/central-scan/backend/src/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let logger: Logger;
beforeEach(async () => {
const port = await getPort();
auth = buildMockDippedSmartCardAuth();
workspace = createWorkspace(dirSync().name, mockBaseLogger());
workspace = createWorkspace(dirSync().name, mockBaseLogger({ fn: jest.fn }));
logger = buildMockLogger(auth, workspace);
const scanner = makeMockScanner();

Expand Down
7 changes: 5 additions & 2 deletions apps/central-scan/backend/src/importer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { createWorkspace } from './util/workspace';
import { makeMockScanner } from '../test/util/mocks';

test('no election is configured', async () => {
const workspace = createWorkspace(dirSync().name, mockBaseLogger());
const workspace = createWorkspace(
dirSync().name,
mockBaseLogger({ fn: jest.fn })
);
const scanner = makeMockScanner();
const importer = new Importer({
workspace,
scanner,
logger: mockLogger(),
logger: mockLogger({ fn: jest.fn }),
});

await expect(importer.startImport()).rejects.toThrowError(
Expand Down
5 changes: 4 additions & 1 deletion apps/central-scan/backend/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { start } from './server';

test('logs device attach/un-attach events', async () => {
const auth = buildMockDippedSmartCardAuth();
const workspace = createWorkspace(dirSync().name, mockBaseLogger());
const workspace = createWorkspace(
dirSync().name,
mockBaseLogger({ fn: jest.fn })
);
const logger = buildMockLogger(auth, workspace);
const { usbDrive } = createMockUsbDrive();
const scanner = makeMockScanner();
Expand Down
4 changes: 2 additions & 2 deletions apps/central-scan/backend/src/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test('get/set scanner as backed up', () => {

test('batch cleanup works correctly', () => {
const dbFile = tmp.fileSync();
const store = Store.fileStore(dbFile.name, mockBaseLogger());
const store = Store.fileStore(dbFile.name, mockBaseLogger({ fn: jest.fn }));

store.reset();

Expand Down Expand Up @@ -730,7 +730,7 @@ test('iterating over each accepted sheet includes correct batch sequence id', ()

test('resetElectionSession', () => {
const dbFile = tmp.fileSync();
const store = Store.fileStore(dbFile.name, mockBaseLogger());
const store = Store.fileStore(dbFile.name, mockBaseLogger({ fn: jest.fn }));
store.setElectionAndJurisdiction({
electionData,
jurisdiction,
Expand Down
2 changes: 1 addition & 1 deletion apps/central-scan/backend/src/util/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('disk space tracking setup', () => {
initializeGetWorkspaceDiskSpaceSummaryMock.mockReturnValueOnce(
getWorkspaceDiskSpaceSummary
);
const workspace = createWorkspace(dir.name, mockBaseLogger());
const workspace = createWorkspace(dir.name, mockBaseLogger({ fn: jest.fn }));
expect(initializeGetWorkspaceDiskSpaceSummaryMock).toHaveBeenCalledTimes(1);
expect(initializeGetWorkspaceDiskSpaceSummaryMock).toHaveBeenCalledWith(
workspace.store,
Expand Down
6 changes: 5 additions & 1 deletion apps/central-scan/backend/test/helpers/setup_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function buildMockLogger(
return mockLogger({
source: LogSource.VxCentralScanService,
getCurrentRole: () => getUserRole(auth, workspace),
fn: jest.fn,
});
}

Expand All @@ -49,7 +50,10 @@ export async function withApp(
): Promise<void> {
const port = await getPort();
const auth = buildMockDippedSmartCardAuth();
const workspace = createWorkspace(dirSync().name, mockBaseLogger());
const workspace = createWorkspace(
dirSync().name,
mockBaseLogger({ fn: jest.fn })
);
const logger = buildMockLogger(auth, workspace);
const scanner = makeMockScanner();
const importer = new Importer({ workspace, scanner, logger });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test('says the sheet is unreadable if it is', async () => {
})
);

const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

renderInAppContext(<BallotEjectScreen isTestMode />, { apiMock, logger });

Expand Down Expand Up @@ -159,7 +159,7 @@ test('says the ballot sheet is overvoted if it is', async () => {
})
);

const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

renderInAppContext(<BallotEjectScreen isTestMode />, { apiMock, logger });

Expand Down Expand Up @@ -271,7 +271,7 @@ test('says the ballot sheet is undervoted if it is', async () => {
})
);

const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

renderInAppContext(<BallotEjectScreen isTestMode />, { apiMock, logger });

Expand Down Expand Up @@ -390,7 +390,7 @@ test('says the ballot sheet is blank if it is', async () => {
})
);

const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

renderInAppContext(<BallotEjectScreen isTestMode />, { apiMock, logger });

Expand Down Expand Up @@ -458,7 +458,7 @@ test('calls out official ballot sheets in test mode', async () => {
})
);

const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

renderInAppContext(<BallotEjectScreen isTestMode />, { apiMock, logger });

Expand Down Expand Up @@ -524,7 +524,7 @@ test('calls out test ballot sheets in live mode', async () => {
})
);

const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

renderInAppContext(<BallotEjectScreen isTestMode={false} />, {
apiMock,
Expand Down Expand Up @@ -577,7 +577,7 @@ test('shows invalid election screen when appropriate', async () => {
})
);

const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

renderInAppContext(<BallotEjectScreen isTestMode={false} />, {
apiMock,
Expand Down Expand Up @@ -695,7 +695,7 @@ test('does not allow tabulating the overvote if disallowCastingOvervotes is set'
})
);

const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

renderInAppContext(<BallotEjectScreen isTestMode />, { apiMock, logger });

Expand Down Expand Up @@ -736,7 +736,7 @@ test('says the scanner needs cleaning if a streak is detected', async () => {
})
);

const logger = mockBaseLogger();
const logger = mockBaseLogger({ fn: jest.fn });

renderInAppContext(<BallotEjectScreen isTestMode />, { apiMock, logger });

Expand Down
5 changes: 4 additions & 1 deletion apps/design/backend/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export function testSetupHelpers() {
const servers: Server[] = [];

function setupApp() {
const workspace = createWorkspace(tmp.dirSync().name, mockBaseLogger());
const workspace = createWorkspace(
tmp.dirSync().name,
mockBaseLogger({ fn: jest.fn })
);
const { store } = workspace;
const speechSynthesizer = new GoogleCloudSpeechSynthesizerWithDbCache({
store,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ function getMockStateMachine() {

function buildTestApi() {
const store = Store.memoryStore();
const workspace = createWorkspace(tmp.dirSync().name, mockBaseLogger(), {
store,
});
const workspace = createWorkspace(
tmp.dirSync().name,
mockBaseLogger({ fn: jest.fn }),
{
store,
}
);
const mockAuth = buildMockInsertedSmartCardAuth();
const mockStateMachine = getMockStateMachine();

Expand Down
5 changes: 4 additions & 1 deletion apps/mark-scan/backend/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,10 @@ test('addDiagnosticRecord', async () => {
});

test('startPaperHandlerDiagnostic fails test if no state machine', async () => {
const workspace = createWorkspace(tmp.dirSync().name, mockBaseLogger());
const workspace = createWorkspace(
tmp.dirSync().name,
mockBaseLogger({ fn: jest.fn })
);
const app = buildApp(mockAuth, logger, workspace, mockUsbDrive.usbDrive);
const serverNoStateMachine = app.listen();
const { port } = serverNoStateMachine.address() as AddressInfo;
Expand Down
10 changes: 7 additions & 3 deletions apps/mark-scan/backend/src/app.ui_strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ jest.mock('@votingworks/utils', (): typeof import('@votingworks/utils') => ({
}));

const store = Store.memoryStore();
const workspace = createWorkspace(tmp.dirSync().name, mockBaseLogger(), {
store,
});
const workspace = createWorkspace(
tmp.dirSync().name,
mockBaseLogger({ fn: jest.fn }),
{
store,
}
);
const mockAuth = buildMockInsertedSmartCardAuth();
const electionDefinition = safeParseElectionDefinition(
JSON.stringify(testCdfBallotDefinition)
Expand Down
2 changes: 1 addition & 1 deletion apps/mark-scan/backend/src/audio/outputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jest.mock('@votingworks/basics', (): typeof import('@votingworks/basics') => ({
const mockExecFile = mockOf(execFile);
const mockSleep = mockOf(sleep);
const mockGetNodeEnv = mockOf(getNodeEnv);
const mockLog = mockLogger();
const mockLog = mockLogger({ fn: jest.fn });

test('setAudioOutput - success on retry', async () => {
mockGetNodeEnv.mockReturnValue('production');
Expand Down
Loading

0 comments on commit f800bb9

Please sign in to comment.