Skip to content
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

Loads global .env in vite.config to fix vscode extension #9884

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
12 changes: 6 additions & 6 deletions common_knowledge/Package-Scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,31 +299,31 @@ See `test-unit`.

### test-api

Definition: `INIT_TEST_DB=true NODE_ENV=test vitest --config ../../vite.config.ts --fileParallelism=false run ./test/integration/api`
Definition: `NODE_ENV=test vitest --config ../../vite.config.ts run ./test/integration/api`

Description: Runs all tests in the /api subfolder of the /integration directory.

### test-integration

Definition: `INIT_TEST_DB=true NODE_ENV=test vitest --config ../../vite.config.ts --fileParallelism=false run ./test/integration`
Definition: `NODE_ENV=test vitest --config ../../vite.config.ts run ./test/integration`

Description: Runs all tests in the /test/integration folder (includes API tests).

### test-devnet:evm

Definition: `INIT_TEST_DB=true NODE_ENV=test vitest --config ../../vite.config.ts --fileParallelism=false run ./test/devnet/evm`
Definition: `NODE_ENV=test vitest --config ../../vite.config.ts run ./test/devnet/evm`

Description: Runs all tests in our `/devnet/evm` folder.

### test-devnet:cosmos

Definition: `INIT_TEST_DB=true NODE_ENV=test vitest --config ../../vite.config.ts --fileParallelism=false run ./test/devnet/cosmos`
Definition: `NODE_ENV=test vitest --config ../../vite.config.ts run ./test/devnet/cosmos`

Description: Runs all tests in our `/devnet/cosmos` folder.

### test-select

Definition: `INIT_TEST_DB=true NODE_ENV=test vitest --config ../../vite.config.ts --fileParallelism=false run`
Definition: `NODE_ENV=test vitest --config ../../vite.config.ts run`

Description: Append a path to run specific test files or folders.

Expand All @@ -335,7 +335,7 @@ Description: Tests all .spec files within the `./test/unit` sub-directory of tes

### test-select:watch

Definition: `INIT_TEST_DB=true NODE_ENV=test vitest --config ../../vite.config.ts --fileParallelism=false`
Definition: `NODE_ENV=test vitest --config ../../vite.config.ts`

Description: Watches for changes to any .spec files within the given path and automatically runs test when they are updated.

Expand Down
4 changes: 2 additions & 2 deletions libs/model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"build": "tsc -b ./tsconfig.build.json",
"clean": "rm -rf build && rm -rf coverage && find . -type f -name '*.tsbuildinfo' -exec rm {} +",
"check-types": "tsc --noEmit",
"test": "INIT_TEST_DB=true NODE_ENV=test vitest --config ../../vite.config.ts --fileParallelism=false --coverage run test",
"test-select": "INIT_TEST_DB=true NODE_ENV=test vitest --config ../../vite.config.ts --fileParallelism=false run",
"test": "NODE_ENV=test vitest --config ../../vite.config.ts --coverage run test",
"test-select": "NODE_ENV=test vitest --config ../../vite.config.ts run",
"lint": "NODE_OPTIONS=\"--max-old-space-size=4096\" eslint -c ../../.eslintrc.cjs './src/**/*.{ts,tsx}'",
"lint-diff": "NODE_OPTIONS=\"--max-old-space-size=4096\" eslint -c ../../.eslintrc-diff.cjs './src/**/*.{ts,tsx}'"
},
Expand Down
7 changes: 1 addition & 6 deletions libs/model/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { configure, config as target } from '@hicommonwealth/core';
import { z } from 'zod';

