Skip to content

Commit

Permalink
test: Electron v28 main process ESM (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Oct 17, 2023
1 parent eb1f60b commit d5279b2
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/electron-forge-webpack/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ description: Electron Forge Webpack with contextIsolation and sandbox
command: yarn && yarn package
condition: supportsContextIsolation && supportsSandbox
timeout: 120
skipEsmAutoTransform: true
1 change: 1 addition & 0 deletions examples/electron-forge/recipe.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
description: Electron Forge
command: yarn
timeout: 120
skipEsmAutoTransform: true
1 change: 1 addition & 0 deletions examples/electron-react-boilerplate/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ command: npm install && npm run build
condition: version.major >= 16
timeout: 120
distPath: release/app
skipEsmAutoTransform: true
1 change: 1 addition & 0 deletions examples/electron-vite/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ description: Electron Vite
command: yarn && yarn build
condition: version.major >= 7
timeout: 120
skipEsmAutoTransform: true
1 change: 1 addition & 0 deletions examples/webpack-context-isolation-preload/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ description: Webpack 5 app with contextIsolation and sandbox with preload
command: yarn && yarn build
condition: supportsContextIsolation && supportsSandbox
timeout: 120
skipEsmAutoTransform: true
1 change: 1 addition & 0 deletions examples/webpack-context-isolation/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ description: Webpack 5 app with contextIsolation and sandbox
command: yarn && yarn build
condition: supportsContextIsolation && supportsSandbox
timeout: 120
skipEsmAutoTransform: true
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"fix": "run-s fix:eslint fix:prettier",
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
"fix:eslint": "eslint . --format stylish --fix",
"update-electron-versions": "electron-latest-versions --start 2 > ./test/e2e/versions.json",
"update-electron-versions": "electron-latest-versions --start 2 --alpha --beta > ./test/e2e/versions.json",
"update-sdk-versions": "node ./scripts/update-sdk-versions.mjs",
"pretest": "yarn build",
"test": "cross-env TS_NODE_PROJECT=tsconfig.test.json xvfb-maybe electron-mocha --require ts-node/register/transpile-only --timeout 12000 ./test/unit/**/*.ts",
Expand Down
59 changes: 55 additions & 4 deletions test/e2e/recipe/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event } from '@sentry/types';
import { parseSemver } from '@sentry/utils';
import { expect } from 'chai';
import { spawnSync } from 'child_process';
import { mkdirSync, writeFileSync } from 'fs';
Expand Down Expand Up @@ -53,6 +54,51 @@ export function getCategorisedTestRecipes(electronVersion: string): Record<strin
}, {} as Record<string, RecipeRunner[]>);
}

function insertAfterLastImport(content: string, insert: string): string {
const lines = content.split('\n');
const importCount = lines.filter((l) => l.startsWith('import ')).length;

let output = '';
let count = 0;
for (const line of lines) {
output += `${line}\n`;

if (line.startsWith('import ')) {
count += 1;
}

if (count === importCount) {
output += `${insert}\n`;
count += 1;
}
}

return output;
}

function convertToEsm(filename: string, content: string): [string, string] {
if (filename.endsWith('package.json')) {
const obj = JSON.parse(content);
obj.main = obj.main.replace(/\.js$/, '.mjs');
return [filename, JSON.stringify(obj)];
}

if (filename.endsWith('main.js')) {
return [
filename.replace(/\.js$/, '.mjs'),
insertAfterLastImport(
content
.replace(/(?:const|var) (\{[\s\S]*?\}) = require\((\S*?)\)/g, 'import $1 from $2')
.replace(/(?:const|var) (\S*) = require\((\S*)\)/g, 'import * as $1 from $2'),
`import * as url from 'url';
const __dirname = url.fileURLToPath(new url.URL('.', import.meta.url));`,
),
];
}

return [filename, content];
}

