Skip to content

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

@MorningLightMountain713

Description

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    answered🎉 Yay! The issue has been resolved, or the question has been answered.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions