Cant resolve "SyntaxError: Unexpected token 'export'" using jest in @nrwl monorepo angular 11

Viewed 598

I am porting an app from an old-style angular repo to @nrwl/nx. I have one app and one lib that gives me issues:

    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { Hash } from './lib/hash';

I am not using hashjs-es directly, it is a dependency of another library.

I have read PAGES on this issue, but nothing seems to work. I am not sure I have the transformIgnorePattern setup right, and I am not a babel pro.

Suggestions for finding the issue?

jest.config.js (in my apps/)

module.exports = {
  displayName: 'nfconsole',
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.(html|svg)$',
      astTransformers: {
        before: [
          'jest-preset-angular/build/InlineFilesTransformer',
          'jest-preset-angular/build/StripStylesTransformer',
        ],
      },
    },
  },
  coverageDirectory: '../../coverage/apps/nfconsole',
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-comment',
  ],
  transform: {
    '^.+\\.(ts|html)$': 'ts-jest',
    '^.+\\.js$': 'babel-jest',
  },
  transformIgnorePatterns: [
    "../../node_modules/(?!hashjs-es).+\\.js$"
  ]
};

.babelrc (in apps/)

{
  "presets": ["babel-preset-env"]
}

tsconfig.spec.ts (in apps/)

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "allowJs": true,
    "outDir": "../../dist/out-tsc",
    "module": "commonjs",
    "types": [
      "jest",
      "node"
    ]
  },
  "files": [
    "src/test-setup.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ],
  "angularCompilerOptions": {
    "enableIvy": false
  }
}

test-setup.js (in apps/src)

import 'jest-preset-angular/setup-jest';

package.json includes

    "babel-jest": "^26.6.3",
    "babel-preset-env": "^1.7.0",
    "@babel/core": "^7.14.3",

the relevant section of angular.json

        "test": {
          "builder": "@nrwl/jest:jest",
          "options": {
            "jestConfig": "apps/<myapp>/jest.config.js",
            "tsConfig": "apps/<myapp>/tsconfig.spec.json",
            "passWithNoTests": true,
            "setupFile": "apps/<myapp>/src/test-setup.ts"
          }
        }
0 Answers
Related