-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate jest-environment-emit
- Loading branch information
Showing
5 changed files
with
160 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import JestEnvironmentJsdom from 'jest-metadata/environment-jsdom'; | ||
|
||
import { WithAllure2 } from './decorator'; | ||
import listener from './listener'; | ||
|
||
export const TestEnvironment = JestEnvironmentJsdom.derive( | ||
listener, | ||
'WithAllure', | ||
); | ||
|
||
export const TestEnvironment = WithAllure2(JestEnvironmentJsdom); | ||
export default TestEnvironment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import type { Circus } from '@jest/types'; | ||
import { state } from 'jest-metadata'; | ||
import { Stage, Status } from '@noomorph/allure-js-commons'; | ||
import type { | ||
AllureTestCaseMetadata, | ||
AllureTestStepMetadata, | ||
} from 'jest-allure2-reporter'; | ||
import type { | ||
TestEnvironmentSetupEvent, | ||
TestEnvironmentCircusEvent, | ||
EnvironmentListenerFn, | ||
} from 'jest-environment-emit'; | ||
|
||
import { CODE, PREFIX, WORKER_ID } from '../constants'; | ||
import realm from '../realms'; | ||
|
||
const listener: EnvironmentListenerFn = (context) => { | ||
context.testEvents | ||
.on('test_environment_setup', testEnvironmentSetup) | ||
.on('add_hook', addHook) | ||
.on('add_test', addTest) | ||
.on('test_start', executableStart) | ||
.on('test_todo', testSkip) | ||
.on('test_skip', testSkip) | ||
.on('test_done', testDone) | ||
.on('hook_start', executableStart) | ||
.on('hook_failure', executableFailure) | ||
.on('hook_success', executableSuccess) | ||
.on('test_fn_start', executableStart) | ||
.on('test_fn_success', executableSuccess) | ||
.on('test_fn_failure', executableFailure) | ||
.on('teardown', flushFiles); | ||
}; | ||
|
||
function testEnvironmentSetup({ env }: TestEnvironmentSetupEvent) { | ||
env.global.__ALLURE__ = realm; | ||
state.currentMetadata.set(WORKER_ID, process.env.JEST_WORKER_ID); | ||
} | ||
|
||
function addHook({ | ||
event, | ||
}: TestEnvironmentCircusEvent<Circus.Event & { name: 'add_hook' }>) { | ||
const code = event.fn.toString(); | ||
const hidden = code.includes( | ||
"during setup, this cannot be null (and it's fine to explode if it is)", | ||
); | ||
|
||
const metadata = { | ||
code, | ||
} as Record<string, unknown>; | ||
|
||
if (hidden) { | ||
delete metadata.code; | ||
metadata.hidden = true; | ||
} | ||
|
||
state.currentMetadata.assign(PREFIX, metadata); | ||
} | ||
|
||
function addTest({ | ||
event, | ||
}: TestEnvironmentCircusEvent<Circus.Event & { name: 'add_test' }>) { | ||
state.currentMetadata.set(CODE, event.fn.toString()); | ||
} | ||
|
||
// eslint-disable-next-line no-empty-pattern | ||
function executableStart({}: TestEnvironmentCircusEvent) { | ||
const metadata: AllureTestStepMetadata = { | ||
start: Date.now(), | ||
stage: Stage.RUNNING, | ||
}; | ||
|
||
state.currentMetadata.assign(PREFIX, metadata); | ||
} | ||
|
||
function executableFailure({ | ||
event, | ||
}: TestEnvironmentCircusEvent< | ||
Circus.Event & { name: 'test_fn_failure' | 'hook_failure' } | ||
>) { | ||
const metadata: AllureTestStepMetadata = { | ||
stop: Date.now(), | ||
stage: Stage.INTERRUPTED, | ||
status: Status.FAILED, | ||
}; | ||
|
||
if (event.error) { | ||
const message = event.error.message ?? `${event.error}`; | ||
const trace = event.error.stack; | ||
|
||
metadata.statusDetails = { message, trace }; | ||
} | ||
|
||
state.currentMetadata.assign(PREFIX, metadata); | ||
} | ||
|
||
async function flushFiles() { | ||
await realm.runtime.flush(); | ||
} | ||
|
||
// eslint-disable-next-line no-empty-pattern | ||
function executableSuccess({}: TestEnvironmentCircusEvent) { | ||
const metadata: AllureTestStepMetadata = { | ||
stop: Date.now(), | ||
stage: Stage.FINISHED, | ||
status: Status.PASSED, | ||
}; | ||
|
||
state.currentMetadata.assign(PREFIX, metadata); | ||
} | ||
|
||
function testSkip() { | ||
const metadata: AllureTestCaseMetadata = { | ||
stop: Date.now(), | ||
stage: Stage.PENDING, | ||
status: Status.SKIPPED, | ||
}; | ||
|
||
state.currentMetadata.assign(PREFIX, metadata); | ||
} | ||
|
||
function testDone({ | ||
event, | ||
}: TestEnvironmentCircusEvent<Circus.Event & { name: 'test_done' }>) { | ||
const hasErrors = event.test.errors.length > 0; | ||
const errorStatus = event.test.errors.some((errors) => { | ||
return Array.isArray(errors) | ||
? errors.some(isMatcherError) | ||
: isMatcherError(errors); | ||
}) | ||
? Status.FAILED | ||
: Status.BROKEN; | ||
|
||
const metadata: AllureTestCaseMetadata = { | ||
stop: Date.now(), | ||
stage: hasErrors ? Stage.INTERRUPTED : Stage.FINISHED, | ||
status: hasErrors ? errorStatus : Status.PASSED, | ||
}; | ||
|
||
state.currentMetadata.assign(PREFIX, metadata); | ||
} | ||
|
||
function isMatcherError(error: any) { | ||
return Boolean(error?.matcherResult); | ||
} | ||
|
||
export default listener; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import JestEnvironmentNode from 'jest-metadata/environment-node'; | ||
|
||
import { WithAllure2 } from './decorator'; | ||
import listener from './listener'; | ||
|
||
export const TestEnvironment = JestEnvironmentNode.derive( | ||
listener, | ||
'WithAllure', | ||
); | ||
|
||
export const TestEnvironment = WithAllure2(JestEnvironmentNode); | ||
export default TestEnvironment; |