Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

Cannot overwrite handleTestEvent because dist moves the method to the constructor #197

Open
Rendez opened this issue Jun 8, 2021 · 8 comments
Labels
bug Something isn't working response-requested Waiting on author's response
Milestone

Comments

@Rendez
Copy link

Rendez commented Jun 8, 2021

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 Rendez added the bug Something isn't working label Jun 8, 2021
@Rendez
Copy link
Author

Rendez commented Jun 9, 2021

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
Copy link
Author

Rendez commented Jun 10, 2021

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

@lpolito
Copy link
Collaborator

lpolito commented Jun 11, 2021

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
Copy link
Collaborator

lpolito commented Jun 12, 2021

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

@ryparker ryparker added the response-requested Waiting on author's response label Jun 21, 2021
@ryparker
Copy link
Owner

Hey @Rendez 👋

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

@Rendez
Copy link
Author

Rendez commented Jun 21, 2021

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

@Rendez
Copy link
Author

Rendez commented Jul 2, 2021

@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

@ryparker ryparker added this to the V2.0 milestone Aug 6, 2021
@cumthyb
Copy link

cumthyb commented Dec 9, 2021

I am also troubled by this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working response-requested Waiting on author's response
Projects
None yet
Development

No branches or pull requests

4 participants