You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to get 100% of coverage in my code and I have to test the repository layer, so:
I have a file called todoRepository.js:
exportdefaultclassTodoRepository{
#schedule
constructor({ db }){//db is an instance from LokiJSthis.#schedule =db.addCollection('schedule')console.log('schedule',this.#schedule)}asynclist(){returnthis.#schedule.find().map(({ meta, $loki, ...result})=>result)}asynccreate(data){const{ $loki, meta, ...result}=this.#schedule.insertOne(data)returnresult}}
And I have my test called todoRepository.test.js:
import{describe,it,beforeEach,afterEach}from'node:test'importTodoRepositoryfrom'../src/todoRepository.js'importassertfrom'node:assert'importsinonfrom'sinon'describe('todoRepository test Suite',()=>{describe('#list',()=>{let_todoRepositorylet_dependenciesletaddCollectionStubconstmockDatabase=[{text: 'I must plan my trip to Europe',when: newDate('2021-03-22T00:00:00.000Z'),status: 'late',id: '31f1825e-c8e3-4098-ae8b-47b1da426442',meta: {revision: 0,created: 1693765894381,version: 0},'$loki': 3}]beforeEach((context)=>{_dependencies={db: {addCollection: addCollectionStub=sinon.stub(),find: context.mock.fn(async()=>mockDatabase)}}_todoRepository=newTodoRepository(_dependencies)})afterEach(()=>{addCollectionStub.restore();})it('should return a list of items',async()=>{constexpected=[{text: 'I must plan my trip to Europe',when: newDate('2021-03-22T00:00:00.000Z'),status: 'late',id: '31f1825e-c8e3-4098-ae8b-47b1da426442'}]constresult=await_todoRepository.list()assert.deepStrictEqual(result,expected)})})})
But when I tried run my tests, I got this error:
todoRepository test Suite ▶ #list ✖ should return a list of items (4.074102ms) TypeError [Error]: Cannot read properties of undefined (reading 'find') at TodoRepository.list (file:///home/vitor.duda/Desktop/estudos/mocking-with-node-test-runner-template/src/todoRepository.js:9:31) at TestContext.<anonymous> (file:///home/vitor.duda/Desktop/estudos/mocking-with-node-test-runner-template/test/todoRepository.test.js:47:44) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:580:25) at async Promise.all (index 0) at async Suite.run (node:internal/test_runner/test:824:7) at async Promise.all (index 0) at async Suite.run (node:internal/test_runner/test:824:7) at async startSubtest (node:internal/test_runner/harness:204:3)
But I didn't understand it because I'm mocking the return value from find with:
find: context.mock.fn(async()=>mockDatabase)
The line db.addCollection('schedule') returns a class LokiEventEmitter, such as:
I believe that I'm getting this error due to method find is outside from addCollection. I think I have to create of a mock from LokiEventEmitter class with the find method, but I don't know how to do it.
Node.js version
20.5.1
Example code
No response
Operating system
Linux 6.2.0-31-generic #31~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Aug 16 13:45:26 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Scope
Running tests with native test runner
Module and version
Not applicable.
The text was updated successfully, but these errors were encountered:
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
Details
I'm trying to get 100% of coverage in my code and I have to test the repository layer, so:
I have a file called
todoRepository.js
:And I have my test called
todoRepository.test.js
:But when I tried run my tests, I got this error:
But I didn't understand it because I'm mocking the return value from find with:
The line
db.addCollection('schedule')
returns a classLokiEventEmitter
, such as:I believe that I'm getting this error due to method
find
is outside fromaddCollection
. I think I have to create of a mock fromLokiEventEmitter
class with thefind
method, but I don't know how to do it.Node.js version
20.5.1
Example code
No response
Operating system
Linux 6.2.0-31-generic #31~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Aug 16 13:45:26 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Scope
Running tests with native test runner
Module and version
Not applicable.
The text was updated successfully, but these errors were encountered: