Skip to content

Commit

Permalink
fix: add sourcemap support for setup files (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Aug 10, 2024
1 parent 6d1ff05 commit a43a65d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ declare module 'jest-allure2-reporter' {
/** @inheritDoc */
export interface AllureTestRunMetadata extends AllureTestCaseMetadata {
config: unknown;
loadedFiles: string[];
sourceLocation?: never;
transformedCode?: never;
}
Expand Down
17 changes: 15 additions & 2 deletions src/environment/listener.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash';
import type { AllureTestItemSourceLocation } from 'jest-allure2-reporter';
import type { Circus } from '@jest/types';
import type {
Expand All @@ -6,15 +7,17 @@ import type {
TestEnvironmentSetupEvent,
} from 'jest-environment-emit';
import * as StackTrace from 'stacktrace-js';
import type { JestEnvironmentConfig } from '@jest/environment';

import * as api from '../api';
import realm from '../realms';
import { autoIndent, getStatusDetails, isJestAssertionError, isLibraryPath } from '../utils';

const listener: EnvironmentListenerFn = (context) => {
context.testEvents
.on('test_environment_setup', injectGlobals)
.on('test_environment_setup', setWorkerId)
.once('test_environment_setup', injectGlobals)
.once('test_environment_setup', setWorkerId)
.once('test_environment_setup', reportSetupFiles(context.config))
.on('add_hook', addHookType)
.on('add_hook', addSourceLocation)
.on('add_hook', addSourceCode)
Expand Down Expand Up @@ -86,6 +89,16 @@ function setWorkerId() {
}
}

function reportSetupFiles(config: JestEnvironmentConfig) {
return () => {
const { setupFilesAfterEnv = [], setupFiles = [] } = config.projectConfig ?? {};
const globalMetadata = realm.runtimeContext.getGlobalMetadata();
const loadedFiles = globalMetadata.get('loadedFiles', []);
const files = _.difference([...setupFiles, ...setupFilesAfterEnv], loadedFiles);
globalMetadata.push('loadedFiles', files);
};
}

function addHookType({ event }: TestEnvironmentCircusEvent<Circus.Event & { name: 'add_hook' }>) {
const metadata = realm.runtimeContext.getCurrentMetadata();
metadata.set('hookType', event.hookType);
Expand Down
12 changes: 9 additions & 3 deletions src/reporter/JestAllure2Reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ export class JestAllure2Reporter extends JestMetadataReporter {
async onTestFileStart(test: Test) {
super.onTestFileStart(test);

await FileNavigatorCache.instance.scanSourcemap(test.path);
const execute = this.#onTestFileStart.bind(this, test);
const attempt = this.#attemptSync.bind(this, 'onTestFileStart()', execute);
const testPath = path.relative(this._globalConfig.rootDir, test.path);
log.trace.begin(__TID(test), testPath);
log.trace.complete(__TID(test), 'onTestFileStart', attempt);
await log.trace.complete(__TID(test), 'onTestFileStart', attempt);
}

#onTestFileStart(test: Test) {
async #onTestFileStart(test: Test) {
await FileNavigatorCache.instance.scanSourcemap(test.path);

const rawMetadata = JestAllure2Reporter.query.test(test);
const testFileMetadata = new AllureMetadataProxy<AllureTestFileMetadata>(rawMetadata);

Expand Down Expand Up @@ -199,6 +200,11 @@ export class JestAllure2Reporter extends JestMetadataReporter {
const rawMetadata = JestAllure2Reporter.query.test(test);
const testFileMetadata = new AllureMetadataProxy<AllureTestFileMetadata>(rawMetadata);
const globalMetadataProxy = this._globalMetadataProxy;

for (const loadedFile of globalMetadataProxy.get('loadedFiles', [])) {
await FileNavigatorCache.instance.scanSourcemap(loadedFile);
}

const allureWriter = this._writer;

fallbacks.onTestFileResult(test, testFileMetadata);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/FileNavigatorCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FileNavigator } from './index';

export class FileNavigatorCache {
#cache = new Map<string, Promise<FileNavigator | undefined>>();
#scannedSourcemaps = new Set<string>();

resolve(filePath: string): Promise<FileNavigator | undefined> {
const absolutePath = path.isAbsolute(filePath) ? filePath : path.resolve(filePath);
Expand All @@ -21,6 +22,9 @@ export class FileNavigatorCache {
async scanSourcemap(filePath: string): Promise<void> {
const sourceMapPath = `${filePath}.map`;

if (this.#scannedSourcemaps.has(sourceMapPath)) return;
this.#scannedSourcemaps.add(sourceMapPath);

const doesNotExist = await fs.access(sourceMapPath).catch(() => true);
if (doesNotExist) return;

Expand Down

0 comments on commit a43a65d

Please sign in to comment.