diff --git a/packages/app/.env.test b/packages/app/.env.test new file mode 100644 index 000000000..ccbfd09e1 --- /dev/null +++ b/packages/app/.env.test @@ -0,0 +1,13 @@ +##### +# VITEST CONFIGURATION +# Copy and configure the variables below in your own `.env.test.local` file +##### + +# URL of h5grove support server +VITEST_H5GROVE_URL=http://localhost:8888 + +# Name of test file +VITEST_H5GROVE_TEST_FILE=sample.h5 + +# Set to `true` to skip testing h5grove API +VITEST_H5GROVE_SKIP= diff --git a/packages/app/src/providers/h5grove/h5grove-api.test.ts b/packages/app/src/providers/h5grove/h5grove-api.test.ts index 5b91a56bc..d2a734541 100644 --- a/packages/app/src/providers/h5grove/h5grove-api.test.ts +++ b/packages/app/src/providers/h5grove/h5grove-api.test.ts @@ -1,18 +1,27 @@ import { assertDataset, + assertEnvVar, assertGroup, assertGroupWithChildren, hasNonNullShape, } from '@h5web/shared/guards'; -import { expect, test } from 'vitest'; +import { beforeAll, expect, test } from 'vitest'; +import { assertListeningAt } from '../../test-utils'; import { getValueOrError } from '../utils'; import { H5GroveApi } from './h5grove-api'; -const H5GROVE_URL = 'http://localhost:8888'; // when running `pnpm support:h5grove` -const TEST_FILE = 'sample.h5'; +const SKIP = import.meta.env.VITEST_H5GROVE_SKIP === 'true'; +const H5GROVE_URL = import.meta.env.VITEST_H5GROVE_URL; +const TEST_FILE = import.meta.env.VITEST_H5GROVE_TEST_FILE; +assertEnvVar(H5GROVE_URL, 'VITE_H5GROVE_URL'); +assertEnvVar(TEST_FILE, 'VITE_TEST_FILE'); -test('test file matches snapshot', async () => { +beforeAll(async () => { + await assertListeningAt(H5GROVE_URL); +}); + +test.skipIf(SKIP)('test file matches snapshot', async () => { const api = new H5GroveApi(H5GROVE_URL, TEST_FILE, { params: { file: TEST_FILE }, }); diff --git a/packages/app/src/test-utils.tsx b/packages/app/src/test-utils.tsx index 349724da6..11bfd18ac 100644 --- a/packages/app/src/test-utils.tsx +++ b/packages/app/src/test-utils.tsx @@ -129,3 +129,14 @@ export function mockConsoleMethod( } }); } + +export async function assertListeningAt( + url: string, + message = `Expected server listening at ${url}`, +) { + try { + await fetch(url); + } catch { + throw new Error(message); + } +} diff --git a/packages/app/vite.config.js b/packages/app/vite.config.js index 794d8cbd2..488e2b90e 100644 --- a/packages/app/vite.config.js +++ b/packages/app/vite.config.js @@ -1,6 +1,7 @@ import react from '@vitejs/plugin-react-swc'; import fs from 'fs'; import path from 'path'; +import { loadEnv } from 'vite'; import { patchCssModules } from 'vite-css-modules'; import { defineProject } from 'vitest/config'; @@ -33,6 +34,7 @@ export default defineProject({ test: { setupFiles: ['src/setupTests.ts'], environment: 'jsdom', + env: loadEnv('test', import.meta.dirname, 'VITEST_'), restoreMocks: true, testTimeout: 15_000, server: { diff --git a/packages/h5wasm/.env.test b/packages/h5wasm/.env.test new file mode 100644 index 000000000..e8b23f0ff --- /dev/null +++ b/packages/h5wasm/.env.test @@ -0,0 +1,10 @@ +##### +# VITEST CONFIGURATION +# Copy and configure the variables below in your own `.env.test.local` file +##### + +# Path to HDF5 test file, relative to the root of the monorepo +VITEST_H5WASM_TEST_FILE=support/sample/dist/sample.h5 + +# Set to `true` to skip testing h5wasm API +VITEST_H5WASM_SKIP= diff --git a/packages/h5wasm/src/h5wasm-api.test.ts b/packages/h5wasm/src/h5wasm-api.test.ts index a033abdce..4eca198e6 100644 --- a/packages/h5wasm/src/h5wasm-api.test.ts +++ b/packages/h5wasm/src/h5wasm-api.test.ts @@ -5,21 +5,28 @@ import path from 'node:path'; import { getValueOrError } from '@h5web/app'; import { assertDataset, + assertEnvVar, assertGroup, assertGroupWithChildren, hasNonNullShape, } from '@h5web/shared/guards'; -import { expect, test } from 'vitest'; +import { beforeAll, expect, test } from 'vitest'; import { H5WasmApi } from './h5wasm-api'; -const TEST_FILE = path.resolve(process.cwd(), 'support/sample/dist/sample.h5'); +const SKIP = import.meta.env.VITEST_H5WASM_SKIP === 'true'; +const H5WASM_TEST_FILE = import.meta.env.VITEST_H5WASM_TEST_FILE; +assertEnvVar(H5WASM_TEST_FILE, 'VITEST_H5WASM_TEST_FILE'); -test('test file matches snapshot', async () => { +const TEST_FILE = path.resolve(process.cwd(), H5WASM_TEST_FILE); + +beforeAll(() => { if (!existsSync(TEST_FILE)) { throw new Error("Sample file doesn't exist"); } +}); +test.skipIf(SKIP)('test file matches snapshot', async () => { const buffer = await readFile(TEST_FILE); const api = new H5WasmApi('sample.h5', buffer); diff --git a/packages/h5wasm/vite.config.js b/packages/h5wasm/vite.config.js index 023e3a3b1..1387dc13f 100644 --- a/packages/h5wasm/vite.config.js +++ b/packages/h5wasm/vite.config.js @@ -1,6 +1,7 @@ import react from '@vitejs/plugin-react-swc'; import fs from 'fs'; import path from 'path'; +import { loadEnv } from 'vite'; import { defineProject } from 'vitest/config'; const [pkg, appPkg, sharedPkg] = ['.', '../app', '../shared'] @@ -29,6 +30,7 @@ export default defineProject({ sourcemap: true, }, test: { + env: loadEnv('test', import.meta.dirname, 'VITEST_'), server: { deps: { inline: ['react-suspense-fetch'] }, },