admin管理员组

文章数量:1400583

I am getting below error while running jest test cases in Angular. All test suites are failing with this error.

Getting typeError: (options.astTransformers || []).map is not a function while running the test suite in Angular

I am getting below error while running jest test cases in Angular. All test suites are failing with this error.

Getting typeError: (options.astTransformers || []).map is not a function while running the test suite in Angular

Share Improve this question edited Sep 8, 2020 at 7:15 Estus Flask 224k79 gold badges472 silver badges611 bronze badges asked Sep 8, 2020 at 4:18 soul truesoul true 311 silver badge4 bronze badges 3
  • Please refer to this – codemax Commented Sep 8, 2020 at 4:39
  • It's not really clear what you're trying to achieve here. What is the type of options.astTransformers? I suppose it's an array. – Tamás Polgár Commented Sep 8, 2020 at 4:58
  • You made a mistake in configuration. See github./thymikee/jest-preset-angular/issues/215 – Estus Flask Commented Sep 8, 2020 at 6:44
Add a ment  | 

5 Answers 5

Reset to default 3

This solution is for Angular 8/9/10

I would remend doing these steps:

  1. Uninstall jest-preset-angular
  2. Reinstall jest-preset-angular
  3. Clear jest cache
  4. Retry

If the above solution not working, try installing

npm install --dev ts-jest

Fix this error by removing the following line in the jest.config.js file.

module.exports = {
    ...
    passWithNoTests: true,
    projects: '<rootDir>/libs/now-version' // <--- newly added property. should be removed
};

As I found this link on github. https://github./nrwl/nx/issues/3885#issuement-706620382

This error started after updating Angular to 11, and when I generate a new lib inside NX and it ends up adding this new line, whenever a new lib is generated, by the CLI

Seems to be a newer version of the package.

In the package.json I changed the jest-preset-angular from "8.3.2" to "8.2.0" and that error went away.

Updating the jest.config.js as below fixed my issue

module.exports = {
  displayName: 'AppName',
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.(html|svg)$',
    },
  },
  coverageDirectory: '../../coverage/libs/appName',
  transform: {
    '^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
  },
  transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
  snapshotSerializers: [
    'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/serializers/html-ment',
  ],
  testRunner: 'jest-jasmine2'
};

Jest configuration for Angular can be tricky.

I remend you use jest-preset-angular which is a library that you will import into your Angular project that will manage the configuration of Jest for Angular.

This may be related to the update of Jest 27 that deprecated astTransformers as string[].

Configure your Angular project to use Jest 26 with jest-preset-angular and you should be fine

本文标签: