angulartypescriptjestjsserializationjest-preset-angular

Jest and Serialize-ts Count find type for field:


I am facing an issue while running testing using jest in my angular application after upgrading jest to v9 while using serilize-ts

The error is:

Count find type for field: amount

      4 | export class Price {
      5 |   @Field()
    > 6 |   amount: number;
        |   ^
      7 |
      8 |   @Field()
      9 |   currency: string;

      at getSerializerForType (../../node_modules/serialize-ts/dist/decorators/type.decorator.js:29:15)
      at getSerializerFromParams (../../node_modules/serialize-ts/dist/decorators/type.decorator.js:20:12)
      at ../../node_modules/serialize-ts/dist/decorators/type.decorator.js:10:26
      at ../../node_modules/serialize-ts/dist/decorators/field.decorator.js:16:46
      at DecorateProperty (../../node_modules/reflect-metadata/Reflect.js:553:33)
      at Object.decorate (../../node_modules/reflect-metadata/Reflect.js:123:24)
      at Object.__decorate (../../node_modules/tslib/tslib.js:96:96)
      at src/app/modules/product/models/price.model.ts:6:3
      at Object.<anonymous> (src/app/modules/product/models/price.model.ts:4:1)
      at Object.<anonymous> (src/app/modules/product/models/offer.model.ts:1:1)
      at Object.<anonymous> (src/app/modules/product/models/index.ts:1:1)
      at Object.<anonymous> (src/app/modules/product/pipes/thumbnail.pipe.spec.ts:6:1)

The price.model.ts:

import { Field, Model } from 'serialize-ts';

@Model()
export class Price {
  @Field()
  amount: number;

  @Field()
  currency: string;
}

Here is my first jest.config.js:

module.exports = {
  preset: "../../jest.config.js",
  coverageDirectory: "../../coverage/",
  collectCoverage: true,
  coverageReporters: [
    'text',
    'text-summary',
    'lcov',
  ],
  moduleNameMapper: {
    '^~shared/(.*)$': '<rootDir>/src/app/shared/$1',
    '^~app/(.*)$': '<rootDir>/src/app/$1',
    '^~core/(.*)$': '<rootDir>/src/app/core/$1',
    '^~env/environment(.*)$': '<rootDir>/src/environments/environment.testing.ts',
  },
  collectCoverageFrom: [
    // included
    '**/src/app/**/{store,services,pipes,utils}/*.{js,ts}',

    // excluded
    '!**/src/app/**/index.{js,ts}',
    '!**/src/app/**/*.{mock,enum,constants,component,module,interceptors,constant,guard,class,directive,model,helper,animations}.{js,ts}',
    '!**/node_modules/**',
  ],
  snapshotSerializers: [
    "jest-preset-angular/build/AngularSnapshotSerializer.js",
    "jest-preset-angular/build/HTMLCommentSerializer.js"
  ],
  setupFilesAfterEnv: ['./src/test-setup.ts']
};

which extends this jest.config.js:

module.exports = {
  testMatch: ['**/+(*.)+(spec|test).+(ts|js|tsx)?(x)'],
  transform: {
    '^.+\\.(ts|js|html|tsx)$': 'ts-jest'
  },
  resolver: '@nrwl/jest/plugins/resolver',
  moduleFileExtensions: ['ts', 'js', 'html', 'tsx'],
  collectCoverage: true,
  coverageReporters: ['html'],
  preset: 'jest-preset-angular'
};

my package.json:


  "devDependencies": {
    "@angular-builders/jest": "9.0.1",
    "@angular-devkit/build-angular": "~0.901.9",
    "@angular/cli": "9.1.9",
    "@angular/compiler-cli": "9.1.11",
    "@angular/language-service": "9.1.11",
    "@angular/platform-server": "9.1.11",
    "@ngrx/schematics": "9.2.0",
    "@nrwl/jest": "9.5.1",
    "@nrwl/workspace": "^9.5.1",
    "eslint": "6.1.0",
    "jest": "26.1.0",
    "jest-preset-angular": "8.2.1",
    "prettier": "^2.0.4",
    "ts-jest": "26.1.1",
    "tslint": "5.11.0",
    "typescript": "3.8.3"
  }

and my test-setup.ts:

import 'jest-preset-angular';

Solution

  • jest-preset-angular uses its own Reflection package, which only works with Angular Decorators. A soon as you want to use another package with decorators, you should include a function-complete Reflect-Metadata package, such as provided by the package core-js.

    To do so, add the Reflect-Metadata-Polyfills before importing jest-preset-angular to your setupFilesAfterEnv-file (this file is usually called src/setupJest.ts or src/test-setup.ts, here for core-js@3):

    require('core-js/es7/reflect');
    require('core-js/proposals/reflect-metadata');
    import `jest-preset-angular`;