4

I'm using jest framework to unit-test my Angular application. I'm having trouble in test.ts file with the zone.js I think.

I'm getting the error stack trace

TypeError: Cannot read property 'prototype' of undefined

      3 | import { getTestBed } from '@angular/core/testing';
      4 | import 'zone.js/dist/zone-testing';
    > 5 | import {
        | ^
      6 |   BrowserDynamicTestingModule,
      7 |   platformBrowserDynamicTesting
      8 | } from '@angular/platform-browser-dynamic/testing';

      at __extends (node_modules/zone.js/dist/zone-testing.js:394:73)
      at node_modules/zone.js/dist/zone-testing.js:530:9
      at node_modules/zone.js/dist/zone-testing.js:619:7
      at node_modules/zone.js/dist/zone-testing.js:620:3
      at Object.<anonymous>.NEWLINE (node_modules/zone.js/dist/zone-testing.js:9:65)
      at Object.<anonymous> (node_modules/zone.js/dist/zone-testing.js:12:2)
      at Object.<anonymous> (src/test.ts:5:1

I'm looking for a way to fix this issue with the minimum changes in code.

I have already tried to adapt to the solutions given in https://github.com/AlexanderZaytsev/react-native-i18n/issues/233 and
Jest: TypeError: Cannot read property of undefined without any luck.

Also, I have tried the docs, but it does not specify anything related to this or I have missed it.

This is my test.ts file.

import { getTestBed } from '@angular/core/testing';
import 'zone.js/dist/zone-testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

I have used the compilerOptions,

    "esModuleInterop": true,
    "module": "es2015",

As I have said, I'm looking for a way to fix this with minimum code change. Any help is appreciated.

1 Answer 1

5

If you have completely migrated to Jest, you can just delete test.ts as it's not required anymore since you are not using Jasmine and Karma anymore. You can read here.

Also, make sure to remove its reference from tsconfig.spec.ts.

If for some reason, you need to keep it, you can ignore test.ts files in Jest configuration, as by default jest will test all files ending with spec.ts/js and test.ts/js.

**Default configuration:** 
testMatch: [ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ]

**Change to:**
testMatch: [ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.spec).[jt]s?(x)" ]

You can read more about configuring Jest here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the solution. I tried it before also. Then it produced the following error. TypeScript diagnostics (customize using '[jest-config].globals.ts-jest.diagnostics' option): error TS6053: File '.../Sample-Login-App_jest/src/test.ts' not found. After some tinkering, I saw that the tsconfig.spec.ts file includes test.ts file in its files array. After I removed it, tests were working fine. Thanks
Great, I'll update the answer with that. Might help someone else. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.