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: i is not a function #2092

Closed
6 tasks done
joshnuss opened this issue Sep 28, 2022 · 8 comments
Closed
6 tasks done

TypeError: i is not a function #2092

joshnuss opened this issue Sep 28, 2022 · 8 comments

Comments

@joshnuss
Copy link
Sponsor Contributor

joshnuss commented Sep 28, 2022

Describe the bug

Test fails with error message TypeError: i is not a function, and there is no stack trace displayed.

It appears the error is caused when a callback to a hook function (like beforeEach, beforeAll ) returns certain data types, like an array:

// TypeError: i is not a function
beforeEach(() => [])

// but this  is ok
beforeEach(() => Promise.resolve())

Reproduction

https://github.com/joshnuss/vitest-type-error-repro

System Info

System:
    OS: Linux 5.15 Linux Mint 20.3 (Una)
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 1.33 GB / 15.52 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.15.1 - ~/.asdf/installs/nodejs/16.15.1/bin/node
    npm: 8.11.0 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 105.0.5195.52
    Firefox: 105.0
  npmPackages:
    vitest: ^0.23.4 => 0.23.4

Used Package Manager

pnpm

Validations

@sheremet-va
Copy link
Member

beforeEach expects a function to be returned: https://vitest.dev/api/#beforeeach

@joshnuss
Copy link
Sponsor Contributor Author

beforeEach seems to accept return values like null or Promise.

Two things:

  • If other values aren't allowed, a better error message would be helpful.
  • The error message didn't contain a stack trace, so it made it harder to debug the issue.

@sheremet-va
Copy link
Member

beforeEach seems to accept return values like null or Promise.

cannot reproduce

Screenshot 2022-09-28 at 09 51 56

If other values aren't allowed, a better error message would be helpful.

Agree

The error message didn't contain a stack trace, so it made it harder to debug the issue.

Stacktrace leads to internal implementations - we hide those, because otherwise throwing in a test will be bloated.

@joshnuss
Copy link
Sponsor Contributor Author

joshnuss commented Sep 28, 2022

cannot reproduce

Maybe it's not reproducible with TS, but it is reproducible with JS.

Stacktrace leads to internal implementations - we hide those, because otherwise throwing in a test will be bloated

can you clarify? From a debugging perspective, if something went wrong while invoking beforeEach, it would be helpful to know where the error came from. Usually the stack trace helps with that kind of thing.

@sheremet-va
Copy link
Member

sheremet-va commented Sep 28, 2022

if something went wrong while invoking beforeEach, it would be helpful to know where the error came from. Usually the stack trace helps with that kind of thing.

If something on your side is going wrong, you will see a stack trace to your code. Here the problem is with Vitest internals, so you don't see an error.

@jherdman
Copy link

This is definitely reproducible with TS. I hit the issue in converting a JS-based test to TS. What tripped me up was the Migration guide's recommendation for a done callback: https://vitest.dev/guide/migration.html. If you're not careful you end up with callback that the before hook doesn't like. Perhaps an assertion for the return type with a friendly error message would be useful?

@kswope
Copy link

kswope commented Oct 2, 2022

Same problem here

I think this is hinted at in the docs: Awaitable.

Type: beforeEach(fn: () => Awaitable<void>, timeout?: number)

To be more clear

works

beforeEach(() => Promise.resolve())

doesn't

beforeEach(() => Promise.resolve("hi!"))

probably because of the cleanup function

beforeEach(() => Promise.resolve(() => console.log("cleaning up")))

So I guess Awaitable is either a promise wrapping a function or nothing?

Probably should check if the returned value is a function before it says incoherent things like "TypeError: i is not a function"

@joshnuss
Copy link
Sponsor Contributor Author

joshnuss commented Oct 5, 2022

Thanks @sheremet-va!

@github-actions github-actions bot locked and limited conversation to collaborators Jun 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants