2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 10:58:59 +00:00
books/tests/helpers.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

2022-10-31 08:09:59 +00:00
import { DatabaseManager } from 'backend/database/manager';
2022-04-25 06:33:31 +00:00
import { config } from 'dotenv';
2022-10-31 08:09:59 +00:00
import { Fyo } from 'fyo';
import { DummyAuthDemux } from 'fyo/tests/helpers';
import { DateTime } from 'luxon';
import path from 'path';
import setupInstance from 'src/setup/setupInstance';
import { SetupWizardOptions } from 'src/setup/types';
import test from 'tape';
import { getFiscalYear } from 'utils/misc';
export function getTestSetupWizardOptions(): SetupWizardOptions {
return {
2022-04-25 06:33:31 +00:00
logo: null,
companyName: 'Test Company',
country: 'India',
fullname: 'Test Person',
email: 'test@testmyfantasy.com',
bankName: 'Test Bank of Scriptia',
currency: 'INR',
fiscalYearStart: DateTime.fromJSDate(
getFiscalYear('04-01', true)!
).toISODate(),
fiscalYearEnd: DateTime.fromJSDate(
getFiscalYear('04-01', false)!
).toISODate(),
chartOfAccounts: 'India - Chart of Accounts',
};
}
2022-04-25 06:33:31 +00:00
2022-05-10 09:26:17 +00:00
export function getTestDbPath(dbPath?: string) {
2022-04-25 06:33:31 +00:00
config();
2022-05-10 09:26:17 +00:00
return dbPath ?? process.env.TEST_DB_PATH ?? ':memory:';
2022-04-25 06:33:31 +00:00
}
2022-10-31 08:09:59 +00:00
/**
* Test Boilerplate
2022-12-14 06:57:40 +00:00
*
* The bottom three functions are test boilerplate for when
* an initialized fyo object is to be used.
2022-12-14 06:57:40 +00:00
*
* They are required because top level await is not supported.
*
2022-12-14 06:57:40 +00:00
* Therefore setup and cleanup of the fyo object is wrapped
* in tests which are executed serially (and awaited in order)
* by tape.
2022-12-14 06:57:40 +00:00
*
* If `closeTestFyo` is not called the test process won't exit.
*/
2022-10-31 08:09:59 +00:00
export function getTestFyo(): Fyo {
return new Fyo({
DatabaseDemux: DatabaseManager,
AuthDemux: DummyAuthDemux,
isTest: true,
isElectron: false,
});
}
const ext = '.spec.ts';
/* eslint-disable @typescript-eslint/no-misused-promises */
export function setupTestFyo(fyo: Fyo, filename: string) {
const testName = path.basename(filename, ext);
return test(`setup: ${testName}`, async () => {
const options = getTestSetupWizardOptions();
const dbPath = getTestDbPath();
await setupInstance(dbPath, options, fyo);
});
}
export function closeTestFyo(fyo: Fyo, filename: string) {
const testName = path.basename(filename, ext);
return test(`cleanup: ${testName}`, async () => {
await fyo.close();
});
}