Open willdavies0 opened 1 year ago
cc @nodejs/testing
Untested, but I think you want this:
// mocking a method of the class
mock.method(ClassToMock, 'doSomething', () => 'Hello World')
to be this:
// mocking a method of the class
mock.method(ClassToMock.prototype, 'doSomething', () => 'Hello World')
You can see more mocking examples in https://github.com/nodejs/node/blob/main/test/parallel/test-runner-mocking.js.
Thanks a lot for this @cjihrig Are there plans to add these examples to the docs https://nodejs.org/api/test.html?
There are no plans that I'm aware of. Documentation PRs are always welcome, although I don't know if it makes sense to document every possibility.
I tried using prototype
mentioned above, but got the following error:
TypeError [ERR_INVALID_ARG_VALUE]: The argument 'methodName' must be a method. Received undefined
any other suggestions? @cjihrig
@0618 I have no clue what your code looks like so it's difficult to provide suggestions.
I tried using
prototype
mentioned above, but got the following error:TypeError [ERR_INVALID_ARG_VALUE]: The argument 'methodName' must be a method. Received undefined
any other suggestions? @cjihrig
hey i just deal with similar issue, i think is because the method declared inside your class in an arrow function, maybe can change from
test = () => {}
into function test(){}
Details
I'm trying to write mocks for a class that gets initialised in the file I am testing.
Please see the code example below for a detailed example of the problem. This problem often arises when trying to mock 3rd party packages (eg AWS-SDK) that export there APIs as a class that must be initialised.
Coming from Jest where mocking classes like this is very easy, I feel like I am missing something fundamental in how to use the node test runner.
Any help greatly appreciated!
Node.js version
v20.5.0
Example code
Below there are three 'files'. The Class I wish to mock, a file that creates an instance of the class, and the test file.
ClassToMock.js
fileToTest.js
fileToTest.test.js
Operating system
macOS
Scope
Native Test Runner
Module and version
Not applicable.