Skip to content

fix(tests): Fix type errors in tests #4908

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

Merged
merged 10 commits into from
Apr 14, 2022
Merged
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
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports = {
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.test.json',
diagnostics: false,
},
},
testPathIgnorePatterns: ['<rootDir>/build/', '<rootDir>/node_modules/'],
Expand Down
14 changes: 10 additions & 4 deletions packages/browser/test/unit/integrations/linkederrors.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtendedError } from '@sentry/types';
import { Event as SentryEvent, Exception, ExtendedError } from '@sentry/types';
import { createStackParser } from '@sentry/utils';

import { BrowserClient } from '../../../src/client';
Expand All @@ -8,6 +8,12 @@ import { setupBrowserTransport } from '../../../src/transports';

const parser = createStackParser(...defaultStackParsers);

type EventWithException = SentryEvent & {
exception: {
values: Exception[];
};
};

describe('LinkedErrors', () => {
describe('handler', () => {
it('should bail out if event does not contain exception', () => {
Expand Down Expand Up @@ -44,7 +50,7 @@ describe('LinkedErrors', () => {
return client.eventFromException(originalException).then(event => {
const result = LinkedErrorsModule._handler(parser, 'cause', 5, event, {
originalException,
});
}) as EventWithException;

// It shouldn't include root exception, as it's already processed in the event by the main error handler
expect(result.exception.values.length).toBe(3);
Expand Down Expand Up @@ -75,7 +81,7 @@ describe('LinkedErrors', () => {
return client.eventFromException(originalException).then(event => {
const result = LinkedErrorsModule._handler(parser, 'reason', 5, event, {
originalException,
});
}) as EventWithException;

expect(result.exception.values.length).toBe(3);
expect(result.exception.values[0].type).toBe('SyntaxError');
Expand Down Expand Up @@ -103,7 +109,7 @@ describe('LinkedErrors', () => {
return client.eventFromException(originalException).then(event => {
const result = LinkedErrorsModule._handler(parser, 'cause', 2, event, {
originalException,
});
}) as EventWithException;

expect(result.exception.values.length).toBe(2);
expect(result.exception.values[0].type).toBe('TypeError');
Expand Down
1 change: 1 addition & 0 deletions packages/browser/test/unit/mocks/simpletransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { eventStatusFromHttpCode, resolvedSyncPromise } from '@sentry/utils';
import { Event, Response } from '../../../src';
import { BaseTransport } from '../../../src/transports';

// @ts-ignore It's okay that we're not implementing the `_sendRequest()` method because we don't use it in our tests
export class SimpleTransport extends BaseTransport {
public sendEvent(_: Event): PromiseLike<Response> {
return this._buffer.add(() =>
Expand Down
6 changes: 4 additions & 2 deletions packages/browser/test/unit/transports/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { BaseTransport } from '../../../src/transports/base';
const testDsn = 'https://[email protected]/42';
const envelopeEndpoint = 'https://sentry.io/api/42/envelope/?sentry_key=123&sentry_version=7';

// @ts-ignore We're purposely not implementing the methods of the abstract `BaseTransport` class in order to be able to
// assert on what the class provides and what it leaves to the concrete class to implement
class SimpleTransport extends BaseTransport {}

describe('BaseTransport', () => {
Expand Down Expand Up @@ -111,12 +113,12 @@ describe('BaseTransport', () => {
});
});

it('doesnt provide sendEvent() implementation', () => {
it('doesnt provide sendEvent() implementation', async () => {
expect.assertions(1);
const transport = new SimpleTransport({ dsn: testDsn });

try {
void transport.sendEvent({});
await transport.sendEvent({});
} catch (e) {
expect(e).toBeDefined();
}
Expand Down
10 changes: 5 additions & 5 deletions packages/browser/test/unit/transports/new-xhr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function createXHRMock() {
case 'Retry-After':
return '10';
case `${retryAfterSeconds}`:
return;
return null;
default:
return `${retryAfterSeconds}:error:scope`;
}
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('NewXHRTransport', () => {
expect(xhrMock.setRequestHeader).toHaveBeenCalledTimes(0);
expect(xhrMock.send).toHaveBeenCalledTimes(0);

await Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange(null)]);
await Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange!({} as Event)]);

expect(xhrMock.open).toHaveBeenCalledTimes(1);
expect(xhrMock.open).toHaveBeenCalledWith('POST', DEFAULT_XHR_TRANSPORT_OPTIONS.url);
Expand All @@ -70,7 +70,7 @@ describe('NewXHRTransport', () => {

const [res] = await Promise.all([
transport.send(ERROR_ENVELOPE),
(xhrMock as XMLHttpRequest).onreadystatechange(null),
(xhrMock as XMLHttpRequest).onreadystatechange!({} as Event),
]);

expect(res).toBeDefined();
Expand All @@ -80,7 +80,7 @@ describe('NewXHRTransport', () => {
it('sets rate limit response headers', async () => {
const transport = makeNewXHRTransport(DEFAULT_XHR_TRANSPORT_OPTIONS);

await Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange(null)]);
await Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange!({} as Event)]);

expect(xhrMock.getResponseHeader).toHaveBeenCalledTimes(2);
expect(xhrMock.getResponseHeader).toHaveBeenCalledWith('X-Sentry-Rate-Limits');
Expand All @@ -99,7 +99,7 @@ describe('NewXHRTransport', () => {
};

const transport = makeNewXHRTransport(options);
await Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange(null)]);
await Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange!({} as Event)]);

expect(xhrMock.setRequestHeader).toHaveBeenCalledTimes(3);
expect(xhrMock.setRequestHeader).toHaveBeenCalledWith('referrerPolicy', headers.referrerPolicy);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/lib/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('BaseClient', () => {

const options = { dsn: PUBLIC_DSN };
const client = new TestClient(options, setupTestTransport(options).transport);
expect(dsnToString(client.getDsn())).toBe(PUBLIC_DSN);
expect(dsnToString(client.getDsn()!)).toBe(PUBLIC_DSN);
});

test('allows missing Dsn', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/mocks/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function setupTestTransport(options: TestOptions): { transport: Transport
const transportOptions = options.transportOptions ? options.transportOptions : { dsn: options.dsn };

if (options.transport) {
return { transport: new this._options.transport(transportOptions) };
return { transport: new options.transport(transportOptions) };
}

return noop;
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/test/gatsby-browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-explicit-any */

const { onClientEntry } = require('../gatsby-browser');
import { onClientEntry } from '../gatsby-browser';

(global as any).__SENTRY_RELEASE__ = '683f3a6ab819d47d23abfca9a914c81f0524d35b';
(global as any).__SENTRY_DSN__ = 'https://[email protected]/0';
Expand Down Expand Up @@ -153,7 +153,7 @@ describe('onClientEntry', () => {

// Run this last to check for any test side effects
it('does not run if plugin params are undefined', () => {
onClientEntry();
onClientEntry(undefined, undefined);
expect(sentryInit).toHaveBeenCalledTimes(0);
expect(tracingAddExtensionMethods).toHaveBeenCalledTimes(0);
});
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby/test/gatsby-node.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-explicit-any */
const { onCreateWebpackConfig } = require('../gatsby-node');
import { onCreateWebpackConfig } from '../gatsby-node';

describe('onCreateWebpackConfig', () => {
it('sets a webpack config', () => {
Expand All @@ -12,7 +12,9 @@ describe('onCreateWebpackConfig', () => {
setWebpackConfig: jest.fn(),
};

onCreateWebpackConfig({ plugins, actions });
const getConfig = jest.fn();

onCreateWebpackConfig({ plugins, actions, getConfig });

expect(plugins.define).toHaveBeenCalledTimes(1);
expect(plugins.define).toHaveBeenLastCalledWith({
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/test/setEnvVars.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
process.env.SENTRY_RELEASE = '14abbb1678a2eb59d1a171ea33d630dd6c6eee70';

// This file needs to have an import or an export to count as a module, which is necessary when using
// the `isolatedModules` tsconfig option.
export const _ = '';
4 changes: 4 additions & 0 deletions packages/hub/test/global.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { getGlobalObject } from '@sentry/utils';

import { getCurrentHub, getHubFromCarrier, Hub } from '../src';

const global = getGlobalObject();

describe('global', () => {
test('getGlobalHub', () => {
expect(getCurrentHub()).toBeTruthy();
Expand Down
Loading