Jest-preset-angular: TypeError: (options.astTransformers || []).map is not a function angular 10

Created on 14 Sep 2020  ·  14Comments  ·  Source: thymikee/jest-preset-angular

🐛 Bug Report

I have a unit test running using the

"jest": "^26.4.2",
"jest-preset-angular": "^8.3.1",
When I run the command npm run test:app it was working fine in Angular 8, now I have updated the angular to the latest version 10. I am getting an error as below on every test component

To Reproduce

Error log:

# content of error stacktrace :
TypeError: (options.astTransformers || []).map is not a function

  at ConfigSet.get (node_modules/ts-jest/dist/config/config-set.js:225:64)
  at ConfigSet.tsJest (node_modules/ts-jest/dist/util/memoize.js:43:24)
  at ConfigSet.get (node_modules/ts-jest/dist/config/config-set.js:297:41)
  at ConfigSet.versions (node_modules/ts-jest/dist/util/memoize.js:43:24)
  at ConfigSet.get (node_modules/ts-jest/dist/config/config-set.js:588:32)
  at ConfigSet.jsonValue (node_modules/ts-jest/dist/util/memoize.js:43:24)
  at ConfigSet.get [as cacheKey] (node_modules/ts-jest/dist/config/config-set.js:603:25)
  at TsJestTransformer.getCacheKey (node_modules/ts-jest/dist/ts-jest-transformer.js:126:36)

envinfo

System:
    OS: Mac os Catalina

Npm packages:
    "jest": "^26.4.2",
    "jest-preset-angular": "^8.3.1",
    "typescript": "~4.0.2"

describe('DateSelectionComponent', () => {
let component: DateSelectionComponent;
let fixture: ComponentFixture;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [DateSelectionComponent, SafePipe],
      imports: [
        SharedModule,
        NgReduxTestingModule,
        RouterTestingModule.withRoutes([])
      ],
      providers: [
        {
          provide: PageAPIActions, useValue: { setPageState() { } }
        }
      ]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DateSelectionComponent);
    component = fixture.componentInstance;
    Object.defineProperties(component, {
      page$: {
        value: of('value'),
        writable: true
      },
      page: {
        value: { destination: 'value' },
        writable: true
      },
      startDate: {
        value: new NgbDate(2019, 2, 27),
        writable: true
      },
      minDate: {
        value: new NgbDate(2019, 2, 27),
        writable: true
      }
    });
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('Should Match Snapshot', async () => {
    (expect(fixture) as any).toMatchSnapshot();
  });
});

Jest.app config

const baseConfig = require('./jest.base.config');

module.exports = {
    ...baseConfig,
    roots: ['<rootDir>/src'],
    modulePaths: ['<rootDir>/dist'],
    modulePathIgnorePatterns: ['<rootDir>/src/app/shared'],
    moduleNameMapper: {
        "^@pts-ngx/core": "<rootDir>/dist/pts-ngx/core",
        "^@pts-ngx/core/(.*)$": "<rootDir>/dist/pts-ngx/core/$1",
        'projects/pts-ngx/core/(.*)$': '<rootDir>/projects/pts-ngx/core/$1'
    }
};

Jest base config

module.exports = {
    preset: 'jest-preset-angular',
    setupFilesAfterEnv: ['<rootDir>/setupJest.ts'],
    moduleNameMapper: {
        "^@pts-ngx/core": "<rootDir>/dist/pts-ngx/core",
        "^@pts-ngx/core/(.*)$": "<rootDir>/dist/pts-ngx/core/$1"
    }
};

Jest.library config

// jest.lib.config.js
const baseConfig = require('./jest.base.config');

module.exports = {
    ...baseConfig,
    roots: ['<rootDir>/projects'],
};
Not An Issue

All 14 comments

Hi, what is the version of ts-jest in your lock file ? Would you please clean jest cache and try again too ?

ts-jest
I can see these version, I tried by cleaning the cache, but it didn't work

