typicode / lowdb

Simple and fast JSON database
MIT License
21.35k stars 918 forks source link

jest 27.5.1 #532

Closed Bidek56 closed 2 years ago

Bidek56 commented 2 years ago

lowdb 1.0 worked fine with jest, but 3.0 does not seem to work with jest 27.5.1. Does anyone know how to fix it? Thx

My sample 3.0 index.test.ts code:

import { Low, JSONFile } from 'lowdb'
it('test file upload', async () => { 
  const adapter = new JSONFile("db.json")  
  const db = new Low(adapter)  
  console.log("Test:", db)  
})

using jest 27.5.1, I get the following error:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './adapters/JSONFile.js';

I have tried the following babel.config.js:

module.exports = {
    presets: [
      ['@babel/preset-env', {targets: {node: 'current'}}],
      '@babel/preset-typescript',
    ],
  };
sand3r commented 2 years ago

Struggling with the same.

Especially lowdb 3.0.0 (which is purely esm) in combination with jest. I have tried running it with yarn node --experimental-vm-modules $(yarn bin jest) on node 16.x

Bidek56 commented 2 years ago

I have switched to Playwright and it works fine.

low.spec.js

import { test, expect } from '@playwright/test';
import { Low, JSONFile } from 'lowdb'

test('lowdb test', async () => {
  const adapter = new JSONFile("./db.json");
  const db = new Low(adapter);

  await db.read()
  expect(db.data.users).toBeTruthy();
});
sand3r commented 2 years ago

Thanks @Bidek56 , I'm gonna give that a try!

kevinrodriguez-io commented 1 year ago

Playwright is neither working.

Bidek56 commented 1 year ago

This simple example using Node 19.3 and this package.json works fine for me:

{
  "name": "lowdb-test",
  "version": "1.0.0",
  "description": "Test of lowdb and jest",
  "type": "module",
  "scripts": {
    "test": "playwright test",
    "build": "rm -rf dist/ && tsc"
  },
  "author": "Darek",
  "license": "ISC",
  "devDependencies": {
    "@playwright/test": "^1.29.1",
    "@types/lowdb": "^1.0.11",
    "playwright": "^1.29.1",
    "typescript": "^4.9.4"
  },
  "dependencies": {
    "lowdb": "5.0.5"
  }
}

low.spec.js

import { test, expect } from '@playwright/test';
import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'

test('lowdb test', async () => {
  const adapter = new JSONFile("./db.json");
  const db = new Low(adapter);

  await db.read()
  expect(db.data.users).toBeTruthy();
});