How to test with Jest?
See original GitHub issueHi,
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:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Mocha runs in the browser
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:
import * as idb from 'idb/with-async-ittr-cjs';
becausewith-async-ittr
doesn’t work in node, at least not from normal files (probably is a way, I’m too lazy to track it down)"fake-indexeddb/auto"
to the setupFiles, e.g.: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.