Error on unit test while compile Strapi

System Information
  • Strapi Version: v4.7.0
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

Hi !
The context : strapi 4.7.0 with typescript and jest.

I have a plugin in typescript generated with strapi cli command, so I have a strapi-server.js file with

module.exports = require('./dist/server');

A the root of my project I created a tests folder with an helper with a setup strapi method :

export async function setupStrapi() {
  if (!instance) {
    try {
      await strapiO.compile();
      await Strapi({
        appDir: process.cwd(),
        distDir: process.cwd() + "/dist",
        autoReload: false,
        serveAdminPanel: false,
    } catch (er) {
      console.log("er", er);
    await waitForServer();

    instance = strapi;
  return instance;

Here is my jest config :

/** @type {import('ts-jest').JestConfigWithTsJest} */
const { defaults: tsjPreset } = require('ts-jest/presets')
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  transform: {
    '^.+\\.[tj]sx?$': [
        tsconfig: 'tsconfig.test.json',
  testTimeout: 15000

And my tsconfig.test.json :

  "extends": "@strapi/typescript-utils/tsconfigs/server",
  "compilerOptions": {
    "rootDir": ".",
    "allowJs": true
  "exclude": [
  "include": ["./", "src/**/*.json"]

My problem : when I run yarn test I have this problem :

Could not load js config file /src/plugins/foo-feature/strapi-server.js: Cannot find module './dist/server' from 'src/plugins/foo-feature/strapi-server.js'

I don’t know how to build the dist folder of my plugin while testing.
Any ideas please ?

You would have to do the strapi develop commando to your plugin before you start testing there are hooks in jest do do this

1 Like