From ea6bfc0b3d54938077d3f83d33f74e48828032bb Mon Sep 17 00:00:00 2001 From: worksofliam Date: Fri, 17 Jan 2025 13:46:32 -0500 Subject: [PATCH] Tests run concurrently Signed-off-by: worksofliam --- .../tests/{globalSetup.ts => connection.ts} | 25 +++++++++++++------ src/api/tests/env.ts | 11 -------- src/api/tests/setup.ts | 18 +++++++++++++ src/api/tests/suites/components.test.ts | 4 +-- src/api/tests/suites/connection.test.ts | 4 +-- src/api/tests/suites/content.test.ts | 2 +- src/api/tests/suites/encoding.test.ts | 4 +-- src/api/tests/suites/search.test.ts | 4 +-- src/api/tests/testConfigSetup.ts | 6 ++++- vitest.config.ts | 4 +-- 10 files changed, 52 insertions(+), 30 deletions(-) rename src/api/tests/{globalSetup.ts => connection.ts} (79%) delete mode 100644 src/api/tests/env.ts create mode 100644 src/api/tests/setup.ts diff --git a/src/api/tests/globalSetup.ts b/src/api/tests/connection.ts similarity index 79% rename from src/api/tests/globalSetup.ts rename to src/api/tests/connection.ts index 1c5c6440d..20b124851 100644 --- a/src/api/tests/globalSetup.ts +++ b/src/api/tests/connection.ts @@ -1,7 +1,4 @@ -import assert from "assert"; import IBMi from "../IBMi"; -import { ENV_CREDS } from "./env"; -import { afterAll, beforeAll, expect } from "vitest"; import { CodeForIStorage } from "../configuration/storage/CodeForIStorage"; import { CustomQSh } from "../components/cqsh"; import path from "path"; @@ -11,8 +8,17 @@ import { GetNewLibl } from "../components/getNewLibl"; import { extensionComponentRegistry } from "../components/manager"; import { JsonConfig, JsonStorage } from "./testConfigSetup"; +export const testStorage = new JsonStorage(); const testConfig = new JsonConfig(); -const testStorage = new JsonStorage(); + +export const CONNECTION_TIMEOUT = process.env.VITE_CONNECTION_TIMEOUT ? parseInt(process.env.VITE_CONNECTION_TIMEOUT) : 25000; + +const ENV_CREDS = { + host: process.env.VITE_SERVER || `localhost`, + user: process.env.VITE_DB_USER, + password: process.env.VITE_DB_PASS, + port: parseInt(process.env.VITE_DB_PORT || `22`) +} export async function newConnection() { const virtualStorage = testStorage; @@ -62,13 +68,18 @@ export async function newConnection() { } ); - expect(result).toBeDefined(); - expect(result.success).toBeTruthy(); + if (!result.success) { + throw new Error(`Failed to connect to IBMi`); + } return conn; } -export function disposeConnection(conn: IBMi) { +export function disposeConnection(conn?: IBMi) { + if (!conn) { + return; + } + conn.dispose(); testStorage.save(); testConfig.save(); diff --git a/src/api/tests/env.ts b/src/api/tests/env.ts deleted file mode 100644 index 1e5129459..000000000 --- a/src/api/tests/env.ts +++ /dev/null @@ -1,11 +0,0 @@ - -/** - * Default credentials for connecting to the daemon server, - * loaded from environment variables. - */ -export const ENV_CREDS = { - host: process.env.VITE_SERVER || `localhost`, - user: process.env.VITE_DB_USER, - password: process.env.VITE_DB_PASS, - port: parseInt(process.env.VITE_DB_PORT || `22`) -} \ No newline at end of file diff --git a/src/api/tests/setup.ts b/src/api/tests/setup.ts new file mode 100644 index 000000000..43baac0ce --- /dev/null +++ b/src/api/tests/setup.ts @@ -0,0 +1,18 @@ +import type { TestProject } from "vitest/node"; +import { disposeConnection, newConnection, testStorage } from "./connection"; + +export async function setup(project: TestProject) { + // You might pre-connect to simply create the configuration files. + // When the config files exist, it makes future connections just slightly faster. + // Mostly useful during the CI stage. + if (!testStorage.exists()) { + console.log(``); + console.log(`Testing connection before tests run since configs do not exist.`); + + const conn = await newConnection(); + disposeConnection(conn); + + console.log(`Testing connection complete. Configs written.`); + console.log(``); + } +} \ No newline at end of file diff --git a/src/api/tests/suites/components.test.ts b/src/api/tests/suites/components.test.ts index 7feac8ffb..b21c03802 100644 --- a/src/api/tests/suites/components.test.ts +++ b/src/api/tests/suites/components.test.ts @@ -3,13 +3,13 @@ import { GetMemberInfo } from '../../components/getMemberInfo'; import { GetNewLibl } from '../../components/getNewLibl'; import { Tools } from '../../Tools'; import IBMi from '../../IBMi'; -import { disposeConnection, newConnection } from '../globalSetup'; +import { CONNECTION_TIMEOUT, disposeConnection, newConnection } from '../connection'; describe('Component Tests', () => { let connection: IBMi beforeAll(async () => { connection = await newConnection(); - }) + }, CONNECTION_TIMEOUT) afterAll(async () => { disposeConnection(connection); diff --git a/src/api/tests/suites/connection.test.ts b/src/api/tests/suites/connection.test.ts index 3f0c749c5..91bc78a90 100644 --- a/src/api/tests/suites/connection.test.ts +++ b/src/api/tests/suites/connection.test.ts @@ -1,7 +1,7 @@ import { expect, describe, afterAll, beforeAll, it } from 'vitest' import { Tools } from '../../Tools'; -import { disposeConnection, newConnection } from '../globalSetup'; +import { disposeConnection, newConnection } from '../connection'; import IBMi from '../../IBMi'; import { getJavaHome } from '../../configuration/DebugConfiguration'; @@ -9,7 +9,7 @@ describe(`connection tests`, {concurrent: true}, () => { let connection: IBMi beforeAll(async () => { connection = await newConnection(); - }) + }, 25000) afterAll(async () => { disposeConnection(connection); diff --git a/src/api/tests/suites/content.test.ts b/src/api/tests/suites/content.test.ts index 2a09cb67f..168f158de 100644 --- a/src/api/tests/suites/content.test.ts +++ b/src/api/tests/suites/content.test.ts @@ -5,7 +5,7 @@ import tmp from 'tmp'; import { Tools } from '../../Tools'; import { posix } from 'path'; import IBMi from '../../IBMi'; -import { newConnection, disposeConnection } from '../globalSetup'; +import { newConnection, disposeConnection } from '../connection'; describe('Content Tests', {concurrent: true}, () => { let connection: IBMi diff --git a/src/api/tests/suites/encoding.test.ts b/src/api/tests/suites/encoding.test.ts index 10183a31e..a3fe2b123 100644 --- a/src/api/tests/suites/encoding.test.ts +++ b/src/api/tests/suites/encoding.test.ts @@ -3,7 +3,7 @@ import IBMi from "../../IBMi"; import { Tools } from "../../Tools"; import { IBMiObject } from "../../types"; import { describe, it, expect, afterAll, beforeAll } from 'vitest'; -import { newConnection, disposeConnection } from "../globalSetup"; +import { newConnection, disposeConnection } from "../connection"; const contents = { '37': [`Hello world`], @@ -43,7 +43,7 @@ describe('Encoding tests', {concurrent: true} ,() => { let connection: IBMi beforeAll(async () => { connection = await newConnection(); - }) + }, 25000) afterAll(async () => { disposeConnection(connection); diff --git a/src/api/tests/suites/search.test.ts b/src/api/tests/suites/search.test.ts index 2cd829c0d..302aa84a1 100644 --- a/src/api/tests/suites/search.test.ts +++ b/src/api/tests/suites/search.test.ts @@ -2,13 +2,13 @@ import { describe, it, expect, afterAll, beforeAll } from 'vitest'; import { parseFilter } from '../../Filter'; import { Search } from '../../Search'; import IBMi from '../../IBMi'; -import { newConnection, disposeConnection } from '../globalSetup'; +import { newConnection, disposeConnection } from '../connection'; describe('Search Tests', {concurrent: true}, () => { let connection: IBMi beforeAll(async () => { connection = await newConnection(); - }) + }, 25000) afterAll(async () => { disposeConnection(connection); diff --git a/src/api/tests/testConfigSetup.ts b/src/api/tests/testConfigSetup.ts index 21bc2e3e2..b0e55180c 100644 --- a/src/api/tests/testConfigSetup.ts +++ b/src/api/tests/testConfigSetup.ts @@ -44,8 +44,12 @@ export class JsonStorage extends BaseStorage { super(); } + exists() { + return existsSync(storagePath); + } + public async load() { - if (existsSync(storagePath)) { + if (this.exists()) { const data = await import(storagePath); for (const key in data) { this.globalState.set(key, data[key]); diff --git a/vitest.config.ts b/vitest.config.ts index ff1871fb8..6d4a56792 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -4,8 +4,8 @@ import { defineConfig } from 'vite' export default defineConfig({ test: { // ... Specify options here. - fileParallelism: false, root: './src/api', - testTimeout: 10000 + globalSetup: [`tests/setup.ts`], + testTimeout: 10000, }, }) \ No newline at end of file