"jest-preset-angular": {
      "version": "8.3.1",
      "resolved": "https://registry.npmjs.org/jest-preset-angular/-/jest-preset-angular-8.3.1.tgz",
      "integrity": "sha512-WhQm+0tXzikkvIF35XErV87DqYbieZI0R0MIGgn7VE/DDhdbXFDmYQxFZ7w4qcA0cGLLvSMTBbIsUtHeE/rFxQ==",
      "requires": {
        "pretty-format": "26.x",
        "ts-jest": "26.x"
      },
"jest-preset-angular": {
          "version": "7.1.1",
          "resolved": "https://registry.npmjs.org/jest-preset-angular/-/jest-preset-angular-7.1.1.tgz",
          "integrity": "sha512-/uJUi9IHoCxtB6aH6I+llpHjyfGfP2q32lcBCUKKk16v1uYKXvj1Nfdlvfvu/eqjZVoKEDLP+Ejup4bbsc0gAA==",
          "dev": true,
          "requires": {
            "jest-environment-jsdom-thirteen": "^1.0.0",
            "pretty-format": "^24.0.0",
            "ts-jest": "^24.0.0"
          }

I would recommend to do these steps:

  • Uninstall jest-preset-angular

  • Reinstall jest-preset-angular

  • Clear jest cache

  • Retry

Would you please do these steps to see if it helps ?

I am facing the same issue after I ran the command rm -rf node_modules && rm ./package-lock.json && npm install I followed the step as you have mentioned in the above comment but not working.

have you clear jest cache ?

In the script of the package.json I have added

"scripts": {
"clear_jest": "jest --clearCache"
  }

I got an error as below

npm run clear_jest

> [email protected] clear_jest /Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp
> jest --clearCache

● Validation Error:

  Module <rootDir>/src/setupJest.ts in the setupFilesAfterEnv option was not found.
         <rootDir> is: /Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html

I am on a mac machine, can you please tell me how can I clear the jest cache

The error is saying about

Module <rootDir>/src/setupJest.ts in the setupFilesAfterEnv option was not found.
         <rootDir> is: /Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp

This means your setupJest.ts cannot be found by jest. You should check your jest config setupFilesAfterEnv to see if you point to the correct path.

I am able to run that command successfully, still, I am facing the same issue options.astTransformers || []).map

npm run clear_jest

> [email protected] clear_jest /Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp
> jest --clearCache

Cleared /private/var/folders/dh/r0v3cpq168x9bjxp1mjrrgxw0000gn/T/jest_dx

what is your jest config ? I think the version of ts-jest in your lock file is still not correct. You can explicitly install ts-jest by

npm install --dev ts-jest

I have the three configuration

Jest.app.config.js

const baseConfig = require('./jest.base.config');

module.exports = {
    ...baseConfig,
    roots: ['<rootDir>/src'],
    modulePaths: ['<rootDir>/dist'],
    modulePathIgnorePatterns: ['<rootDir>/src/app/shared'],
    moduleNameMapper: {
        "^@ngx/core": "<rootDir>/dist/ngx/core",
        "^@ngx/core/(.*)$": "<rootDir>/dist/ngx/core/$1",
        'projects/ngx/core/(.*)$': '<rootDir>/projects/ngx/core/$1'
    }
};

Jest.base.config.js

module.exports = {
    preset: 'jest-preset-angular',
    setupFilesAfterEnv: ['<rootDir>/setupJest.ts'],
    moduleNameMapper: {
        "^@ngx/core": "<rootDir>/dist/ngx/core",
        "^@ngx/core/(.*)$": "<rootDir>/dist/pts-ngx/core/$1"
    }
};

Jest.lib.config.js

const baseConfig = require('./jest.base.config');

module.exports = {
    ...baseConfig,
    roots: ['<rootDir>/projects'],
};

The config looks fine, maybe you should try installing explicitly ts-jest.

Thanks for that, test cases are working, but I got another error now

Test Suites: 20 passed, 20 total
Tests:       26 passed, 26 total
Snapshots:   2 obsolete, 5 passed, 5 total
Time:        80.053 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test:app: `jest --config ./jest.app.config.js --no-cache`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] test:app script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/macbook/.npm/_logs/2020-09-15T13_55_27_962Z-debug.log

Error from that log file

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'test:app' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'pretest:app', 'test:app', 'posttest:app' ]
5 info lifecycle [email protected]~pretest:app: [email protected]
6 info lifecycle [email protected]~test:app: [email protected]
7 verbose lifecycle [email protected]~test:app: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~test:app: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp/node_modules/.bin:/usr/local/opt/helm@2/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Xamarin Workbooks.app/Contents/SharedSupport/path-bin
9 verbose lifecycle [email protected]~test:app: CWD: /Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp
10 silly lifecycle [email protected]~test:app: Args: [ '-c', 'jest --config ./jest.app.config.js --no-cache' ]
11 silly lifecycle [email protected]~test:app: Returned: code: 1  signal: null
12 info lifecycle [email protected]~test:app: Failed to exec test:app script
13 verbose stack Error: [email protected] test:app: `jest --config ./jest.app.config.js --no-cache`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:314:20)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:314:20)
13 verbose stack     at maybeClose (internal/child_process.js:1051:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5)
14 verbose pkgid [email protected]
15 verbose cwd /Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp
16 verbose Darwin 19.6.0
17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test:app"
18 verbose node v14.5.0
19 verbose npm  v6.14.8
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] test:app: `jest --config ./jest.app.config.js --no-cache`
22 error Exit status 1
23 error Failed at the [email protected] test:app script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

I have the node version - 14.5.0
Angular version - 10

this error is not jest-preset-angular error, this is from jest. I would suggest you to Google this or ask in Jest discord channel. You can find jest Discord channel also via Google.

Seems to be a newer version of the package causing the issue.

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

Was this page helpful?
0 / 5 - 0 ratings