jest-circus-allure-environment
jest-circus-allure-environment copied to clipboard
Cannot overwrite handleTestEvent because dist moves the method to the constructor
Describe the bug
The dist/allure-base-environment.js
file is transpiled as:
function extendAllureBaseEnvironment(Base) {
// @ts-expect-error (ts(2545)) Incorrect assumption about a mixin class: https://github.com/microsoft/TypeScript/issues/37142
return class AllureBaseEnvironment extends Base {
constructor(config, context) {
super(config, context);
this.handleTestEvent = (event, state) => {
// [...]
Probably the tsc compiler doesn't ship with the public class properties proposal (stage 3), but babel does! Therefore we can no longer call super.handleTestEvent(event, state)
(as it doesn't exists) if we extend this class.
Workaround
In the constructor, we would store the reference and call it in our own handleTestEvent, this is an ugly hack.
Expected behavior
The class is compiled with es6 support for class properties, and we let others decide if further transpiling is needed on their projects.
Screenshots
Additional context
I think handleTestEvent is also not a declared method in the returned type, which also makes it very tricky to call. Maybe we can return typeof NodeEnvironment & {handleTestEvent: Circus.EventHandler}
as AllureNodeEnvironment
?
The workaround I am using right now looks like this:
export default class AllureEnvironment extends Base {
global!: Global & {allure: JestAllureInterface};
handleTestEvent: Circus.EventHandler;
constructor(config: JestConfig.ProjectConfig, context: EnvironmentContext) {
super(config, context);
const parentHandleTestEvent = Reflect.get(this, 'handleTestEvent');
this.handleTestEvent = async (event: Circus.Event, state: Circus.State) => {
await parentHandleTestEvent.call(this, event, state);
// custom stuff
};
}
}
Would it be possible to publish the source .ts
files as well?
As far as I can tell this is a commentary on NodeEnvironment
, which implements JestEnvironment
but doesn't implement handleTestEvent
because it's optional.
This is certainly because of the way I passed down the types with extendAllureBaseEnvironment
when creating AllureNodeEnvironment
. I can see why you're getting that "does not exist on type" error.
I'm fairly sure I know how to fix this so I will work on a PR. Good find!
If possible, could you use my repo as your dependency to test my changes? https://github.com/lpolito/jest-circus-allure-environment
As far as I know you can just do yarn add -D lpolito/jest-circus-allure-environment
. I think you'll have to go into its node_module directory and run yarn && yarn build
, though, for it to work (unless it depends on how you have something like babel set up?)
Hey @Rendez :wave:
Thanks for bringing this to our attention. Were you able to test with @lpolito's fix?
Hi, not quite yet, but I'll get to it soon!
@lpolito I just tried it with the steps you indicated, and unfortunately allure-node-enviroment.d.ts
looks like this:
declare const _default;
export default _default;
Which results in any
:
I am also troubled by this issue.