jest-circus-allure-environment icon indicating copy to clipboard operation
jest-circus-allure-environment copied to clipboard

Cannot overwrite handleTestEvent because dist moves the method to the constructor

Open Rendez opened this issue 3 years ago • 8 comments

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

image

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?

Rendez avatar Jun 08 '21 20:06 Rendez

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
         };
    }
}

Rendez avatar Jun 09 '21 11:06 Rendez

Would it be possible to publish the source .ts files as well?

Rendez avatar Jun 10 '21 10:06 Rendez

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!

lpolito avatar Jun 11 '21 21:06 lpolito

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?)

lpolito avatar Jun 12 '21 16:06 lpolito

Hey @Rendez :wave:

Thanks for bringing this to our attention. Were you able to test with @lpolito's fix?

ryparker avatar Jun 21 '21 07:06 ryparker

Hi, not quite yet, but I'll get to it soon!

Rendez avatar Jun 21 '21 15:06 Rendez

@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:

image

Rendez avatar Jul 02 '21 15:07 Rendez

I am also troubled by this issue.

cumthyb avatar Dec 09 '21 07:12 cumthyb