Skip to content

Commit

Permalink
fix: confusing error when no reporter (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Oct 11, 2023
1 parent a63f156 commit efacb37
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
10 changes: 10 additions & 0 deletions e2e/jest-recorder.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ const presets = {
...mixins.env(true),
...mixins.workers(2),
},
'no-reporter-1': {
...mixins.env(true),
...mixins.workers(2),
reporters: ['default'],
},
'no-reporter-N': {
...mixins.env(true),
...mixins.workers(2),
reporters: ['default'],
},
'no-env-1': {
...mixins.env(false),
...mixins.workers(1),
Expand Down
32 changes: 21 additions & 11 deletions src/environment-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { inspect } from 'util';
import type { EnvironmentContext, JestEnvironment, JestEnvironmentConfig } from '@jest/environment';
import type { Circus } from '@jest/types';
import { JestMetadataError } from './errors';
Expand All @@ -6,26 +7,35 @@ import { SemiAsyncEmitter } from './utils';

const emitterMap: WeakMap<object, SemiAsyncEmitter<ForwardedCircusEvent>> = new WeakMap();

/**
* @param jestEnvironment {@link JestEnvironment}
* @param _jestEnvironmentConfig {@link JestEnvironmentConfig}
* @param environmentContext {@link EnvironmentContext}
*/
export function onTestEnvironmentCreate(
jestEnvironment: JestEnvironment,
_jestEnvironmentConfig: JestEnvironmentConfig,
jestEnvironmentConfig: JestEnvironmentConfig,
environmentContext: EnvironmentContext,
): void {
injectRealmIntoSandbox(jestEnvironment.global, realm);
const testFilePath = environmentContext.testPath;
realm.environmentHandler.handleEnvironmentCreated(testFilePath);
realm.events.add(realm.setEmitter);

if (realm.type === 'child_process') {
realm.coreEmitter.emit({
type: 'add_test_file',
testFilePath,
});
if (!realm.globalMetadata.hasTestFileMetadata(testFilePath)) {
if (realm.type === 'child_process') {
realm.coreEmitter.emit({
type: 'add_test_file',
testFilePath,
});
} else {
const { globalConfig } = jestEnvironmentConfig;
const first = <T>(r: T[]) => r[0];
const hint = globalConfig?.reporters
? ` "reporters": ${inspect(globalConfig.reporters.map(first))}\n`
: ''; // Jest 27 fallback

throw new JestMetadataError(
`Cannot use a metadata test environment without a metadata server.\n` +
`Please check that at least one of the reporters in your Jest config inherits from "jest-metadata/reporter".\n` +
hint,
);
}
}

const testEventHandler = ({ event, state }: ForwardedCircusEvent) => {
Expand Down
9 changes: 6 additions & 3 deletions src/metadata/containers/GlobalMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ export class GlobalMetadata extends BaseMetadata {
}

public getTestFileMetadata(testFilePath: string): TestFileMetadata {
const testFileMetadata = this[$byTestFilePath].get(testFilePath);
if (!testFileMetadata) {
if (!this.hasTestFileMetadata(testFilePath)) {
throw new JestMetadataError(`No file metadata found for: ${testFilePath}`);
}

return testFileMetadata;
return this[$byTestFilePath].get(testFilePath)!;
}

public hasTestFileMetadata(testFilePath: string): boolean {
return this[$byTestFilePath].has(testFilePath);
}

public registerTestFile(testFilePath: string): TestFileMetadata {
Expand Down

0 comments on commit efacb37

Please sign in to comment.