export class RecipeRunner {
private constructor(private readonly _electronVersion: string, private readonly _recipe: TestRecipe) {}

Expand Down Expand Up @@ -105,9 +151,6 @@ export class RecipeRunner {
for (const file of Object.keys(this._recipe.files)) {
log(`Writing file '${file}'`);

const path = join(appPath, file);

mkdirSync(dirname(path), { recursive: true });
let content = this._recipe.files[file];

// Replace with the test server localhost DSN
Expand All @@ -125,12 +168,20 @@ export class RecipeRunner {
/"@sentry\/electron": ".*"/,
`"@sentry/electron": "file:./../../../../sentry-electron-${SDK_VERSION}.tgz"`,
)
// We replace the Sentry JavaScript dependency versions to match that of @sentry/electron
// We replace the Sentry JavaScript dependency versions to match that of @sentry/core
.replace(/"@sentry\/replay": ".*"/, `"@sentry/replay": "${JS_VERSION}"`)
.replace(/"@sentry\/react": ".*"/, `"@sentry/react": "${JS_VERSION}"`)
.replace(/"@sentry\/vue": ".*"/, `"@sentry/vue": "${JS_VERSION}"`);
}

let filename = file;

if (!this._recipe.metadata.skipEsmAutoTransform && (parseSemver(this._electronVersion).major || 0) >= 28) {
[filename, content] = convertToEsm(file, content);
}

const path = join(appPath, filename);
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, content);
}

Expand Down
1 change: 1 addition & 0 deletions test/e2e/recipe/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function normalizeEvent(event: Event & ReplayEvent): Event {
frame.colno = 0;
frame.lineno = 0;
frame.function = '{{function}}';
frame.filename = frame.filename?.replace(/\.mjs$/, '.js');
}
}

Expand Down
1 change: 1 addition & 0 deletions test/e2e/recipe/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface TestMetadata {
runTwice?: boolean;
expectedError?: string;
distPath?: string;
skipEsmAutoTransform?: boolean;
}

export interface TestRecipe {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/test-apps/other/error-entry-point/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ command: yarn && yarn build
condition: version.major >= 15
timeout: 120
expectedError: This code is intended to run in the Electron renderer
skipEsmAutoTransform: true
1 change: 1 addition & 0 deletions test/e2e/test-apps/other/error-iframe/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ description: JavaScript Error from <iframe>
command: yarn && yarn build
condition: supportsContextIsolation && supportsSandbox
timeout: 120
skipEsmAutoTransform: true
1 change: 1 addition & 0 deletions test/e2e/test-apps/other/main-process-module/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ description: Webpack app with error in main process
command: yarn && yarn build
condition: supportsContextIsolation && supportsSandbox
timeout: 120
skipEsmAutoTransform: true
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
description: Electron net Tracing
command: yarn
skipEsmAutoTransform: true
1 change: 1 addition & 0 deletions test/e2e/test-apps/other/net-breadcrumbs/recipe.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
description: Electron net Breadcrumbs
command: yarn
skipEsmAutoTransform: true
2 changes: 1 addition & 1 deletion test/e2e/versions.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["2.0.18","3.1.13","4.2.12","5.0.13","6.1.12","7.3.3","8.5.5","9.4.4","10.4.7","11.5.0","12.2.3","13.6.9","14.2.9","15.5.7","16.2.8","17.4.11","18.3.15","19.1.9","20.3.12","21.4.4","22.3.27","23.3.13","24.8.7","25.9.0","26.3.0","27.0.0"]
["2.0.18","3.1.13","4.2.12","5.0.13","6.1.12","7.3.3","8.5.5","9.4.4","10.4.7","11.5.0","12.2.3","13.6.9","14.2.9","15.5.7","16.2.8","17.4.11","18.3.15","19.1.9","20.3.12","21.4.4","22.3.27","23.3.13","24.8.8","25.9.1","26.4.0","27.0.0","28.0.0-alpha.3"]

0 comments on commit d5279b2

Please sign in to comment.