Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update error JSON handling #3654

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
import 'jest-extended'

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface AsymmetricMatchers {
toThrowWith(cb: (error: Error) => void): void;
}
interface Matchers<R> {
toThrowWith(cb: (error: Error) => void): R;
}
}
}

declare module "expect" {
interface AsymmetricMatchers {
toThrowWith(cb: (error: Error) => void): void;
}
interface Matchers<R> {
toThrowWith(cb: (error: Error) => void): R;
}
}
6 changes: 4 additions & 2 deletions jest.config.base.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
rootDir: '../../',
testEnvironment: 'node',
testRegex: 'test\\/.*\\.ts$',
coverageDirectory: 'coverage',
Expand All @@ -9,10 +10,11 @@ export default {
['jest-junit', { outputDirectory: 'reports', outputName: 'nodejs_junit.xml' }],
],
moduleNameMapper: {
'^@gitbeaker/(.*)$': '<rootDir>/../$1/src',
'^@gitbeaker/core/map.json': '<rootDir>/packages/core/dist/map.json',
'^@gitbeaker/(.*)$': '<rootDir>/packages/$1/src',
},
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
setupFilesAfterEnv: ['jest-extended/all'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
};
75 changes: 75 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@
import 'jest-extended';
import 'jest-extended/all';

import { expect } from '@jest/globals';
import type {MatcherFunction} from 'expect';

const toThrowWith: MatcherFunction<[cb:unknown]> = async function (expectCallbackOrPromiseReturn, matcherCallback) {
const isFromReject = this && this.promise === 'rejects'; // See https://github.com/facebook/jest/pull/7621#issue-244312550
if ((!expectCallbackOrPromiseReturn || typeof expectCallbackOrPromiseReturn !== 'function') && !isFromReject) {
return {
pass: false,
message: () =>
`Received value must be a function but instead "${expectCallbackOrPromiseReturn}" was found`,
};
}

if ((!matcherCallback || typeof matcherCallback !== 'function')) {
return {
pass: false,
message: () =>
`matcherCallback value must be a function but instead "${matcherCallback}" was found`,
};
}


let error;
if (isFromReject) {
error = expectCallbackOrPromiseReturn;
} else {
try {
if (typeof expectCallbackOrPromiseReturn === 'function') {
await expectCallbackOrPromiseReturn();
}
} catch (e) {
error = e;
}
}

await matcherCallback(error)

if (!error) {
return {
pass: false,
message: () => 'Expected the function to throw an error.\n' + "But it didn't throw anything.",
};
} else {
return {
pass: true,
message: () => 'Expected the function not to throw an error"
};
}
}

expect.extend({
toThrowWith,
});

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface AsymmetricMatchers {
toThrowWith(cb: (error: Error) => void): void;
}
interface Matchers<R> {
toThrowWith(cb: (error: Error) => void): R;
}
}
}

declare module "expect" {
interface AsymmetricMatchers {
toThrowWith(cb: (error: Error) => void): void;
}
interface Matchers<R> {
toThrowWith(cb: (error: Error) => void): R;
}
}
4 changes: 3 additions & 1 deletion packages/rest/src/Requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ async function throwFailedRequestError(

if (contentType?.includes('application/json')) {
const output = JSON.parse(content);
const contentProperty = output.error || output.message;

description = output.message;
description =
typeof contentProperty === 'string' ? contentProperty : JSON.stringify(contentProperty);
} else {
description = content;
}
Expand Down
Loading