question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test with Jest?

See original GitHub issue

Hi,

I’m trying to figure out how to write tests using this library. I looked at fake-indexeddb/auto and while this seems to work partly I end up with an instance of idb that doesn’t have a put method. I looked at the mocha tests included in the lib… but I didn’t understand them – they seem to have been written without mocking IndexedDB at all?

Here’s what I tried:

import actions from "../actions";
import IndexedDB from "fake-indexeddb/auto";
import { openDB } from "idb/with-async-ittr-cjs"; 

class DbService {
  instance = void 0;
  async initialize() {
    this.instance = await openDB("MNR", 2, {
    ...
    })
  }
};
const db = new DbService();
db.initialize();
test("IDB_resetBusy to call put", async () => {
    await db.initialize();
    db.instance.put = jest.fn();
    await actions.IDB_resetBusy("blah");
    expect(db.instance.put).toHaveBeenCalledTimes(1);
  });

I get the error:

TypeError: Cannot read property 'put' of undefined

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jakearchibaldcommented, Mar 25, 2020

Mocha runs in the browser

1reaction
taxiliancommented, Sep 30, 2021

While it would be good to do real tests with karma or similar, there are definitely advantages to testing with something like jest or mocha as well using mocks. I have unit tests working with jest, fake-indexeddb, and idb by doing the following:

  • Make sure you’re using the cjs import if you are using async iterators – i.e. import * as idb from 'idb/with-async-ittr-cjs'; because with-async-ittrdoesn’t work in node, at least not from normal files (probably is a way, I’m too lazy to track it down)
  • In your jest.config.js add "fake-indexeddb/auto" to the setupFiles, e.g.:
module.exports = {
  preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
  transform: {
    '^.+\\.vue$': 'vue-jest'
  },
  setupFiles: [
    "fake-indexeddb/auto"
  ]
}

Beyond that things Just Work so far. Remember it’s a mock so you should do real integration tests as well, but for checking application logic this runs much faster than something requiring a browser, even a headless one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest · Delightful JavaScript Testing
Jest is a JavaScript testing framework designed to ensure correctness of any JavaScript codebase. It allows you to write tests with an approachable, ......
Read more >
Jest Tutorial for Beginners: Getting Started With JavaScript ...
Time to create your first Jest test. Open up filterByTerm.spec.js and create a test block: describe("Filter function" ...
Read more >
Jest Testing Tutorial: 5 Easy Steps - Testim Blog
1. Install Jest Globally · 2. Create a Sample Project · 3. Add Jest to the Project · 4. Write Your First Test...
Read more >
Jest Tutorial - JavaScript Unit Testing Using Jest Framework
Jest is a Javascript Testing framework built by Facebook. It is primarily designed for React (which is also built by Facebook) based apps...
Read more >
Testing with Jest - GeeksforGeeks
Install Jest using npm: · Project Structure: In the project root directory, make a tests folder. This folder will store all the test...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found