Skip to content

Commit 4c649e2

Browse files
committed
init(react): Flesh out testing and ts
1 parent 48abece commit 4c649e2

14 files changed

+519
-75
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"packages/integrations",
2727
"packages/minimal",
2828
"packages/node",
29-
"package/react",
29+
"packages/react",
3030
"packages/types",
3131
"packages/typescript",
3232
"packages/utils"

packages/react/package.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,32 @@
1616
"access": "public"
1717
},
1818
"dependencies": {
19-
"@sentry/browser": "5.16.0-beta.5"
19+
"@sentry/browser": "5.16.0-beta.5",
20+
"@sentry/types": "5.16.0-beta.5",
21+
"tslib": "^1.9.3"
2022
},
2123
"peerDependencies": {
2224
"react": "^16.0.0"
2325
},
2426
"devDependencies": {
27+
"@testing-library/react": "^10.0.4",
2528
"@types/react": "^16.9.35",
26-
"@sentry/types": "5.16.0-beta.5",
2729
"jest": "^24.7.1",
30+
"npm-run-all": "^4.1.2",
2831
"prettier": "^1.17.0",
2932
"prettier-check": "^2.0.0",
33+
"react": "^16.0.0",
34+
"react-dom": "^16.0.0",
3035
"rimraf": "^2.6.3",
31-
"npm-run-all": "^4.1.2",
3236
"rollup": "^1.10.1",
3337
"rollup-plugin-commonjs": "^9.3.4",
3438
"rollup-plugin-license": "^0.8.1",
3539
"rollup-plugin-node-resolve": "^4.2.3",
3640
"rollup-plugin-terser": "^4.0.4",
3741
"rollup-plugin-typescript2": "^0.21.0",
3842
"tslint": "^5.16.0",
39-
"typescript": "^3.4.5"
43+
"tslint-react": "^5.0.0",
44+
"typescript": "^3.5.1"
4045
},
4146
"scripts": {
4247
"build": "run-p build:es5 build:esm build:bundle",
@@ -62,15 +67,18 @@
6267
"jest": {
6368
"collectCoverage": true,
6469
"transform": {
65-
"^.+\\.ts$": "ts-jest"
70+
"^.+\\.ts$": "ts-jest",
71+
"^.+\\.tsx$": "ts-jest"
6672
},
6773
"moduleFileExtensions": [
6874
"js",
69-
"ts"
75+
"ts",
76+
"tsx"
7077
],
71-
"testEnvironment": "node",
78+
"testEnvironment": "jsdom",
7279
"testMatch": [
73-
"**/*.test.ts"
80+
"**/*.test.ts",
81+
"**/*.test.tsx"
7482
],
7583
"globals": {
7684
"ts-jest": {

packages/react/rollup.config.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { terser } from 'rollup-plugin-terser';
2+
import typescript from 'rollup-plugin-typescript2';
3+
import license from 'rollup-plugin-license';
4+
import resolve from 'rollup-plugin-node-resolve';
5+
import commonjs from 'rollup-plugin-commonjs';
6+
7+
const commitHash = require('child_process')
8+
.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' })
9+
.trim();
10+
11+
const terserInstance = terser({
12+
mangle: {
13+
// captureExceptions and captureMessage are public API methods and they don't need to be listed here
14+
// as mangler doesn't touch user-facing thing, however sentryWrapped is not, and it would be mangled into a minified version.
15+
// We need those full names to correctly detect our internal frames for stripping.
16+
// I listed all of them here just for the clarity sake, as they are all used in the frames manipulation process.
17+
reserved: ['captureException', 'captureMessage', 'sentryWrapped'],
18+
properties: {
19+
regex: /^_[^_]/,
20+
},
21+
},
22+
});
23+
24+
const paths = {
25+
'@sentry/utils': ['../utils/src'],
26+
'@sentry/types': ['../types/src'],
27+
'@sentry/browser': ['../browser/src'],
28+
};
29+
30+
const plugins = [
31+
typescript({
32+
tsconfig: 'tsconfig.build.json',
33+
tsconfigOverride: {
34+
compilerOptions: {
35+
declaration: false,
36+
declarationMap: false,
37+
module: 'ES2015',
38+
paths,
39+
},
40+
},
41+
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
42+
}),
43+
resolve({
44+
mainFields: ['module'],
45+
}),
46+
commonjs(),
47+
];
48+
49+
const bundleConfig = {
50+
input: 'src/index.ts',
51+
output: {
52+
format: 'iife',
53+
name: 'Sentry',
54+
sourcemap: true,
55+
strict: false,
56+
},
57+
context: 'window',
58+
plugins: [
59+
...plugins,
60+
license({
61+
sourcemap: true,
62+
banner: `/*! @sentry/browser <%= pkg.version %> (${commitHash}) | https://github.com/getsentry/sentry-javascript */`,
63+
}),
64+
],
65+
};
66+
67+
export default [
68+
// ES5 Browser Bundle
69+
{
70+
...bundleConfig,
71+
output: {
72+
...bundleConfig.output,
73+
file: 'build/bundle.js',
74+
},
75+
},
76+
{
77+
...bundleConfig,
78+
output: {
79+
...bundleConfig.output,
80+
file: 'build/bundle.min.js',
81+
},
82+
// Uglify has to be at the end of compilation, BUT before the license banner
83+
plugins: bundleConfig.plugins
84+
.slice(0, -1)
85+
.concat(terserInstance)
86+
.concat(bundleConfig.plugins.slice(-1)),
87+
},
88+
// ------------------
89+
// ES6 Browser Bundle
90+
{
91+
...bundleConfig,
92+
output: {
93+
...bundleConfig.output,
94+
file: 'build/bundle.es6.js',
95+
},
96+
plugins: [
97+
typescript({
98+
tsconfig: 'tsconfig.build.json',
99+
tsconfigOverride: {
100+
compilerOptions: {
101+
declaration: false,
102+
declarationMap: false,
103+
module: 'ES2015',
104+
paths,
105+
target: 'es6',
106+
},
107+
},
108+
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
109+
}),
110+
...plugins.slice(1),
111+
],
112+
},
113+
{
114+
...bundleConfig,
115+
output: {
116+
...bundleConfig.output,
117+
file: 'build/bundle.es6.min.js',
118+
},
119+
plugins: [
120+
typescript({
121+
tsconfig: 'tsconfig.build.json',
122+
tsconfigOverride: {
123+
compilerOptions: {
124+
declaration: false,
125+
declarationMap: false,
126+
module: 'ES2015',
127+
paths,
128+
target: 'es6',
129+
},
130+
},
131+
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
132+
}),
133+
...plugins
134+
.slice(1)
135+
.slice(0, -1)
136+
.concat(terserInstance)
137+
.concat(bundleConfig.plugins.slice(-1)),
138+
],
139+
},
140+
// ------------------
141+
];

packages/react/src/errorboundary.ts

Whitespace-only changes.

packages/react/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * as Sentry from "@sentry/browser"
1+
export * from '@sentry/browser';

packages/react/src/profiler.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

packages/react/src/profiler.tsx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { getCurrentHub } from '@sentry/browser';
2+
import { Integration, IntegrationClass } from '@sentry/types';
3+
import * as React from 'react';
4+
5+
/** The Props Injected by the HOC */
6+
interface InjectedProps {
7+
/** Called when a transaction is finished */
8+
finishProfile(): void;
9+
}
10+
11+
const DEFAULT_DURATION = 30000;
12+
13+
const TRACING_GETTER = ({
14+
id: 'Tracing',
15+
} as any) as IntegrationClass<Integration>;
16+
17+
const getInitActivity = (componentDisplayName: string, timeout = DEFAULT_DURATION): number | null => {
18+
const tracingIntegration = getCurrentHub().getIntegration(TRACING_GETTER);
19+
20+
if (tracingIntegration !== null) {
21+
// tslint:disable-next-line:no-unsafe-any
22+
return (tracingIntegration as any).constructor.pushActivity(
23+
componentDisplayName,
24+
{
25+
data: {},
26+
description: `<${componentDisplayName}>`,
27+
op: 'react',
28+
},
29+
{
30+
autoPopAfter: timeout,
31+
},
32+
);
33+
}
34+
35+
return null;
36+
};
37+
38+
// tslint:disable-next-line: variable-name
39+
export const withProfiler = <P extends object>(WrappedComponent: React.ComponentType<P>, timeout?: number) => {
40+
const componentDisplayName = WrappedComponent.displayName || WrappedComponent.name || 'Component';
41+
42+
return class WithProfiler extends React.Component<Omit<P, keyof InjectedProps>> {
43+
public static displayName: string = `profiler(${componentDisplayName})`;
44+
45+
public activity: number | null = getInitActivity(componentDisplayName, timeout);
46+
47+
// tslint:disable-next-line: completed-docs
48+
public componentWillUnmount(): void {
49+
this.finishProfile();
50+
}
51+
52+
public finishProfile = () => {
53+
if(!this.activity) {
54+
return;
55+
}
56+
57+
const tracingIntegration = getCurrentHub().getIntegration(TRACING_GETTER);
58+
if (tracingIntegration !== null) {
59+
// tslint:disable-next-line:no-unsafe-any
60+
(tracingIntegration as any).constructor.popActivity(this.activity);
61+
this.activity = null
62+
}
63+
}
64+
65+
// tslint:disable-next-line: completed-docs
66+
public render(): React.ReactNode {
67+
return <WrappedComponent {...this.props as P} finishProfile={this.finishProfile} />;
68+
}
69+
};
70+
};

packages/react/test/profiler.test.ts

Whitespace-only changes.

packages/react/test/profiler.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { render } from '@testing-library/react';
2+
import * as React from 'react';
3+
4+
import { withProfiler } from '../src/profiler';
5+
6+
describe('withProfiler', () => {
7+
it("sets displayName properly", () => {
8+
// tslint:disable-next-line: variable-name
9+
const HelloWorld = () => <h1>Hello World</h1>;
10+
11+
// tslint:disable-next-line: variable-name
12+
const ProfiledComponent = withProfiler(HelloWorld);
13+
expect(ProfiledComponent.displayName).toBe("profiler(HelloWorld)")
14+
})
15+
});

packages/react/tsconfig.build.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
44
"baseUrl": ".",
5-
"outDir": "dist"
5+
"outDir": "dist",
6+
"jsx": "react"
67
},
78
"include": ["src/**/*"]
89
}

packages/react/tsconfig.esm.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "../../tsconfig.esm.json",
33
"compilerOptions": {
44
"baseUrl": ".",
5-
"outDir": "esm"
5+
"outDir": "esm",
6+
"jsx": "react"
67
},
78
"include": ["src/**/*"]
89
}

packages/react/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"extends": "./tsconfig.build.json",
3-
"include": ["src/**/*.ts", "test/**/*.ts"],
3+
"include": ["src/**/*.ts", "test/**/*.ts", "src/**/*.tsx", "test/**/*.tsx"],
44
"exclude": ["dist"],
55
"compilerOptions": {
66
"rootDir": ".",
7-
"types": ["node", "jest"]
7+
"types": ["jest"]
88
}
99
}

0 commit comments

Comments
 (0)