Tried to add jest to my strapi ts project but it has problems with load the routes in the test environment

System Information
  • Strapi Version: “@strapi/strapi”: “4.5.5”,
  • Operating System: macos
  • Database: sqlite
  • Node Version: 16.19.0
  • NPM Version:
  • Yarn Version:
added jest test to my existing and running typescript strapi project.

``` strapi.js
const Strapi = require('@strapi/strapi');
const fs = require('fs');

let instance;

async function setupStrapi() {
	if (!instance) {
		/** the following code in copied from `./node_modules/strapi/lib/Strapi.js` */
		await Strapi().load();

		instance = strapi; // strapi is global now

		await instance.server
			.use(instance.server.router.routes()) // populate KOA routes
			.use(instance.server.router.allowedMethods()); // populate KOA methods

		await instance.server.mount();
	return instance;

On test start i got:

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

   7 |      if (!instance) {
   8 |              /** the following code in copied from `./node_modules/strapi/lib/Strapi.js` */
>  9 |              await Strapi().load();
     |              ^
  10 |
  11 |              instance = strapi; // strapi is global now
  12 |

  at node_modules/@strapi/strapi/lib/services/server/register-routes.js:98:21
  at node_modules/lodash/lodash.js:4967:15
  at baseForOwn (node_modules/lodash/lodash.js:3032:24)
  at node_modules/lodash/lodash.js:4936:18
  at Function.forEach (node_modules/lodash/lodash.js:9410:14)
  at registerAPIRoutes (node_modules/@strapi/strapi/lib/services/server/register-routes.js:94:7)
  at Object.<anonymous>.module.exports (node_modules/@strapi/strapi/lib/services/server/register-routes.js:25:3)
  at Object.initRouting (node_modules/@strapi/strapi/lib/services/server/index.js:96:13)
  at Strapi.bootstrap (node_modules/@strapi/strapi/lib/Strapi.js:465:23)
  at Strapi.load (node_modules/@strapi/strapi/lib/Strapi.js:478:5)
  at setupStrapi (src/tests/helpers/strapi.js:9:3)
  at Object.<anonymous> (src/tests/app.test.js:5:2)

The routes are undefined here :

```register-routes.js from strapi package
 * Register api routes
 * @param {import('../../').Strapi} strapi
const registerAPIRoutes = (strapi) => {
  for (const apiName of Object.keys(strapi.api)) {
    const api = strapi.api[apiName];

    const generateRouteScope = createRouteScopeGenerator(`api::${apiName}`);

    _.forEach(api.routes, (router) => {
      // TODO: remove once auth setup
      // pass meta down to compose endpoint
      router.type = 'content-api';
      router.routes.forEach((route) => {
        generateRouteScope(route); = { apiName };

      return strapi.server.routes(router);

Any help is welcome. Thanks

is undefined

After looking more deeply and nightly in it. i found out, that it was some kind of typescript / jest issue!

I configured my ts files according to strapi-ts-codesandbox - CodeSandbox and everything works like a charm.

Hope this helps

Hello, this Sandbox is not working anymore. Is there an any change to share your test suit? I am facing with the exact same issue you were facing with.