Unit Tests with Flowable Form

Hi everyone,

currently we have a problem with our unit tests in React where we import forms from Flowable. Every component with the following import “import {Form} from '@flowable/forms or @flowable/forms/flowable-forms/src/flw/Form” always throws the following error in the unit tests:

Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:https://jestjs.io/docs/configuration

For information about custom transformations, see:https://jestjs.io/docs/code-transformation

Details:

C:\Users\username\IdeaProjects\react-app\node_modules\@flowable\forms\flowable-forms\src\flw\Form.d.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import React from "react";

SyntaxError: Cannot use import statement outside a module

1 | import { Form } from "@flowable/forms/flowable-forms/src/flw/Form"

Does anyone know why this happens and how to fix it.

Here is our package.json, babel.config.js, tsconfig.json, tsconfig.spec.json and craco.config.js

package.json

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test --watchAll --verbose",
    "eject": "craco eject",
    "codegen": "graphql-codegen --config codegen.ts",
    "storybook": "start-storybook -p 6006 -s public",
    "build-storybook": "build-storybook -s public"
  },
  "dependencies": {
    "@emotion/react": "^11.0.0",
    "@emotion/styled": "^11.0.0",
    "@flowable/forms": "^3.13.0",
    "@mui/icons-material": "^5.11.0",
    "@mui/material": "^5.11.0",
    "@tanstack/react-query": "^4.20.4",
    "@tanstack/react-query-devtools": "^4.20.4",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-router-dom": "^6.4.5",
    "react-scripts": "5.0.1",
    "rxjs": "^7.6.0",
    "sass": "^1.57.1",
    "ts-simple-nameof": "^1.3.1",
    "web-vitals": "^2.1.4"
  },
  "devDependencies": {
    "@babel/core": "^7.20.12",
    "@babel/preset-env": "^7.20.2",
    "@craco/craco": "7.0.0",
    "@craco/types": "^7.0.0",
    "@graphql-codegen/cli": "^2.16.1",
    "@graphql-codegen/client-preset": "^1.2.3",
    "@graphql-codegen/typescript-react-query": "^4.0.6",
    "@storybook/addon-actions": "^6.5.15",
    "@storybook/addon-essentials": "^6.5.15",
    "@storybook/addon-interactions": "^6.5.15",
    "@storybook/addon-links": "^6.5.15",
    "@storybook/builder-webpack5": "^6.5.15",
    "@storybook/manager-webpack5": "^6.5.15",
    "@storybook/node-logger": "^6.5.15",
    "@storybook/preset-create-react-app": "^4.1.2",
    "@storybook/react": "^6.5.15",
    "@storybook/testing-library": "^0.0.13",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^12.1.5",
    "@testing-library/user-event": "12.5.0",
    "@types/jest": "^29.2.6",
    "@types/node": "^18.11.18",
    "@types/react": "^17.0.52",
    "@types/react-dom": "^17.0.18",
    "jest": "^29.3.1",
    "ts-jest": "^29.0.5",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.4",
    "babel-plugin-named-exports-order": "^0.0.2",
    "prop-types": "^15.8.1",
    "webpack": "^5.75.0"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ],
    "overrides": [
      {
        "files": [
          "**/*.stories.*"
        ],
        "rules": {
          "import/no-anonymous-default-export": "off"
        }
      }
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

babel.config.js

module.exports = {
    presets: [
        ['@babel/preset-env', {targets: {node: 'current'}}],
        '@babel/preset-typescript',
    ],
    env: {
        test: {
            plugins: ["@babel/plugin-transform-modules-commonjs"]
        }
    }
};

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext",
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ],
  "extends": "./tsconfig.paths.json"
}

tsconfig.spec.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "out-tsc/spec",
    "types": ["jest"],
    "module": "commonjs",
    "emitDecoratorMetadata": true,
    "allowJs": true
  },
  "include": ["src/**/*.test.ts", "src/**/*.d.ts"]
}

craco.config.js

const path = require('path');

module.exports = {
    webpack: {
        alias: {
            'app': path.resolve(__dirname, 'src/app'),
            'shared': path.resolve(__dirname, 'src/shared'),
        },
    },
    jest: {
        configure: {
            preset: 'ts-jest/presets/js-with-ts',
            testEnvironment: "node",
            globals: {
                'ts-jest': {
                    tsconfig: '<rootDir>/tsconfig.spec.json',
                },
            },
            transform: {
                '^.+\\.(ts|tsx)?$': 'ts-jest',
                "^.+\\.(js|jsx)$": "babel-jest",
            },
            transformIgnorePatterns: ['node_modules/(?!(.*.mjs$)|@flowable/)'],
            verbose: true,
            moduleNameMapper: {
                'app/(.*)': `<rootDir>/src/app/$1`,
                'shared/(.*)': `<rootDir>/src/shared/$1`,
                '@flowable/forms/flowable-forms/src/flw/Form': `<rootDir>/node_modules/@flowable/forms/flowable-forms/src/flw/Form\.d\.ts`,
            }
        }
    },
};

Thank you in advance and I look forward to your answers.

Hey @Fatih,

This looks like a question for our Enterprise Forum. This forum here is for Flowable Open Source.

Cheers,
Filip

P.S. I think that this was a bug that has been fixed in one of our latest enterprise releases. However, to be extra sure, I would suggest that you raise a Service Desk issue to validate this.