Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError in window constructor #627

Closed
Mas0nShi opened this issue Oct 19, 2022 · 4 comments · Fixed by #630
Closed

TypeError in window constructor #627

Mas0nShi opened this issue Oct 19, 2022 · 4 comments · Fixed by #630
Labels
bug Something isn't working

Comments

@Mas0nShi
Copy link
Contributor

Describe the bug

const { Window } = require('happy-dom');

const window = new Window({
    innerWidth: 1024,
    innerHeight: 768,
    url: 'http://localhost:8080',
});

console.log(window);
/*
            this.location.href = options.url;
                               ^

TypeError: Cannot set properties of undefined (setting 'href')
*/
@Mas0nShi Mas0nShi added the bug Something isn't working label Oct 19, 2022
@Mas0nShi
Copy link
Contributor Author

I don't know why we didn't test this in Window.test.ts ?

@jledentu
Copy link
Contributor

Weird. TypeScript generates the Window constructor like this:

constructor(options) {
        super();
        this.innerWidth = options?.innerWidth ? options.innerWidth : 0;
        this.innerHeight = options?.innerHeight ? options.innerHeight : 0;
        if (options?.url) {
            this.location.href = options.url;
        }

        // ...

        this.location = new Location_1.default();
}

So this.location is not yet initialized when calling this.location.href = options.url;. I don't know why TypeScript handles the class field initializers like that, but I'd suggest to move the initialization of location in the constructor.

jledentu added a commit to jledentu/happy-dom that referenced this issue Oct 20, 2022
@Mas0nShi
Copy link
Contributor Author

Yes, I think so. submit a PR if you're ready.

capricorn86 added a commit that referenced this issue Oct 24, 2022
#627@patch: Fix location.href initialization in Window.
@capricorn86
Copy link
Owner

Thanks for reporting @Mas0nShi! 🙂

I guess I missed adding a unit test for it. Weird behavior by Typescript, it is not really expected.

Big thanks to @jledentu for you contribution! 🙂

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v7.6.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants