Skip to content

Commit

Permalink
chore: move test error creation to constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Aerilym committed Oct 1, 2024
1 parent 677bc62 commit 6580e65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
10 changes: 6 additions & 4 deletions packages/testing/tests/expectations.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { expectFunctionToCatchErrors, getThrownError } from '../expectations';

const testError = new Error('foo');

describe('getThrownError', () => {
it('should fail an expectation if the given function does not catch its error', async () => {
expect.assertions(1);

const testFunc = () => {
throw new Error('foo');
throw testError;
};

await expect(getThrownError(testFunc)).resolves.toStrictEqual(new Error('foo'));
await expect(getThrownError(testFunc)).resolves.toStrictEqual(testError);
});

it('should do nothing if the given function catches its own error', async () => {
expect.assertions(1);

const testFunc = () => {
try {
throw new Error('foo');
throw testError;
} catch (err) {
// Do nothing
}
Expand All @@ -32,7 +34,7 @@ describe('expectFunctionToCatchErrors', () => {

const testFunc = () => {
try {
throw new Error('foo');
throw testError;
} catch (err) {
// Do nothing
}
Expand Down
20 changes: 11 additions & 9 deletions packages/util-js/tests/try.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { safeTry, safeTrySync } from '../try';
import { expectFunctionToCatchErrors } from '@session/testing/expectations';

const testError = new Error('foo');

describe('safeTry', () => {
it('should return a tuple with the result of the promise', async () => {
expect.assertions(5);
Expand All @@ -17,9 +19,9 @@ describe('safeTry', () => {
it('should return a tuple with an error if the promise rejects', async () => {
expect.assertions(5);

const result = await safeTry(Promise.reject(new Error('foo')));
const result = await safeTry(Promise.reject(testError));

expect(result).toEqual([new Error('foo'), null]);
expect(result).toEqual([testError, null]);
expect(Array.isArray(result)).toBe(true);
expect(result).toHaveLength(2);
expect(result[0]).toBeInstanceOf(Error);
Expand All @@ -43,11 +45,11 @@ describe('safeTry', () => {
expect.assertions(5);

const func = async () => {
throw new Error('foo');
throw testError;
};
const result = await safeTry(func());

expect(result).toEqual([new Error('foo'), null]);
expect(result).toEqual([testError, null]);
expect(Array.isArray(result)).toBe(true);
expect(result).toHaveLength(2);
expect(result[0]).toBeInstanceOf(Error);
Expand All @@ -58,7 +60,7 @@ describe('safeTry', () => {
expect.assertions(1);

const testFunc = async () => {
throw new Error('foo');
throw testError;
};

await expectFunctionToCatchErrors(() => safeTry(testFunc()));
Expand All @@ -82,10 +84,10 @@ describe('safeTrySync', () => {
expect.assertions(5);

const result = safeTrySync(() => {
throw new Error('foo');
throw testError;
});

expect(result).toEqual([new Error('foo'), null]);
expect(result).toEqual([testError, null]);
expect(Array.isArray(result)).toBe(true);
expect(result).toHaveLength(2);
expect(result[0]).toBeInstanceOf(Error);
Expand Down Expand Up @@ -119,14 +121,14 @@ describe('safeTrySync', () => {

// @ts-expect-error - This should be a TS error as the function is asynchronous
const result = safeTrySync(async () => {
throw new Error('foo');
throw testError;
});

expect(Array.isArray(result)).toBe(true);
expect(result).toHaveLength(2);
expect(result[0]).toBeNull();
expect(result[1]).toBeInstanceOf(Promise);
await expect(result[1]).rejects.toThrow(new Error('foo'));
await expect(result[1]).rejects.toThrow(testError);
});
});
});

0 comments on commit 6580e65

Please sign in to comment.