# Setup Jest in Angular 14 + Ionic 6 project

**URL:** <https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030>\
**Category:** Ionic Framework\
**Created:** [September 19, 2022, 5:44pm UTC](https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030 "2022-09-19T17:44:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![neil89](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/neil89/32/142505_2.png) [@neil89](https://forum.ionicframework.com/u/neil89)\
**Post date:** [September 19, 2022, 5:44pm UTC](https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030/1 "2022-09-19T17:44:25Z")

</div>

I’m trying to setup Jest test framework into my project which use Angular 14 and Ionic 6, among other plugins which may be conflictive such as firebase and ngrx.

I’ve been following mainly this [Tim Deschryver tutorial](https://timdeschryver.dev/blog/integrate-jest-into-an-angular-application-and-library) and some other, even copying and pasting some code from stack overflow about my Jest testing errors, but nothing works. Even I tried a fresh begin removing all my packages and modifications and starting again, but with no luck.

I have my updated repo in here [GitHub - neil89/igloo](https://github.com/neil89/igloo) (it’s pretty manageable). But as a summary, my main modifications have been these:

**package.json**

```auto
{
  "name": "igloo",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "jest",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^14.0.0",
    "@angular/core": "^14.0.0",
    "@angular/fire": "^7.4.1",
    "@angular/forms": "^14.0.0",
    "@angular/platform-browser": "^14.0.0",
    "@angular/platform-browser-dynamic": "^14.0.0",
    "@angular/router": "^14.0.0",
    "@briebug/jest": "^1.3.1",
    "@ionic/angular": "^6.2.6",
    "@ngrx/effects": "^14.3.1",
    "@ngrx/store": "^14.3.1",
    "firebase": "^9.9.4",
    "rxjs": "~6.6.0",
    "tslib": "^2.2.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.0.0",
    "@angular-eslint/builder": "~13.0.1",
    "@angular-eslint/eslint-plugin": "~13.0.1",
    "@angular-eslint/eslint-plugin-template": "~13.0.1",
    "@angular-eslint/template-parser": "~13.0.1",
    "@angular/cli": "^14.0.0",
    "@angular/compiler": "^14.0.0",
    "@angular/compiler-cli": "^14.0.0",
    "@angular/language-service": "^14.0.0",
    "@types/jest": "^29.0.1",
    "@types/node": "^12.11.1",
    "@typescript-eslint/eslint-plugin": "5.3.0",
    "@typescript-eslint/parser": "5.3.0",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsdoc": "30.7.6",
    "eslint-plugin-prefer-arrow": "1.2.2",
    "jest": "^28.1.3",
    "jest-preset-angular": "^12.2.2",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "typescript": "~4.7.3"
  },
  "jest": {
    "preset": "jest-preset-angular",
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupJest.ts"
    ],
    "globalSetup": "jest-preset-angular/global-setup"
  },
  "description": "An Ionic project"
}

```

**jest.config.js**

```auto
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  globals: {
    'ts-jest': {
      tsconfig: 'tsconfig.spec.json',
      isolatedModules: true,
    },
  },
  moduleNameMapper: {
    '@angular/compiler-cli/ngcc': '<rootDir>/node_modules/@angular/compiler-cli/bundles/ngcc/main-ngcc.js',
  },
  testEnvironment: 'jsdom',
  modulePathIgnorePatterns: ['examples/.*', 'website/.*'],
  snapshotSerializers: [require.resolve('jest-snapshot-serializer-raw')],
  testPathIgnorePatterns: ['/node_modules/', '/examples/', '/e2e/.*/ __tests__', '\\.snap

```

**setupJest.ts**

```auto
import 'jest-preset-angular';

```

One of my simpler failing tests: **app.component.spec.ts**

```auto
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      imports: [
        RouterTestingModule.withRoutes([])
      ],
    }).compileComponents();
  });

  it('should create the app', (() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it('should have menu labels', (() => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const app = fixture.nativeElement;
    const menuItems = app.querySelectorAll('ion-label');
    expect(menuItems.length).toEqual(7);
    expect(menuItems[0].textContent).toContain('Inbox');
    expect(menuItems[1].textContent).toContain('Outbox');
  }));

  it('should have urls', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const app = fixture.nativeElement;
    const menuItems = app.querySelectorAll('ion-item');
    expect(menuItems.length).toEqual(7);
    expect(menuItems[0].getAttribute('ng-reflect-router-link')).toEqual('/folder/Inbox');
    expect(menuItems[1].getAttribute('ng-reflect-router-link')).toEqual('/folder/Outbox');
  });

});

```

And now, my output when I ran `npm test` is next:

```auto
> igloo@0.0.1 test
> jest

Determining test suites to run...
ngcc-jest-processor: running ngcc
 FAIL src/app/fridge/fridge.service.spec.ts
  ● 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:

    /Users/daniel.rodriguez/Documents/Personal/iGloo/igloo/node_modules/firebase/app/dist/index.esm.js:1
    ({"Object.<anonymous>":function(module,exports,require, __dirname,__ filename,jest){import { registerVersion } from '@firebase/app';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at node_modules/@angular/fire/bundles/angular-fire.umd.js:2:111
      at Object.<anonymous> (node_modules/@angular/fire/bundles/angular-fire.umd.js:5:2)

 FAIL src/app/fridge/fridge.component.spec.ts
  ● 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:

    /Users/daniel.rodriguez/Documents/Personal/iGloo/igloo/node_modules/firebase/app/dist/index.esm.js:1
    ({"Object.<anonymous>":function(module,exports,require, __dirname,__ filename,jest){import { registerVersion } from '@firebase/app';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at node_modules/@angular/fire/bundles/angular-fire.umd.js:2:111
      at Object.<anonymous> (node_modules/@angular/fire/bundles/angular-fire.umd.js:5:2)

 FAIL src/app/folder/folder.page.spec.ts
  ● FolderPage › should create

    Need to call TestBed.initTestEnvironment() first

       9 |
      10 | beforeEach(async () => {
    > 11 | await TestBed.configureTestingModule({
         | ^
      12 | declarations: [FolderPage],
      13 | imports: [IonicModule.forRoot(), RouterModule.forRoot([])]
      14 | }).compileComponents();

      at TestBedRender3.get compiler [as compiler] (node_modules/@angular/core/fesm2020/testing.mjs:26367:19)
      at TestBedRender3.configureTestingModule (node_modules/@angular/core/fesm2020/testing.mjs:26290:14)
      at Function.configureTestingModule (node_modules/@angular/core/fesm2020/testing.mjs:26126:30)
      at src/app/folder/folder.page.spec.ts:11:19
      at node_modules/tslib/tslib.js:118:75
      at Object.__awaiter (node_modules/tslib/tslib.js:114:16)
      at Object.<anonymous> (src/app/folder/folder.page.spec.ts:10:25)

  ● FolderPage › should create

    zone-testing.js is needed for the fakeAsync() test helper but could not be found.
            Please make sure that your environment includes zone.js/testing

      at resetFakeAsyncZone (node_modules/@angular/core/fesm2020/testing.mjs:273:11)
      at Object.<anonymous> (node_modules/@angular/core/fesm2020/testing.mjs:26603:13)

 FAIL src/app/app.component.spec.ts
  ● AppComponent › should create the app

    Need to call TestBed.initTestEnvironment() first

       7 |
       8 | beforeEach(async () => {
    > 9 | await TestBed.configureTestingModule({
         | ^
      10 | declarations: [
      11 | AppComponent
      12 | ],

      at TestBedRender3.get compiler [as compiler] (node_modules/@angular/core/fesm2020/testing.mjs:26367:19)
      at TestBedRender3.configureTestingModule (node_modules/@angular/core/fesm2020/testing.mjs:26290:14)
      at Function.configureTestingModule (node_modules/@angular/core/fesm2020/testing.mjs:26126:30)
      at src/app/app.component.spec.ts:9:19
      at node_modules/tslib/tslib.js:118:75
      at Object.__awaiter (node_modules/tslib/tslib.js:114:16)
      at Object.<anonymous> (src/app/app.component.spec.ts:8:25)

  ● AppComponent › should create the app

    zone-testing.js is needed for the fakeAsync() test helper but could not be found.
            Please make sure that your environment includes zone.js/testing

      at resetFakeAsyncZone (node_modules/@angular/core/fesm2020/testing.mjs:273:11)
      at Object.<anonymous> (node_modules/@angular/core/fesm2020/testing.mjs:26603:13)

  ● AppComponent › should have menu labels

    NG0908: In this configuration Angular requires Zone.js

      25 |
      26 | it('should have menu labels', (() => {
    > 27 | const fixture = TestBed.createComponent(AppComponent);
         | ^
      28 | fixture.detectChanges();
      29 | const app = fixture.nativeElement;
      30 | const menuItems = app.querySelectorAll('ion-label');

      at new NgZone (node_modules/@angular/core/fesm2020/core.mjs:26180:19)
      at R3TestBedCompiler.compileTestModule (node_modules/@angular/core/fesm2020/testing.mjs:25851:24)
      at R3TestBedCompiler.finalize (node_modules/@angular/core/fesm2020/testing.mjs:25419:14)
      at TestBedRender3.get testModuleRef [as testModuleRef] (node_modules/@angular/core/fesm2020/testing.mjs:26377:49)
      at TestBedRender3.inject (node_modules/@angular/core/fesm2020/testing.mjs:26300:29)
      at TestBedRender3.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26340:44)
      at Function.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26179:37)
      at Object.<anonymous> (src/app/app.component.spec.ts:27:29)

  ● AppComponent › should have menu labels

    zone-testing.js is needed for the fakeAsync() test helper but could not be found.
            Please make sure that your environment includes zone.js/testing

      at resetFakeAsyncZone (node_modules/@angular/core/fesm2020/testing.mjs:273:11)
      at Object.<anonymous> (node_modules/@angular/core/fesm2020/testing.mjs:26603:13)

  ● AppComponent › should have urls

    NG0908: In this configuration Angular requires Zone.js

      35 |
      36 | it('should have urls', () => {
    > 37 | const fixture = TestBed.createComponent(AppComponent);
         | ^
      38 | fixture.detectChanges();
      39 | const app = fixture.nativeElement;
      40 | const menuItems = app.querySelectorAll('ion-item');

      at new NgZone (node_modules/@angular/core/fesm2020/core.mjs:26180:19)
      at R3TestBedCompiler.compileTestModule (node_modules/@angular/core/fesm2020/testing.mjs:25851:24)
      at R3TestBedCompiler.finalize (node_modules/@angular/core/fesm2020/testing.mjs:25419:14)
      at TestBedRender3.get testModuleRef [as testModuleRef] (node_modules/@angular/core/fesm2020/testing.mjs:26377:49)
      at TestBedRender3.inject (node_modules/@angular/core/fesm2020/testing.mjs:26300:29)
      at TestBedRender3.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26340:44)
      at Function.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26179:37)
      at Object.<anonymous> (src/app/app.component.spec.ts:37:29)

  ● AppComponent › should have urls

    zone-testing.js is needed for the fakeAsync() test helper but could not be found.
            Please make sure that your environment includes zone.js/testing

      at resetFakeAsyncZone (node_modules/@angular/core/fesm2020/testing.mjs:273:11)
      at Object.<anonymous> (node_modules/@angular/core/fesm2020/testing.mjs:26603:13)

Test Suites: 4 failed, 4 total
Tests: 4 failed, 4 total
Snapshots: 0 total
Time: 1.861 s, estimated 2 s
Ran all test suites.

```

I’d appreciate any help on this, for sure there is something unconfigured or uninitialized. Thanks!

---

<div class="post-metadata">

**Author:** ![Verfranc](https://avatars.discourse-cdn.com/v4/letter/v/58956e/32.png) [@Verfranc](https://forum.ionicframework.com/u/Verfranc)\
**Post date:** [October 5, 2022, 9:18pm UTC](https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030/2 "2022-10-05T21:18:50Z")

</div>

try this…

# setupJest.ts

import ‘jest-preset-angular/setup-jest.js’;

# jest.config.js

module.exports = {  
preset: ‘jest-preset-angular’,  
setupFilesAfterEnv: [‘/setup-jest.ts’],  
globalSetup: ‘jest-preset-angular/global-setup’,  
};

# package.json

{  
“extends”: “./tsconfig.json”,  
“compilerOptions”: {  
“outDir”: “./out-tsc/spec”,  
“module”: “CommonJs”,  
“types”: [“jest”]  
},  
“include”: [“src/ **/\*.spec.ts", "src/** /\*.d.ts”]  
}

---

<div class="post-metadata">

**Author:** ![MozMike](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@MozMike](https://forum.ionicframework.com/u/MozMike)\
**Post date:** [October 6, 2022, 9:06pm UTC](https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030/3 "2022-10-06T21:06:53Z")

</div>

Thanks @Verfranc for posting you first solution to the forum! We hope to see you around more in the future.

@neil89 was the above helpful?

---

<div class="post-metadata">

**Author:** ![sandy6755](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@sandy6755](https://forum.ionicframework.com/u/sandy6755)\
**Post date:** [November 14, 2023, 4:25pm UTC](https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030/4 "2023-11-14T16:25:14Z")

</div>

Hi @Verfranc I tried the given solution but facing the same error something missing .

---

<div class="post-metadata">

**Author:** ![marshall1986](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@marshall1986](https://forum.ionicframework.com/u/marshall1986)\
**Post date:** [January 23, 2025, 2:44pm UTC](https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030/5 "2025-01-23T14:44:14Z")

</div>

did you find a solution?

---

<div class="post-metadata">

**Author:** ![fabricio-dev](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@fabricio-dev](https://forum.ionicframework.com/u/fabricio-dev)\
**Post date:** [February 12, 2025, 2:45pm UTC](https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030/6 "2025-02-12T14:45:42Z")

</div>

Find any solution to this?

---

<div class="post-metadata">

**Author:** ![marshall1986](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@marshall1986](https://forum.ionicframework.com/u/marshall1986)\
**Post date:** [February 13, 2025, 7:47am UTC](https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030/7 "2025-02-13T07:47:35Z")

</div>

i’m using this configuration:

```auto
globalThis.ngJest = {
    skipNgcc: true,
    tsconfig: 'tsconfig.spec.json', // this is the project root tsconfig
};

/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
    preset: 'jest-preset-angular',
    testEnvironment: 'jsdom',
    setupFilesAfterEnv: ['<rootDir>/src/setup-jest.ts'],
    // setupFilesAfterEnv: ['<rootDir>/node_modules/jest-preset-angular/build/setup-jest.js'],
    transform: {
        '^.+\\.ts$': 'ts-jest', // Only transform .ts files
    },
    transformIgnorePatterns: [
        '/node_modules/(?!flat)/', // Exclude modules except 'flat' from transformation
    ],
    moduleNameMapper: {
        '\\.(css|scss)$': 'identity-obj-proxy',
        '^@/(.*)$': '<rootDir>/src/$1',
        '^app/(.*)$': '<rootDir>/src/app/$1',
        '^src/(.*)$': '<rootDir>/src/$1',
        '^shared/(.*)$': '<rootDir>/../shared/$1',
        '^mockServices/(.*)$': '<rootDir>/mockServices/$1',
        '^@app/(.*)$': '<rootDir>/src/app/$1',
        '^@environments/(.*)$': '<rootDir>/src/environments/$1',
    },
    moduleDirectories: ['node_modules', 'src'],
    fakeTimers: {
        enableGlobally: true,
    }
};

```

It seems fine even though the tests… it’s quite more complicated than using Karma, pity they deprecated it 🙁

---

<div class="post-metadata">

**Author:** ![fabricio-dev](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@fabricio-dev](https://forum.ionicframework.com/u/fabricio-dev)\
**Post date:** [February 19, 2025, 12:23pm UTC](https://forum.ionicframework.com/t/setup-jest-in-angular-14-ionic-6-project/227030/8 "2025-02-19T12:23:36Z")

</div>

At my case a similar solution solve the error

I add the ignore Pattern at jest.config, then solves the jest error

```auto
transformIgnorePatterns: [
'node_modules/(?!(@stencil|@ionic|@ionic/angular|@ionic/core|jose|@angular|.*\\.mjs$))',
],

```
