Setup jest with angular 16

523 views Asked by At

I am trying to setup jest with Angular.

jest.config.ts:

import { pathsToModuleNameMapper } from 'ts-jest/utils';
import type { Config } from 'jest';
const preset = require('jest-preset-angular/jest-preset');
const { compilerOptions } = require('./tsconfig');

const jestConfig: Config = {
  preset: 'jest-preset-angular/presets/defaults-esm',
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  // globals: {
  //   ...preset.globals,
  //   APP_VERSION: { hash: '00000000', describe: 'v0.0.0', short: 'v0.0.0', branch: '_test_', date_short: '00000000' },
  // },
  globalSetup: 'jest-preset-angular/global-setup',

  moduleNameMapper: { ...pathsToModuleNameMapper(compilerOptions.paths || {}, {
    prefix: '<rootDir>/',
  }), tslib: 'tslib/tslib.es6.js' },
  coverageReporters: ['html', 'text', 'text-summary'],
  transformIgnorePatterns: ['node_modules/(?!@ionic-native|@ionic)'],
  testPathIgnorePatterns: ['<rootDir>/build/', '<rootDir>/pack/'],
  moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
};

export default jestConfig;

my jest.setup.ts file:

import '@angular/localize/init';

// global.capacitorExports = require('@capacitor/core');
Object.defineProperty(global, 'capacitorExports', {
  value: require('@capacitor/core'),
  enumerable: true
});

const { TextEncoder, TextDecoder } = require('util');
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

Object.defineProperty(window, 'CSS', { value: null});
Object.defineProperty(window, 'getComputedStyle', {
  value: () => {
    return {
      display: 'none',
      appearance: ['-webkit-appearance']
    };
  }
});

Object.defineProperty(document, 'doctype', {
  value: '<!DOCTYPE html>'
});
Object.defineProperty(document.body.style, 'transform', {
  value: () => {
    return {
      enumerable: true,
      configurable: true
    };
  }
});

Problem: When I try to run the tests, either by running npx jest <file.spec.ts>, or by running npm run test, I get the error: enter image description here

Versions:
Angular: 16
ts-jest: 26.4.4
jest: 29.7
jest-preset-angular: 13.1.2

I think I almost tried every solutions found on the docs so if somebody has an idea on how to help me ...

Thanks a lot !

0

There are 0 answers