const {
TEST_DB_NAME,
DATABASE_URL,
DATABASE_CLEAN_HOUR,
DATABASE_LOG_TRACE,
Expand All @@ -13,7 +12,6 @@ const {
PRIVATE_KEY,
TBC_BALANCE_TTL_SECONDS,
BLACKLISTED_EVENTS,
INIT_TEST_DB,
MAX_USER_POSTS_PER_CONTEST,
JWT_SECRET,
ADDRESS_TOKEN_EXPIRES_IN,
Expand Down Expand Up @@ -43,8 +41,7 @@ const {
OPENAI_ORGANIZATION,
} = process.env;

const NAME =
target.NODE_ENV === 'test' ? TEST_DB_NAME || 'common_test' : 'commonwealth';
const NAME = target.NODE_ENV === 'test' ? 'common_test' : 'commonwealth';

const DEFAULTS = {
JWT_SECRET: 'my secret',
Expand All @@ -67,7 +64,6 @@ export const config = configure(
CLEAN_HOUR: DATABASE_CLEAN_HOUR
? parseInt(DATABASE_CLEAN_HOUR, 10)
: undefined,
INIT_TEST_DB: INIT_TEST_DB === 'true',
TRACE: DATABASE_LOG_TRACE === 'true',
},
WEB3: {
Expand Down Expand Up @@ -175,7 +171,6 @@ export const config = configure(
NAME: z.string(),
NO_SSL: z.boolean(),
CLEAN_HOUR: z.coerce.number().int().min(0).max(24).optional(),
INIT_TEST_DB: z.boolean(),
TRACE: z.boolean(),
}),
WEB3: z.object({
Expand Down
58 changes: 38 additions & 20 deletions libs/model/src/tester/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import { QueryTypes, Sequelize } from 'sequelize';
import { SequelizeStorage, Umzug } from 'umzug';
import { config } from '../config';
import { buildDb, type DB } from '../models';
import { buildDb, syncDb, type DB } from '../models';

/**
* Verifies the existence of a database,
Expand Down Expand Up @@ -32,7 +32,8 @@ export const verify_db = async (name: string): Promise<void> => {
}
} catch (error) {
console.error(`Error verifying db [${name}]:`, error);
throw error;
// ignore verification errors
// throw error;
} finally {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
pg && pg.close();
Expand Down Expand Up @@ -205,31 +206,48 @@ export const get_info_schema = async (
// so that the db object does not need to be rebuilt for every to bootstrap_testing from within
// a single test suite
let db: DB | undefined = undefined;
let bootstrapLock: Promise<DB> | undefined = undefined;

async function _bootstrap_testing(filename: string): Promise<DB> {
if (!db) {
try {
await verify_db(config.DB.NAME);
db = buildDb(
new Sequelize({
dialect: 'postgres',
database: config.DB.NAME,
username: 'commonwealth',
password: 'edgeware',
logging: false,
}),
);
await syncDb(db);
console.log('Database synced:', filename);
} catch (e) {
console.error('Error bootstrapping test db:', e);
throw e;
}
}
return db;
}

/**
* Bootstraps testing, creating/migrating a fresh instance if it doesn't exist.
* @meta import meta of calling test
* @param truncate when true, truncates all tables in model
* @returns synchronized sequelize db instance
*/
export const bootstrap_testing = async (truncate = false): Promise<DB> => {
if (!db) {
db = buildDb(
new Sequelize({
dialect: 'postgres',
database: config.DB.NAME,
username: 'commonwealth',
password: 'edgeware',
logging: false,
}),
);
console.log('Database object built');
export const bootstrap_testing = async (meta: ImportMeta): Promise<DB> => {
const filename = path.basename(meta.filename);
if (bootstrapLock) {
return await bootstrapLock;
}

if (truncate) {
await truncate_db(db);
console.log('Database truncated');
bootstrapLock = _bootstrap_testing(filename);
try {
return await bootstrapLock;
} finally {
bootstrapLock = undefined;
}

return db;
};

config.NODE_ENV === 'test' && dispose(async () => truncate_db(db));
1 change: 0 additions & 1 deletion libs/model/src/tester/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from './bootstrap';
export * from './e2eSeeds';
export * from './seed';
export * from './seedDb';
export { setup } from './vitestDatabaseSetup';
2 changes: 1 addition & 1 deletion libs/model/src/tester/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function seed<T extends schemas.Aggregates>(
values?: DeepPartial<z.infer<(typeof schemas)[T]>>,
options: SeedOptions = { mock: true },
): Promise<[z.infer<(typeof schemas)[T]> | undefined, State[]]> {
const db = await bootstrap_testing();
const db = await bootstrap_testing(import.meta);

const records: State[] = [];
await _seed(db![name], values ?? {}, options, records, 0);
Expand Down
4 changes: 2 additions & 2 deletions libs/model/src/tester/seedDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { bootstrap_testing } from './bootstrap';
* such as associated IDs and seed values. Additionally, not all tests require every
* entity to be seeded, so focus should be on seeding only what is explicitly needed.
*/
export const seedDb = async () => {
export const seedDb = async (meta: ImportMeta) => {
try {
const models = await bootstrap_testing(true);
const models = await bootstrap_testing(meta);

await models.User.bulkCreate(
[{ email: '[email protected]' }, { email: '[email protected]' }].map(
Expand Down
38 changes: 0 additions & 38 deletions libs/model/src/tester/vitestDatabaseSetup.ts

This file was deleted.

3 changes: 1 addition & 2 deletions libs/model/test/community/stake-historical-price.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Actor, dispose, query } from '@hicommonwealth/core';
import { BalanceType } from '@hicommonwealth/shared';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { bootstrap_testing, seed } from 'model/src/tester';
import { seed } from 'model/src/tester';
import { afterAll, beforeAll, describe, test } from 'vitest';
import { GetStakeHistoricalPrice } from '../../src/community/GetStakeHistoricalPrice.query';

Expand All @@ -13,7 +13,6 @@ describe('Stake Historical Price', () => {
let actor: Actor;

beforeAll(async () => {
await bootstrap_testing(true);
const [node] = await seed('ChainNode', {
url: 'https://ethereum-sepolia.publicnode.com',
name: 'Sepolia Testnet',
Expand Down
3 changes: 1 addition & 2 deletions libs/model/test/community/stake-transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Actor, command, dispose, query } from '@hicommonwealth/core';
import { BalanceType, commonProtocol } from '@hicommonwealth/shared';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { bootstrap_testing, seed } from 'model/src/tester';
import { seed } from 'model/src/tester';
import { afterAll, beforeAll, describe, test } from 'vitest';
import {
CreateStakeTransaction,
Expand All @@ -17,7 +17,6 @@ describe('Stake transactions', () => {
let community_id: string;

beforeAll(async () => {
await bootstrap_testing(true);
const [node] = await seed('ChainNode', {
url: 'https://ethereum-sepolia.publicnode.com',
private_url: 'https://ethereum-sepolia.publicnode.com',
Expand Down
3 changes: 1 addition & 2 deletions libs/model/test/contest-worker/contest-worker-policy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dispose, handleEvent } from '@hicommonwealth/core';
import { afterAll, beforeAll, describe, test } from 'vitest';
import { commonProtocol, models } from '../../src';
import { ContestWorker } from '../../src/policies';
import { bootstrap_testing, seed } from '../../src/tester';
import { seed } from '../../src/tester';

describe('Contest Worker Policy', () => {
const addressId = 444;
Expand All @@ -17,7 +17,6 @@ describe('Contest Worker Policy', () => {
let topicId: number = 0;

beforeAll(async () => {
await bootstrap_testing();
const [chainNode] = await seed('ChainNode', { contracts: [] });
const [user] = await seed(
'User',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import chaiAsPromised from 'chai-as-promised';
import { afterAll, beforeAll, describe, test } from 'vitest';
import z from 'zod';
import { Contest, TopicAttributes } from '../../src/index';
import { bootstrap_testing, seed } from '../../src/tester';
import { seed } from '../../src/tester';

chai.use(chaiAsPromised);

Expand All @@ -31,7 +31,6 @@ describe('Contests metadata commands lifecycle', () => {
let communityMemberActor: Actor | null = null;

beforeAll(async () => {
await bootstrap_testing();
const [chain] = await seed('ChainNode', {});

const [communityAdminUser] = await seed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ describe('Contests projection lifecycle', () => {
getContestScore = Sinon.stub(contestHelper, 'getContestScore');
getContestStatus = Sinon.stub(contestHelper, 'getContestStatus');

await bootstrap_testing();
// TODO: add ContractAbi to seeder aggregates and replace direct model calls below to avoid calling this here
await bootstrap_testing(import.meta);

try {
const recurringContestAbi = await models.ContractAbi.create({
Expand Down
3 changes: 1 addition & 2 deletions libs/model/test/email/digest-email-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ExternalServiceUserIds, dispose, query } from '@hicommonwealth/core';
import { models } from '@hicommonwealth/model';
import { Community } from '@hicommonwealth/schemas';
import { expect } from 'chai';
import { bootstrap_testing, seed } from 'model/src/tester';
import { seed } from 'model/src/tester';
import { afterAll, afterEach, beforeAll, describe, test } from 'vitest';
import { z } from 'zod';
import { GetDigestEmailDataQuery } from '../../src/emails';
Expand All @@ -14,7 +14,6 @@ describe('Digest email lifecycle', () => {
let communityThree: z.infer<typeof Community> | undefined;

beforeAll(async () => {
await bootstrap_testing(true);
const [authorUser] = await seed('User', {
isAdmin: false,
selected_community_id: null,
Expand Down
3 changes: 1 addition & 2 deletions libs/model/test/launchpad/launchpad.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { config } from '@hicommonwealth/model';
import { BalanceType, commonProtocol } from '@hicommonwealth/shared';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { bootstrap_testing, seed } from 'model/src/tester';
import { seed } from 'model/src/tester';
import { afterAll, beforeAll, describe, test } from 'vitest';
import { ChainNodeAttributes } from '../../src';
import { CreateLaunchpadTrade, CreateToken } from '../../src/token';
Expand All @@ -20,7 +20,6 @@ describe('Launchpad Lifecycle', () => {
let node: ChainNodeAttributes;

beforeAll(async () => {
await bootstrap_testing(true);
[node] = (await seed('ChainNode', {
url: `https://base-sepolia.g.alchemy.com/v2/${config.ALCHEMY.APP_KEYS.PUBLIC}`,
private_url: `https://base-sepolia.g.alchemy.com/v2/${config.ALCHEMY.APP_KEYS.PUBLIC}`,
Expand Down
3 changes: 1 addition & 2 deletions libs/model/test/reaction/reaction-lifecycle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dispose } from '@hicommonwealth/core';
import { expect } from 'chai';
import { bootstrap_testing, seed } from 'model/src/tester';
import { seed } from 'model/src/tester';
import { afterAll, beforeAll, describe, test } from 'vitest';
import { models } from '../../src/database';

Expand All @@ -10,7 +10,6 @@ describe('Reactions lifecycle', () => {
const threadId = 999;

beforeAll(async () => {
await bootstrap_testing();
const [chain] = await seed('ChainNode', { contracts: [] });
const [user] = await seed(
'User',
Expand Down
4 changes: 2 additions & 2 deletions libs/model/test/seed/model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../src/tester';

const generateSchemas = async () => {
const model = await bootstrap_testing();
const model = await bootstrap_testing(import.meta);
const migration = await create_db_from_migrations('common_migrated_test');

// TODO: resolve remaining conflicts!!!
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('Model schema', () => {

beforeAll(async () => {
schemas = await generateSchemas();
});
}, 20000);

afterAll(async () => {
await dispose()();
Expand Down
Loading
Loading