Skip to content

Commit

Permalink
Add integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Jun 21, 2023
1 parent 45d4700 commit f0d8e74
Show file tree
Hide file tree
Showing 14 changed files with 10,269 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
"lint": "run-s lint:prettier lint:eslint",
"lint:eslint": "eslint . --format stylish",
"lint:prettier": "prettier --check \"{src,test,scripts}/**/**.ts\"",
"test": "jest",
"test": "run-s test:unit test:integration",
"test:unit": "jest",
"test:integration": "ts-node ./test/integration/run.ts",
"test:watch": "jest --watch",
"yalc:publish": "ts-node ../../scripts/prepack.ts && yalc publish build --push"
},
Expand Down
29 changes: 29 additions & 0 deletions packages/react/test/integration/create-hash-router/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

/test-results/
/playwright-report/
/playwright/.cache/

!*.d.ts
66 changes: 66 additions & 0 deletions packages/react/test/integration/create-hash-router/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "react-create-hash-router-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"@sentry/react": "file:../../..",
"@testing-library/jest-dom": "5.14.1",
"@testing-library/react": "13.0.0",
"@testing-library/user-event": "13.2.1",
"@types/jest": "27.0.1",
"@types/node": "16.7.13",
"@types/react": "18.0.0",
"@types/react-dom": "18.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-router-dom": "^6.4.1",
"react-scripts": "5.0.1",
"typescript": "4.4.2",
"web-vitals": "2.1.0"
},
"scripts": {
"build": "react-scripts build",
"start": "serve -s build",
"pretest": "run-s build",
"test": "playwright test",
"posttest": "rimraf build node_modules"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@playwright/test": "1.31.1",
"serve": "14.0.1"
},
"resolutions": {
"@sentry/browser": "file:../../../../browser",
"@sentry/core": "file:../../../../core",
"@sentry/integrations": "file:../../../../integrations",
"@sentry/node": "file:../../../../node",
"@sentry/react": "file:../../../../react",
"@sentry/replay": "file:../../../../replay",
"@sentry/tracing": "file:../../../../tracing",
"@sentry-internal/tracing": "file:../../../../tracing-internal",
"@sentry/types": "file:../../../../types",
"@sentry/utils": "file:../../../../utils"
},
"volta": {
"node": "16.19.0",
"yarn": "1.22.19"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';

/**
* See https://playwright.dev/docs/test-configuration.
*/
const config: PlaywrightTestConfig = {
testDir: './tests',
/* Maximum time one test can run for. */
timeout: 60 * 1000,
expect: {
/**
* Maximum time expect() should wait for the condition to be met.
* For example in `await expect(locator).toHaveText();`
*/
timeout: 5000,
},
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: 0,
/* Opt out of parallel tests on CI. */
workers: 1,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'list',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 0,

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},
// For now we only test Chrome!
// {
// name: 'firefox',
// use: {
// ...devices['Desktop Firefox'],
// },
// },
// {
// name: 'webkit',
// use: {
// ...devices['Desktop Safari'],
// },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: 'yarn start',
port: 4321,
env: {
PORT: 4321,
},
},
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
50 changes: 50 additions & 0 deletions packages/react/test/integration/create-hash-router/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import * as Sentry from '@sentry/react';
import {
useLocation,
useNavigationType,
createRoutesFromChildren,
matchRoutes,
RouterProvider,
createHashRouter,
} from 'react-router-dom';
import Index from './pages/Index';
import User from './pages/User';

Sentry.init({
// environment: 'qa', // dynamic sampling bias to keep transactions
dsn: 'https://[email protected]/1337',
integrations: [
new Sentry.BrowserTracing({
routingInstrumentation: Sentry.reactRouterV6Instrumentation(
React.useEffect,
useLocation,
useNavigationType,
createRoutesFromChildren,
matchRoutes,
),
}),
],
debug: true,
// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0,
});

const sentryCreateHashRouter = Sentry.wrapCreateBrowserRouter(createHashRouter);

const router = sentryCreateHashRouter([
{
path: '/',
element: <Index />,
},
{
path: '/user/:id',
element: <User />,
},
]);

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);

root.render(<RouterProvider router={router} />);
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react';
import * as Sentry from '@sentry/react';
import { Link } from 'react-router-dom';

const Index = () => {
return (
<>
<input
type="button"
value="Capture Exception"
id="exception-button"
onClick={() => {
Sentry.captureException(new Error('I am an error!'));
}}
/>
<Link to="/user/5" id="navigation">
navigate
</Link>
</>
);
};

export default Index;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

const User = () => {
return <p>I am a blank page :)</p>;
};

export default User;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
48 changes: 48 additions & 0 deletions packages/react/test/integration/create-hash-router/tests/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Transaction } from '@sentry/types';
import { getMultipleSentryEnvelopeRequests } from '../../utils';
import { test, expect } from '@playwright/test';

test('Sends an error to Sentry', async ({ page }) => {
await page.goto('/');

await page.waitForTimeout(2000);

const [, errors] = await Promise.all([
page.click('#exception-button'),
getMultipleSentryEnvelopeRequests<Event>(page, 1),
]);

expect(errors[0].exception?.values?.[0].value).toBe('I am an error!');
});

test('Sends a pageload transaction to Sentry', async ({ page }) => {
const [pageloadEnvelope] = await getMultipleSentryEnvelopeRequests<Transaction>(page, 1, {
url: '/',
});

expect(pageloadEnvelope.contexts?.trace?.op).toBe('pageload');
expect(pageloadEnvelope.transaction).toBe('/');
});

test('Sends a parameterized pageload transaction to Sentry', async ({ page }) => {
const [pageloadEnvelope] = await getMultipleSentryEnvelopeRequests<Transaction>(page, 1, {
url: '/#/user/123',
});

expect(pageloadEnvelope.contexts?.trace?.op).toBe('pageload');
expect(pageloadEnvelope.transaction).toBe('/user/:id');
});

test('Sends a navigation transaction to Sentry', async ({ page }) => {
await page.goto('/');

await page.waitForTimeout(2000);

const [, transactions] = await Promise.all([
page.click('#navigation'),
getMultipleSentryEnvelopeRequests<Transaction>(page, 1),
]);

expect(transactions[0].contexts?.trace?.op).toBe('navigation');
expect(transactions[0].transaction).toBe('/user/:id');
});
20 changes: 20 additions & 0 deletions packages/react/test/integration/create-hash-router/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": ["src", "tests", "../utils.ts"]
}
Loading

0 comments on commit f0d8e74

Please sign in to comment.