Skip to content

Commit

Permalink
New internal testing utility: withErrorSpy(it, "should...", ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jun 22, 2021
1 parent d7edb88 commit d11ea15
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ Array [
"mockSingleLink",
"stripSymbols",
"subscribeAndCount",
"withErrorSpy",
]
`;

Expand Down
1 change: 1 addition & 0 deletions src/utilities/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export { createMockClient } from './mocking/mockClient';
export { stripSymbols } from './stripSymbols';
export { default as subscribeAndCount } from './subscribeAndCount';
export { itAsync } from './itAsync';
export { withErrorSpy } from './withErrorSpy';
17 changes: 9 additions & 8 deletions src/utilities/testing/itAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ function wrap<TResult>(key?: "only" | "skip" | "todo") {
}

const wrappedIt = wrap();
export function itAsync(...args: Parameters<typeof wrappedIt>) {
return wrappedIt.apply(this, args);
}

export namespace itAsync {
export const only = wrap("only");
export const skip = wrap("skip");
export const todo = wrap("todo");
}
export const itAsync = Object.assign(function (
...args: Parameters<typeof wrappedIt>
) {
return wrappedIt.apply(this, args);
}, {
only: wrap("only"),
skip: wrap("skip"),
todo: wrap("todo"),
});
21 changes: 21 additions & 0 deletions src/utilities/testing/withErrorSpy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function withErrorSpy<
TArgs extends any[],
TResult,
>(
it: (...args: TArgs) => TResult,
...args: TArgs
) {
const fn = args[1];
args[1] = function () {
const args = arguments;
const errorSpy = jest.spyOn(console, 'error');
errorSpy.mockImplementation(() => {});
return new Promise(resolve => {
resolve(fn?.apply(this, args));
}).finally(() => {
expect(errorSpy).toMatchSnapshot();
errorSpy.mockReset();
});
};
return it(...args);
}

0 comments on commit d11ea15

Please sign in to comment.