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

Native Test Runner: Error to mocking "find" method from "db.addCollection('schedule')" of LokiJS (1.15.12) #4239

Closed
vitorvd opened this issue Sep 3, 2023 · 3 comments

Comments

@vitorvd
Copy link

vitorvd commented Sep 3, 2023

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:

export default class TodoRepository {
    #schedule
    constructor({ db }) { //db is an instance from LokiJS
        this.#schedule = db.addCollection('schedule')
        console.log('schedule', this.#schedule)
    }

    async list() {
        return this.#schedule.find().map(({ meta, $loki, ...result }) => result)
    }

    async create(data) {
        const { $loki, meta, ...result} = this.#schedule.insertOne(data)
        return result
    }
}

And I have my test called todoRepository.test.js:

import { describe, it, beforeEach, afterEach } from 'node:test'
import TodoRepository from '../src/todoRepository.js'
import assert from 'node:assert'
import sinon from 'sinon'

describe('todoRepository test Suite', () => {
  describe('#list', () => {
    let _todoRepository
    let _dependencies
    let addCollectionStub

    const mockDatabase = [
      {
        text: 'I must plan my trip to Europe',
        when: new Date('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 = new TodoRepository(_dependencies)
    })

    afterEach(() => {
      addCollectionStub.restore();
    })

    it('should return a list of items', async () => {
      const expected = [
        {
          text: 'I must plan my trip to Europe',
          when: new Date('2021-03-22T00:00:00.000Z'),
          status: 'late',
          id: '31f1825e-c8e3-4098-ae8b-47b1da426442'
        }
      ]
     
      const result = 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:

LokiEventEmitter {
  name: 'schedule',
  data: [],
  idIndex: null,
  binaryIndices: {},
  constraints: { unique: {}, exact: {} },
  uniqueNames: [],
  transforms: {},
  objType: 'schedule',
  dirty: true,
  cachedIndex: null,
  cachedBinaryIndex: null,
  cachedData: null,
  adaptiveBinaryIndices: true,
  transactional: false,
  cloneObjects: false,
  cloneMethod: 'parse-stringify',
  asyncListeners: false,
  disableMeta: false,
  disableChangesApi: true,
  disableDeltaChangesApi: true,
  autoupdate: false,
  serializableIndices: true,
  disableFreeze: true,
  ttl: { age: null, ttlInterval: null, daemon: null },
  maxId: 0,
  DynamicViews: [],
  events: {
    insert: [],
    update: [],
    'pre-insert': [],
    'pre-update': [],
    close: [],
    flushbuffer: [],
    error: [],
    delete: [ [Function: deleteCallback] ],
    warning: [ [Function (anonymous)] ]
  },
  changes: [],
  dirtyIds: [],
  observerCallback: [Function: observerCallback],
  getChangeDelta: [Function: getChangeDelta],
  getObjectDelta: [Function: getObjectDelta],
  getChanges: [Function (anonymous)],
  flushChanges: [Function: flushChanges],
  setChangesApi: [Function (anonymous)],
  isIncremental: undefined
}

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.

@preveen-stack
Copy link
Contributor

Not able to understand the reason for the error.
https://www.sammeechward.com/mocking-a-database-with-jest-in-javascript

Copy link

github-actions bot commented May 7, 2024

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.

@github-actions github-actions bot added the stale label May 7, 2024
Copy link

github-actions bot commented Jun 7, 2024

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.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2024
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