Skip to content

Commit

Permalink
Merge pull request #1655 from silx-kit/test-env
Browse files Browse the repository at this point in the history
Improve h5grove/h5wasm API testing set-up
  • Loading branch information
axelboc authored May 29, 2024
2 parents 27cb734 + f64f540 commit 3ad7123
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 7 deletions.
13 changes: 13 additions & 0 deletions packages/app/.env.test
Original file line number Diff line number Diff line change
@@ -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=
17 changes: 13 additions & 4 deletions packages/app/src/providers/h5grove/h5grove-api.test.ts
Original file line number Diff line number Diff line change
@@ -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 },
});
Expand Down
11 changes: 11 additions & 0 deletions packages/app/src/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions packages/app/vite.config.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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: {
Expand Down
10 changes: 10 additions & 0 deletions packages/h5wasm/.env.test
Original file line number Diff line number Diff line change
@@ -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=
13 changes: 10 additions & 3 deletions packages/h5wasm/src/h5wasm-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions packages/h5wasm/vite.config.js
Original file line number Diff line number Diff line change
@@ -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']
Expand Down Expand Up @@ -29,6 +30,7 @@ export default defineProject({
sourcemap: true,
},
test: {
env: loadEnv('test', import.meta.dirname, 'VITEST_'),
server: {
deps: { inline: ['react-suspense-fetch'] },
},
Expand Down

0 comments on commit 3ad7123

Please sign in to comment.