Skip to content

Commit 26f5416

Browse files
authored
test: Add manual webpack tests with jsdom browser environment (#1760)
* test: Add manual webpack tests with jsdom browser environment * test: Exit process when anything errors out * Use jsdom v11 to support Node 6
1 parent 0201c40 commit 26f5416

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

packages/browser/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"devDependencies": {
2424
"@types/md5": "2.1.32",
2525
"chai": "^4.1.2",
26+
"jsdom": "^11.12.0",
2627
"karma": "^2.0.2",
2728
"karma-chai": "^0.1.0",
2829
"karma-chrome-launcher": "^2.2.0",
@@ -47,7 +48,8 @@
4748
"rollup-plugin-uglify": "^3.0.0",
4849
"sinon": "^5.0.3",
4950
"tslint": "^5.11.0",
50-
"typescript": "^3.0.1"
51+
"typescript": "^3.0.1",
52+
"webpack": "^4.26.0"
5153
},
5254
"scripts": {
5355
"build": "rollup --config",
@@ -65,6 +67,7 @@
6567
"test:integration": "karma start test/karma/karma.integration.config.js",
6668
"test:integration:watch": "karma start test/karma/karma.integration.config.js --auto-watch --no-single-run",
6769
"test:saucelabs": "karma start test/karma/karma.saucelabs.config.js",
70+
"test:manual": "node test/manual/npm-build.js && rm test/manual/tmp.js",
6871
"size:check": "cat build/bundle.min.js | gzip -9 | wc -c | awk '{$1=$1/1024; print $1,\"kB\";}'",
6972
"version": "node ../../scripts/versionbump.js src/version.ts"
7073
},
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const webpack = require('webpack');
4+
const { JSDOM } = require('jsdom');
5+
6+
webpack(
7+
{
8+
entry: path.join(__dirname, 'test-code.js'),
9+
output: {
10+
path: __dirname,
11+
filename: 'tmp.js',
12+
},
13+
mode: 'production',
14+
},
15+
(err, stats) => {
16+
if (err) {
17+
console.error(err.stack || err);
18+
if (err.details) {
19+
console.error(err.details);
20+
}
21+
return;
22+
}
23+
24+
const info = stats.toJson();
25+
26+
if (stats.hasErrors()) {
27+
console.error(info.errors);
28+
}
29+
30+
if (stats.hasWarnings()) {
31+
console.warn(info.warnings);
32+
}
33+
34+
runTests();
35+
},
36+
);
37+
38+
function runTests() {
39+
const bundlePath = path.join(__dirname, 'tmp.js');
40+
const { window } = new JSDOM(``, { runScripts: 'dangerously' });
41+
42+
window.onerror = function() {
43+
process.exit(1);
44+
};
45+
46+
const myLibrary = fs.readFileSync(bundlePath, { encoding: 'utf-8' });
47+
const scriptEl = window.document.createElement('script');
48+
scriptEl.textContent = myLibrary;
49+
window.document.body.appendChild(scriptEl);
50+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const Sentry = require('../../dist/index.js');
2+
3+
// Init
4+
Sentry.init({
5+
dsn: 'https://[email protected]/42',
6+
beforeSend(event) {
7+
console.log('Got an event');
8+
return null;
9+
},
10+
beforeBreadcrumb(crumb) {
11+
console.log(`Got a breadcrumb: ${crumb.category}`);
12+
return crumb;
13+
},
14+
});
15+
16+
// Configure
17+
Sentry.configureScope(scope => {
18+
scope.setExtra('foo', 'bar');
19+
scope.setFingerprint('foo');
20+
scope.setLevel('warning');
21+
scope.setTag('foo', 'bar');
22+
scope.setUser('foo', 'bar');
23+
});
24+
25+
// Breadcrumbs integration
26+
window.console.log('Console', 'Breadcrumb');
27+
window.console.error({ foo: 'bar' });
28+
29+
const clickEvent = new MouseEvent('click');
30+
const clickElement = window.document.createElement('button');
31+
clickElement.addEventListener('click', () => {
32+
// do nothing, just capture a breadcrumb
33+
});
34+
clickElement.dispatchEvent(clickEvent);
35+
36+
const keypressEvent = new KeyboardEvent('keypress');
37+
const keypressElement = window.document.createElement('input');
38+
keypressElement.addEventListener('keypress', () => {
39+
// do nothing, just capture a breadcrumb
40+
});
41+
keypressElement.dispatchEvent(keypressEvent);
42+
43+
// Basic breadcrumb
44+
Sentry.addBreadcrumb({
45+
category: 'basic',
46+
message: 'crumb',
47+
});
48+
49+
// Capture methods
50+
Sentry.captureException(new Error('foo'));
51+
Sentry.captureMessage('bar');
52+
53+
// Scope behavior
54+
Sentry.withScope(scope => {
55+
scope.setExtra('baz', 'qux');
56+
scope.setFingerprint('baz');
57+
scope.setLevel('error');
58+
scope.setTag('baz', 'qux');
59+
scope.setUser('baz', 'qux');
60+
Sentry.captureException(new TypeError('bar'));
61+
Sentry.captureMessage('baz');
62+
});

scripts/browser-integration.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ yarn
66
yarn build
77
cd packages/browser
88
yarn test:integration
9+
yarn test:manual
910

0 commit comments

Comments
 (0)