Error following unit testing guide

System Information
  • Strapi Version: 4.15.4, 4.15.2, 4.10.8:
  • Operating System: MacOS 13.0:
  • Database: PosgreSQL / sqlite3:
  • Node Version: v20.5.1:
  • NPM Version: 9.8.0:
  • Yarn Version:

Hello,
I followed the guide at Testing | Strapi Documentation up to and including the headline Test a Strapi Instance Testing | Strapi Documentation . When running the test, I get the following error:

During setup I received the error:

TypeError: Cannot destructure property ‘client’ of ‘db.config.connection’ as it is undefined.

And during cleanUp I received the error:

TypeError: Cannot read properties of undefined (reading ‘connection’)

During development, I am using postgres, however, the test database configuration is set to sqlite according to the guide. I am using TypeScript. The errors were observed with strapi version 4.15.2, 4.15.4 and 4.10.5.

Does anyone know how to fix this?

1 Like

I was not able to fix the problem with a strapi setup using typescript. Among other things, I tried out using better-sqlite3 instead of sqlite3 and I followed the resources Installation | ts-jest and Jest - TypeScript Deep Dive to use ts-jest.

I switched to javascript instead of typescript and now it works.

I gave it another try and the fix was embarrassingly simple. I checked at TypeScript | Strapi Documentation and found that the code from Testing | Strapi Documentation had to be adjusted in that line 8 of tests/helpers/strapi.js needs to be changed from
await Strapi().load();
to
await Strapi({ distDir: "./dist" }).load();.

1 Like

Hi
Thanks for the tip, I really struggled to set up tests with strapi, as the doc only shows js and I’m using typescript,
and the solution works but you have to rebuild each time in case of change in the api to have the js code up to date with the ts code,
by doing,
Strapi.compile().then((appContext) => Strapi(appContext).load());
the strapi will compile every time you run the test, so you don’t have to build each time
hoping this will help

1 Like

You are so amazing. I spent like 3 hours after trying to read the official guide trying to figure out why it wasn’t working. I originally had the config/env/test/database.js file like the original tutorial stated, but my middleware wasn’t loaded, so went down a path trying to enable typescript in jest (still not a fun activity) only to start adding console.log to the internals.

Long story short, I was so stuck because strapi only loads .js for config files

then i found this post

<3 so much man thanks