Angular TypeError:(options.astTransformers | |[])。贴图不是函数

Angular TypeError:(options.astTransformers | |[])。贴图不是函数,angular,typescript,jestjs,angular-test,angular10,Angular,Typescript,Jestjs,Angular Test,Angular10,我使用 "jest": "^26.4.2", "jest-preset-angular": "^8.3.1", 当我运行命令npm run test:app时,它在Angular 8中工作正常,现在我已经将Angular更新为最新版本10。我在每个测试组件上都得到如下错误 测试套件无法运行 TypeError: (options.astTransformers || []).map is not a functio

我使用

"jest": "^26.4.2",
"jest-preset-angular": "^8.3.1",
当我运行命令
npm run test:app
时,它在Angular 8中工作正常,现在我已经将Angular更新为最新版本10。我在每个测试组件上都得到如下错误

测试套件无法运行

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)
这是面临错误的组件之一

describe('DateSelectionComponent', () => {
      let component: DateSelectionComponent;
      let fixture: ComponentFixture<DateSelectionComponent>;
    
      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();
      });
    });
description('DateSelectionComponent',()=>{
let组件:DateSelectionComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[DateSelectionComponent,SafePipe],
进口:[
共享模块,
NgReduxTestingModule,
RouterTestingModule.withRoutes([])
],
供应商:[
{
提供:PageApiations,useValue:{setPageState(){}
}
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(DateSelectionComponent);
组件=fixture.componentInstance;
对象。定义属性(组件{
第页$:{
值:of('value'),
可写:对
},
第页:{
值:{destination:'value'},
可写:对
},
起始日期:{
价值:新NGB日期(2019年2月27日),
可写:对
},
minDate:{
价值:新NGB日期(2019年2月27日),
可写:对
}
});
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
它('应该匹配快照',异步()=>{
(除(固定装置)外)toMatchSnapshot();
});
});
Jest配置

Jest.app配置

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'
    }
};
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.lib.config.js
const baseConfig = require('./jest.base.config');

module.exports = {
    ...baseConfig,
    roots: ['<rootDir>/projects'],
};
constbaseconfig=require('./jest.base.config');
module.exports={
…baseConfig,
根:['/src'],
模块路径:['/dist'],
modulePathIgnorePatterns:['/src/app/shared'],
模块映射:{
“^@pts ngx/core”:“/dist/pts ngx/core”,
“^@pts ngx/core/(.*)$”:“/dist/pts ngx/core/$1”,
“projects/pts ngx/core/(.*)$”:“/projects/pts ngx/core/$1”
}
};
Jest基本配置

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'
    }
};
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.lib.config.js
const baseConfig = require('./jest.base.config');

module.exports = {
    ...baseConfig,
    roots: ['<rootDir>/projects'],
};
module.exports={
预设:“开玩笑预设角度”,
setupFilesAfterEnv:['/setupJest.ts'],
模块映射:{
“^@pts ngx/core”:“/dist/pts ngx/core”,
“^@pts ngx/core/(.*)$”:“/dist/pts ngx/core/$1”
}
};
Jest.library配置

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'
    }
};
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.lib.config.js
const baseConfig = require('./jest.base.config');

module.exports = {
    ...baseConfig,
    roots: ['<rootDir>/projects'],
};
//jest.lib.config.js
const baseConfig=require('./jest.base.config');
module.exports={
…baseConfig,
根:['/projects'],
};
尝试将“ts jest”更新到最新版本(26.3.0)

这有助于我

  • 卸载jest预设值
  • 重新安装jest预设角度
  • 清除jest缓存
  • 重试
  • 要清除angular.json文件上的缓存,请添加以下脚本

    "scripts": {
    "clear_jest": "jest --clearCache"
      }
    
    然后运行
    npm运行clear\u jest

    如果上述选项不起作用,请尝试通过
    npm安装--dev ts jest


    出现此错误的主要原因是package.json.lock文件中的
    ts jest
    版本错误。

    运行npm安装--dev ts jest解决了我的问题