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

Function calls that doesn't match signatures result in test success #5628

Closed
6 tasks done
treeindev opened this issue Apr 26, 2024 · 4 comments
Closed
6 tasks done

Function calls that doesn't match signatures result in test success #5628

treeindev opened this issue Apr 26, 2024 · 4 comments

Comments

@treeindev
Copy link

Describe the bug

I have noticed that writing a test case which contains a call to a method or function with invalid parameters, does not result in a test failure. Consider the following example using Typescript:

// test.test.ts
import { test, expect, vi } from "vitest";

const function2 = vi.fn((a, b) => {})
const function1 = (param1: number, param2: number) => {
  function2(param1, param2);
  return "done";
}

test("That calls that don't match signatures are detected.", () => {
  const result = function1();
  
  expect(result).toBe("done");
  expect(function2).toHaveBeenCalledOnce();
});

example2

In this scenario, function1() requires two parameter of type number that will be sent over to function2(). As its internal logic does directly interact with the parameters, no exceptions are thrown during executor. So both expect are a success and the test case passes.

I understand in plain JS, function1 would receive both parameters as undefined. But this being a TypeScript file, wouldn't the internal test compiler throw an error that result in a test failure?

The error is indeed highlighted is you use an IDE that supports TS checks:

example

IMO the use of an invalid method signature should not result in a valid test case.

Reproduction

See description above with example.

System Info

Using vitest 1.2.2

Used Package Manager

npm

Validations

@AriPerkkio
Copy link
Member

How would you detect function arguments in Javascript?

I would recommend to use toHaveBeenCalledWith in addition here. Or even combine them into toHaveBeenNthCalledWith(1, ...).

@treeindev
Copy link
Author

@AriPerkkio I think the argument detection should happen when the TypeScript code gets transpiled to JS. In this scenario, if a .ts file contains any invalid syntax the whole test suit described on that file could be marked as failure.

I agree with you that using toHaveBeenCalledWith and toHaveBeenNthCalledWith can be used for the case of my example. But the issue I mentioned can be reproduced, i.e., while creating a new class instance and not sending the correct parameters to the constructor.

@AriPerkkio
Copy link
Member

Vite doesn't do Typechecking. It uses ESBuild which simply strips out the types.

Sounds like you would want to run tsc against the test files.

@treeindev
Copy link
Author

@AriPerkkio If it's expected for Vite not to check types, then this issue can be closed.
Thanks for your replies!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants