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

node:test with typescript, code coverage and typing for mock #4325

Closed
MorningLightMountain713 opened this issue Jan 12, 2024 · 6 comments
Closed

Comments

@MorningLightMountain713
Copy link

MorningLightMountain713 commented Jan 12, 2024

Details

Just FYI, testing works fine when using the following:

node --import tsx  --test test/myfile.ts

Problem 1

When trying to use code coverage with node --import tsx --experimental-test-coverage --test test/myfile.ts

I get the following warning (and no code coverage):

ℹ Warning: Could not report code coverage. TypeError: Cannot read properties of undefined (reading 'line')

What can I do to get code coverage?

Problem 2

Whenever I spy in the ts file, intellisense gives warnings about type missing for mock. Eg:

    t.mock.method(serverFromOtherTsFile, "myMockedMethod");

    assert.strictEqual(serverFromOtherTsFile.myMockedMethod.mock.calls.length, 0);
    await serverFromOtherTsFile.myMockedMethod();
    assert.strictEqual(serverFromOtherTsFile.myMockedMethod.mock.calls.length, 1);

The mock property doesn't exist on type myMockedMethod. How can I type this? For clarity, the tests still run, it's just in VSCode I get the warnings as mock doesn't exist on myMockedMethod.

Thanks for your help

Node.js version

v20.10.0

Example code

No response

Operating system

macOS

Scope

code / runtime

Module and version

Not applicable.

@MorningLightMountain713
Copy link
Author

I have ended up doing the following:

assert.strictEqual((serverFromOtherTsFile.myMockedMethod as any).mock.calls.length, 0);

I don't like it, but it lets me run tsc without throwing errors

@NikitaIT
Copy link

NikitaIT commented Apr 5, 2024

ℹ Warning: Could not report code coverage. TypeError: Cannot read properties of undefined (reading 'line')

test.ts

import { a } from "./a.ts";

a.ts

// no imports <-- TypeError: Cannot read properties of undefined (reading 'line')
export function a(): string {
  return "a";
}

@liza-unito
Copy link

+1 also having this issue. (... as any) does work but... it's not the prettiest solution

@RedYetiDev
Copy link
Member

Fixed by nodejs/node#53000

@RedYetiDev
Copy link
Member

Keep in mind the coverage won't exactly be useful in the transpiled code, see nodejs/node#52775 (comment)

@khaosdoctor
Copy link

Btw, if someone is having the same thing, please first check if the transpiler (if you're using any) provides an inline source map generation for the code, the underlying problem in that case is that the generated transpiled code does not map to anything in the original code, hence there's no way to tell which function is which and the code coverage won't work properly.

Weird thing though, if the code is simple enough, the coverage works, even with transpilers

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

5 participants