Skip to content

🎥 Make videos programmatically with React

improve tests to better detect errors

Calvin Falter1c88867d3a44
diff --git a/packages/core/src/test/measure-spring.test.ts b/packages/core/src/test/measure-spring.test.ts
index 0bb9dba819..edf1f29c2c 100644
--- a/packages/core/src/test/measure-spring.test.ts
+++ b/packages/core/src/test/measure-spring.test.ts
@@ -1,23 +1,25 @@
import {measureSpring, spring} from '../spring';
import {expectToThrow} from './expect-to-throw';
-test('Measure spring should work', () => {
+describe('Measure spring should work', () => {
const duration = measureSpring({
fps: 30,
});
- expect(duration).toBe(28);
- expect(
- spring({
- fps: 30,
- frame: duration,
- })
- ).toBeCloseTo(1);
- expect(
- spring({
- fps: 30,
- frame: duration - 1,
- })
- ).not.toBe(1);
+ test('test measureSpring()', () => expect(duration).toBe(28));
+ test('spring should be close to 1', () =>
+ expect(
+ spring({
+ fps: 30,
+ frame: duration,
+ })
+ ).toBeCloseTo(1));
+ test('spring should not be 1', () =>
+ expect(
+ spring({
+ fps: 30,
+ frame: duration - 1,
+ })
+ ).not.toBe(1));
});
test('Higher threshold should lead to faster spring', () => {
@@ -32,22 +34,30 @@ test('Lower threshold should lead to slower spring', () => {
);
});
-test('Threshold edge cases', () => {
- expect(measureSpring({fps: 30, threshold: 0})).toBe(Infinity);
- expect(measureSpring({fps: 30, threshold: 1})).toBe(0);
-
- expectToThrow(
- () => measureSpring({fps: 30, threshold: NaN}),
- /Threshold is NaN/
- );
- expectToThrow(
- () => measureSpring({fps: 30, threshold: Infinity}),
- /Threshold is not finite/
+describe('Threshold edge cases', () => {
+ // threshold, expected
+ const validEdgeCases: [number, number][] = [
+ [0, Infinity],
+ [1, 0],
+ ];
+ validEdgeCases.forEach((entry) =>
+ test('', () =>
+ expect(measureSpring({fps: 30, threshold: entry[0]})).toBe(entry[1]))
);
- expectToThrow(
- // @ts-expect-error
- () => measureSpring({fps: 30, threshold: null}),
- /threshold must be a number, got null of type object/
+
+ // threshold, errMsg
+ const errorEdgeCases: [number | null, RegExp][] = [
+ [NaN, /Threshold is NaN/],
+ [Infinity, /Threshold is not finite/],
+ [null, /threshold must be a number, got null of type object/],
+ ];
+
+ errorEdgeCases.forEach((entry) =>
+ expectToThrow(
+ // @ts-expect-error
+ () => measureSpring({fps: 30, threshold: entry[0]}),
+ entry[1]
+ )
);
});
diff --git a/packages/core/src/test/random.test.ts b/packages/core/src/test/random.test.ts
index cbce2d0116..c842fe31e4 100644
--- a/packages/core/src/test/random.test.ts
+++ b/packages/core/src/test/random.test.ts
@@ -1,9 +1,10 @@
import {random} from '../random';
-test('Should support negative random numbers', () => {
- expect(random(-1)).toBe(random(-1));
- expect(random(-2)).toBe(random(-2));
- expect(random(-2)).not.toBe(random(-1));
+describe('Should support negative random numbers', () => {
+ test('test with -1', () => expect(random(-1)).toBe(random(-1)));
+ test('test with -2', () => expect(random(-2)).toBe(random(-2)));
+ test('different seeds should be different randoms', () =>
+ expect(random(-2)).not.toBe(random(-1)));
});
test('Random should be deterministic', () => {
@@ -13,7 +14,7 @@ test('Random should be deterministic', () => {
expect(random(null)).not.toBe(random(null));
});
-test('Random should be uniform', () => {
+describe('Random should be uniform', () => {
const ITEM_COUNT = 100000;
const mapped = new Array(ITEM_COUNT).fill(true).map((a, i) => {
return random(i);
@@ -21,29 +22,50 @@ test('Random should be uniform', () => {
const average = mapped.reduce((a, b) => a + b, 0) / mapped.length;
- const tenthPercentile = mapped.filter((m) => m <= 0.1);
- const twentiethPercentile = mapped.filter((m) => m <= 0.2 && m >= 0.1);
- const ninetiethPercentile = mapped.filter((m) => m <= 0.9 && m >= 0.8);
- expect(average).toBeLessThan(0.51);
- expect(average).toBeGreaterThan(0.49);
- expect(tenthPercentile.length).toBeLessThan(ITEM_COUNT * 0.105);
- expect(tenthPercentile.length).toBeGreaterThan(ITEM_COUNT * 0.095);
- expect(twentiethPercentile.length).toBeLessThan(ITEM_COUNT * 0.105);
- expect(twentiethPercentile.length).toBeGreaterThan(ITEM_COUNT * 0.095);
- expect(ninetiethPercentile.length).toBeLessThan(ITEM_COUNT * 0.105);
- expect(ninetiethPercentile.length).toBeGreaterThan(ITEM_COUNT * 0.095);
-
- const distances = mapped
- .map((a, i) => {
- if (i === 0) {
- return null;
- }
- return Math.abs(mapped[i] - mapped[i - 1]);
+ test('test if average is around 0.5', () => {
+ expect(average).toBeLessThan(0.51);
+ expect(average).toBeGreaterThan(0.49);
+ });
+
+ const tenPercentSections: number[][] = [];
+ const stepInterval = 0.1;
+ let currentStep = 0;
+ while (currentStep < 0.999999) {
+ console.log(currentStep);
+ const items = mapped.filter(
+ (m) => m >= currentStep && m <= currentStep + stepInterval
+ );
+ tenPercentSections.push(items);
+ currentStep += stepInterval;
+ }
+ const ACCURACY = 0.005;
+ tenPercentSections.forEach((entries, index) =>
+ test(`section ${index} should contain around ${
+ ITEM_COUNT * stepInterval
+ } entries`, () => {
+ expect(entries.length).toBeLessThan(
+ ITEM_COUNT * (stepInterval + ACCURACY)
+ );
+ expect(entries.length).toBeGreaterThan(
+ ITEM_COUNT * (stepInterval - ACCURACY)
+ );
})
- .filter((f) => f !== null) as number[];
- const averageDistance =
- distances.reduce((a, b) => a + b, 0) / distances.length;
- expect(averageDistance).toBeGreaterThan(0.3);
+ );
+
+ test('test for average distance', () => {
+ const distances = mapped
+ .map((a, i) => {
+ if (i === 0) {
+ return null;
+ }
+ return Math.abs(mapped[i] - mapped[i - 1]);
+ })
+ .filter((f) => f !== null) as number[];
+ const averageDistance =
+ distances.reduce((a, b) => a + b, 0) / distances.length;
+
+ expect(averageDistance).toBeGreaterThan(0.3);
+ });
});
test('Random string should be uniform', () => {
diff --git a/packages/core/src/test/range.test.tsx b/packages/core/src/test/range.test.tsx
index 11ac96ae1a..0bdcadbf25 100644
--- a/packages/core/src/test/range.test.tsx
+++ b/packages/core/src/test/range.test.tsx
@@ -2,81 +2,92 @@ import {setFrameRange, setFrameRangeFromCli} from '../config/frame-range';
import {Internals} from '../internals';
import {expectToThrow} from './expect-to-throw';
-test('Frame range should throw exception with invalid inputs', () => {
- expectToThrow(() => setFrameRange(-1), /non-negative/);
- expectToThrow(
- () => setFrameRange(1.111),
- /Frame must be an integer, but got a float \(1.111\)/
- );
- expectToThrow(() => setFrameRange(Infinity), /finite number, got Infinity/);
- expectToThrow(
+describe('Frame range should throw exception with invalid inputs', () => {
+ const testValues: [number | [number, number] | null, RegExp][] = [
+ [-1, /non-negative/],
+ [1.111, /Frame must be an integer, but got a float \(1.111\)/],
+ [Infinity, /finite number, got Infinity/],
// @ts-expect-error
- () => setFrameRange([0, 2, 4]),
- /Frame range must be a tuple, got an array with length 3/
- );
- expectToThrow(
+ [[0, 2, 4], /Frame range must be a tuple, got an array with length 3/],
// @ts-expect-error
- () => setFrameRange([]),
- /Frame range must be a tuple, got an array with length 0/
- );
- expectToThrow(
- // @ts-expect-error
- () => setFrameRange(['0', 2]),
- /Each value of frame range must be a number, but got string \("0"\)/
- );
- expectToThrow(
- () => setFrameRange([0.111, 2]),
- /Each value of frame range must be an integer, but got a float \(0.111\)/
- );
- expectToThrow(
- () => setFrameRange([0, Infinity]),
- /Each value of frame range must be finite, but got Infinity/
- );
- expectToThrow(
- () => setFrameRange([-1, 0]),
- /Each value of frame range must be non-negative, but got -1/
- );
- expectToThrow(
- () => setFrameRange([10, 0]),
- /The second value of frame range must be not smaller than the first one, but got 10-0/
- );
- expectToThrow(
- // @ts-expect-error
- () => setFrameRange('10'),
- /Frame range must be a number or a tuple of numbers, but got object of type string/
+ [[], /Frame range must be a tuple, got an array with length 0/],
+ [
+ // @ts-expect-error
+ ['0', 2],
+ /Each value of frame range must be a number, but got string \("0"\)/,
+ ],
+ [
+ [0.111, 2],
+ /Each value of frame range must be an integer, but got a float \(0.111\)/,
+ ],
+ [
+ [0, Infinity],
+ /Each value of frame range must be finite, but got Infinity/,
+ ],
+ [[-1, 0], /Each value of frame range must be non-negative, but got -1/],
+ [
+ [10, 0],
+ /The second value of frame range must be not smaller than the first one, but got 10-0/,
+ ],
+ [
+ // @ts-expect-error
+ '10',
+ /Frame range must be a number or a tuple of numbers, but got object of type string/,
+ ],
+ ];
+
+ testValues.forEach((entry) =>
+ test(`test with input ${JSON.stringify(entry[0])}`, () =>
+ expectToThrow(() => setFrameRange(entry[0]), entry[1]))
);
});
-test('Frame range tests with valid inputs', () => {
- setFrameRange(null);
- expect(Internals.getRange()).toEqual(null);
- setFrameRange([10, 20]);
- expect(Internals.getRange()).toEqual([10, 20]);
- setFrameRange(10);
- expect(Internals.getRange()).toBe(10);
- setFrameRange(0);
- expect(Internals.getRange()).toBe(0);
+describe('Frame range tests with valid inputs', () => {
+ const testValues: (number | [number, number] | null)[] = [
+ null,
+ [10, 20],
+ 10,
+ 0,
+ ];
+ testValues.forEach((entry) =>
+ test(`test with input ${entry}`, () => {
+ setFrameRange(entry);
+ expect(Internals.getRange()).toEqual(entry);
+ })
+ );
});
-test('Frame range CLI should throw exception with invalid inputs', () => {
- expectToThrow(
- () => setFrameRangeFromCli('1-2-3'),
- /--frames flag must be a number or 2 numbers separated by '-', instead got 3 numbers/
- );
- expectToThrow(
- () => setFrameRangeFromCli('2-1'),
- /The second number of the --frames flag number should be greater or equal than first number/
- );
- expectToThrow(
- () => setFrameRangeFromCli('one-two'),
- /--frames flag must be a single number, or 2 numbers separated by `-`/
+describe('Frame range CLI should throw exception with invalid inputs', () => {
+ const testValues: [string, RegExp][] = [
+ [
+ '1-2-3',
+ /--frames flag must be a number or 2 numbers separated by '-', instead got 3 numbers/,
+ ],
+ [
+ '2-1',
+ /The second number of the --frames flag number should be greater or equal than first number/,
+ ],
+ [
+ 'one-two',
+ /--frames flag must be a single number, or 2 numbers separated by `-`/,
+ ],
+ ];
+ testValues.forEach((entry) =>
+ test(`test with input ${entry[0]}`, () =>
+ expectToThrow(() => setFrameRangeFromCli(entry[0]), entry[1]))
);
});
-test('Frame range CLI tests with valid inputs', () => {
+describe('Frame range CLI tests with valid inputs', () => {
setFrameRange(null);
- setFrameRangeFromCli(0);
- expect(Internals.getRange()).toEqual(0);
- setFrameRangeFromCli(10);
- expect(Internals.getRange()).toEqual(10);
- setFrameRangeFromCli('1-10');
- expect(Internals.getRange()).toEqual([1, 10]);
+
+ const testValues: [number | string, number | [number, number]][] = [
+ [0, 0],
+ [10, 10],
+ ['1-10', [1, 10]],
+ ];
+ testValues.forEach((entry) =>
+ test(`test with input ${entry[0]}`, () => {
+ setFrameRangeFromCli(entry[0]);
+ expect(Internals.getRange()).toEqual(entry[1]);
+ })
+ );
});