Skip to content

Commit

Permalink
Move node tests to serverside and enable vitest (#1214)
Browse files Browse the repository at this point in the history
* Move node tests to serverside and enable vitest
  • Loading branch information
thekevinscott authored Oct 12, 2023
1 parent 43a1344 commit 628cc2b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 55 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ jobs:
env:
NODE_OPTIONS: --max_old_space_size=8192

integration-node-local:
name: 'Integration / Node / Local'
integration-serverside-local:
name: 'Integration / Serverside'
runs-on: ubuntu-latest
steps:
- name: 'Checkout repository'
Expand All @@ -219,10 +219,10 @@ jobs:
node-version: 16

- name: 'Bundle'
run: pnpm test:integration:node -- --ci --verbose --skip-test
run: pnpm test:integration:serverside -- --ci --verbose --skip-test

- name: 'Integration Tests'
run: pnpm test:integration:node -- --ci --verbose --skip-bundle
run: pnpm test:integration:serverside -- --ci --verbose --skip-bundle

integration-models-browser:
name: 'Models / Browser'
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"test:start-test-server": "wireit",
"test:integration:browserstack": "wireit",
"test:integration:browser": "wireit",
"test:integration:node": "wireit",
"test:integration:serverside": "wireit",
"test:model": "wireit",
"test:memory-leaks": "wireit",
"test:unit:browser:playwright": "wireit",
Expand Down Expand Up @@ -291,8 +291,8 @@
"build:models:umd"
]
},
"test:integration:node": {
"command": "pnpm --filter @upscalerjs/scripts test:integration:node",
"test:integration:serverside": {
"command": "pnpm --filter @upscalerjs/scripts test:integration:serverside",
"dependencies": [
"./internals/common:build",
"./internals/test-runner:build",
Expand Down
2 changes: 1 addition & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"model:write-docs": "pnpm __run_command ./package-scripts/write-model-docs.ts",
"test:integration:browserstack": "pnpm __run_command ./test.ts --kind integration --platform browser --runner browserstack",
"test:integration:browser": "pnpm __run_command ./test.ts --kind integration --platform browser",
"test:integration:node": "pnpm __run_command ./test.ts --kind integration --platform node",
"test:integration:serverside": "pnpm __run_command ./test.ts --kind integration --platform node",
"test:memory-leaks": "pnpm __run_command ./test.ts --kind memory --platform browser",
"test:model": "pnpm __run_command ./test.ts --kind model",
"update:version": "pnpm __run_command ./package-scripts/update-version.ts",
Expand Down
40 changes: 27 additions & 13 deletions scripts/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ROOT_BUNDLER_OUTPUT_DIR = path.resolve(ROOT_DIR, 'tmp/bundlers');
*/
type Platform = 'browser' | 'node';
type Runner = 'local' | 'browserstack';
type Kind = 'integraticn' | 'memory' | 'model';
type Kind = 'integration' | 'memory' | 'model';

/****
* Utility Functions & Classes
Expand Down Expand Up @@ -52,11 +52,16 @@ const getAllTestFiles = (platform: Platform, runner: Runner, kind: Kind): string
if (runner === 'browserstack') {
const globPath = path.resolve(TEST_DIR, 'integration/browserstack/tests/**/*.mts');
const files: string[] = sync(globPath);
return files.map(file => file.split('/').pop() || '').filter(file => file !== 'vitest.config.ts');
return files.map(file => file.split('/').pop() || '').filter(file => file !== 'vite.config.ts');
}
if (platform === 'node') {
const globPath = path.resolve(TEST_DIR, 'integration/serverside/tests/**/*.mts');
const files: string[] = sync(globPath);
return files.map(file => file.split('/').pop() || '').filter(file => file !== 'vite.config.mts');
}
const globPath = path.resolve(TEST_DIR, 'integration', getFolder(platform, runner, kind), `**/*.ts`);
const files: string[] = sync(globPath);
return files.map(file => file.split('/').pop() || '').filter(file => file !== 'vitest.config.ts');
return files.map(file => file.split('/').pop() || '').filter(file => file !== 'vite.config.ts');
};

const getDependencies = async (_platforms: Platform | Platform[], runner: Runner, kind: Kind, ...specificFiles: (number | string)[]): Promise<Bundle[]> => {
Expand All @@ -73,7 +78,7 @@ const getDependencies = async (_platforms: Platform | Platform[], runner: Runner
filesForPlatforms.push({
platform,
files,
})
});

for (const file of files) {
const fileName = `${file}`.split('.').slice(0, -1).join('.');
Expand Down Expand Up @@ -145,15 +150,24 @@ const test = async (platform: Platform | Platform[], runner: Runner, kind: Kind,

if (skipTest !== true) {
const jestConfigPath = getJestConfigPath(platform, runner, kind);
const args = runner === 'browserstack' ? ['pnpm', 'vitest', '-c', path.resolve(ROOT_DIR, './test/integration/browserstack/vite.config.mts')] : [
'pnpm',
'jest',
'--config',
jestConfigPath,
'--detectOpenHandles',
watch ? '--watch' : undefined,
...positionalArgs,
].filter(Boolean).map(arg => `${arg}`);
const getArgs = () => {
if (runner === 'browserstack') {
return ['pnpm', 'vitest', '-c', path.resolve(ROOT_DIR, './test/integration/browserstack/vite.config.mts')];
}
if (kind === 'integration' && platform === 'node') {
return ['pnpm', 'vitest', '-c', path.resolve(ROOT_DIR, './test/integration/serverside/vite.config.mts')];
}
return [
'pnpm',
'jest',
'--config',
jestConfigPath,
'--detectOpenHandles',
watch ? '--watch' : undefined,
...positionalArgs,
];
};
const args = getArgs().filter(Boolean).map(arg => `${arg}`);

if (verbose) {
console.log(args.join(' '));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'path';
import fs from 'fs';
import * as tf from '@tensorflow/tfjs-node';
import { checkImage } from '../../lib/utils/checkImage';
import { LOCAL_UPSCALER_NAME } from '../../lib/node/constants';
import { Main, NodeTestRunner } from '../utils/NodeTestRunner';
import { MODELS_DIR } from '../../../scripts/package-scripts/utils/constants';
import { checkImage } from '../../../lib/utils/checkImage.js';
import { LOCAL_UPSCALER_NAME } from '../../../lib/node/constants.js';
import { Main, NodeTestRunner } from '../../utils/NodeTestRunner.js';
import { MODELS_DIR } from '../../../../scripts/package-scripts/utils/constants.js';

const MODEL_PATH = 'file://' + path.join(MODELS_DIR, 'pixel-upsampler/models/x4/x4.json');

Expand All @@ -26,9 +26,6 @@ export const getInvalidChannelsOfTensor = (input: tf.Tensor): Error => new Error
`Full tensor shape: ${JSON.stringify(input.shape)}`,
].join(' '));

const JEST_TIMEOUT = 60 * 1000;
jest.setTimeout(JEST_TIMEOUT * 1); // 60 seconds timeout

const main: Main = async (deps) => {
const {
Upscaler,
Expand Down Expand Up @@ -65,7 +62,7 @@ describe('Node Image Loading Integration Tests', () => {
'tf': `@tensorflow/tfjs-node`,
'Upscaler': `${LOCAL_UPSCALER_NAME}/node`,
'fs': 'fs',
'base64ArrayBuffer': path.resolve(__dirname, '../../lib/utils/base64ArrayBuffer'),
'base64ArrayBuffer': path.resolve(__dirname, '../../../lib/utils/base64ArrayBuffer'),
'flower_tensor': path.resolve(MODELS_DIR, 'pixel-upsampler/test/__fixtures__', 'flower-small-tensor.json'),
},
globals: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import path from 'path';
import * as _tf from '@tensorflow/tfjs-node';
import { checkImage } from '../../lib/utils/checkImage';
import { LOCAL_UPSCALER_NAME } from '../../lib/node/constants';
import { Main, NodeTestRunner } from '../utils/NodeTestRunner';
import { MODELS_DIR, TMP_DIR } from '../../../scripts/package-scripts/utils/constants';
import { checkImage } from '../../../lib/utils/checkImage.js';
import { LOCAL_UPSCALER_NAME } from '../../../lib/node/constants.js';
import { Main, NodeTestRunner } from '../../utils/NodeTestRunner.js';
import { MODELS_DIR, TMP_DIR } from '../../../../scripts/package-scripts/utils/constants.js';
import {
MultiArgTensorProgress,
} from '../../../packages/upscalerjs/src/shared/types';
} from '../../../../packages/upscalerjs/src/shared/types.js';
import type {
ModelDefinition,
} from '../../../packages/shared/src/types';
} from '../../../../packages/shared/src/types.js';
import {
WARNING_PROGRESS_WITHOUT_PATCH_SIZE,
WARNING_UNDEFINED_PADDING,
WARNING_INPUT_SIZE_AND_PATCH_SIZE,
GET_WARNING_PATCH_SIZE_INDIVISIBLE_BY_DIVISIBILITY_FACTOR,
} from '../../../packages/upscalerjs/src/shared/errors-and-warnings';
} from '../../../../packages/upscalerjs/src/shared/errors-and-warnings.js';

const PIXEL_UPSAMPLER_DIR = path.resolve(MODELS_DIR, 'pixel-upsampler/test/__fixtures__');
const DEFAULT_MODEL_DIR = path.resolve(MODELS_DIR, 'default-model/test/__fixtures__');

const JEST_TIMEOUT = 60 * 1000 * 5;
jest.setTimeout(JEST_TIMEOUT); // 5 minute timeout

const makeModelAndWeights = (scale: number, batchInputShape: (null | number)[]) => {
if (scale < 2 || scale > 4) {
throw new Error('Scale must be between 2 and 4');
Expand Down Expand Up @@ -110,7 +107,7 @@ describe('Node Model Loading Integration Tests', () => {
'tf': '@tensorflow/tfjs-node',
'Upscaler': `${LOCAL_UPSCALER_NAME}/node`,
'fs': 'fs',
'base64ArrayBuffer': path.resolve(__dirname, '../../lib/utils/base64ArrayBuffer'),
'base64ArrayBuffer': path.resolve(__dirname, '../../../lib/utils/base64ArrayBuffer'),
},
});

Expand All @@ -136,7 +133,7 @@ describe('Node Model Loading Integration Tests', () => {
},
globals: {
model: JSON.stringify({
path: 'file://' + path.join(__dirname, '../../../models/pixel-upsampler/models/x4/x4.json'),
path: 'file://' + path.join(__dirname, '../../../../models/pixel-upsampler/models/x4/x4.json'),
scale: 4,
}),
imagePath: JSON.stringify(fixturePath),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import path from 'path';
import { checkImage } from '../../lib/utils/checkImage';
import { LOCAL_UPSCALER_NAME } from '../../lib/node/constants';
import { Main, NodeTestRunner } from '../utils/NodeTestRunner';
import { MODELS_DIR } from '../../../scripts/package-scripts/utils/constants';
import { checkImage } from '../../../lib/utils/checkImage.js';
import { LOCAL_UPSCALER_NAME } from '../../../lib/node/constants.js';
import { Main, NodeTestRunner } from '../../utils/NodeTestRunner.js';
import { MODELS_DIR } from '../../../../scripts/package-scripts/utils/constants.js';

const PIXEL_UPSAMPLER_DIR = path.resolve(MODELS_DIR, 'pixel-upsampler/test/__fixtures__');

const JEST_TIMEOUT = 60 * 1000;
jest.setTimeout(JEST_TIMEOUT * 1); // 60 seconds timeout

const main: Main = async (deps) => {
const {
Upscaler,
Expand Down Expand Up @@ -42,12 +39,12 @@ describe('Node Platforms Integration Tests', () => {
trackTime: false,
dependencies: {
'fs': 'fs',
'base64ArrayBuffer': path.resolve(__dirname, '../../lib/utils/base64ArrayBuffer'),
'base64ArrayBuffer': path.resolve(__dirname, '../../../lib/utils/base64ArrayBuffer'),
'flower': path.resolve(PIXEL_UPSAMPLER_DIR, 'flower-small-tensor.json'),
},
globals: {
model: JSON.stringify({
path: 'file://' + path.join(__dirname, '../../../models/pixel-upsampler/models/x4/x4.json'),
path: 'file://' + path.join(__dirname, '../../../../models/pixel-upsampler/models/x4/x4.json'),
scale: 4,
}),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import path from 'path';
import { LOCAL_UPSCALER_NAME, LOCAL_UPSCALER_NAMESPACE } from '../../lib/node/constants';
import { Main, NodeTestRunner } from '../utils/NodeTestRunner';
import { MODELS_DIR } from '../../../scripts/package-scripts/utils/constants';
import { LOCAL_UPSCALER_NAME, LOCAL_UPSCALER_NAMESPACE } from '../../../lib/node/constants.js';
import { Main, NodeTestRunner } from '../../utils/NodeTestRunner.js';
import { MODELS_DIR } from '../../../../scripts/package-scripts/utils/constants.js';

const JEST_TIMEOUT = 60 * 1000;
jest.setTimeout(JEST_TIMEOUT * 1); // 60 seconds timeout
const LOWER_THRESHOLD = 20; // in milliseconds
const UPPER_THRESHOLD = 10; // in milliseconds
const DATE_AT_WHICH_SPEED_TESTS_TAKE_EFFECT = new Date('December 1, 2023 00:00:00');
Expand Down
8 changes: 8 additions & 0 deletions test/integration/serverside/vite.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig, mergeConfig } from 'vitest/config';
import viteConfig from '../../vite.config.mjs';

export default mergeConfig(viteConfig, defineConfig({
test: {
root: __dirname,
},
}))

0 comments on commit 628cc2b

Please sign in to comment.