如何解决TypeError:options.astTransformers || []map不是角度为10的函数
我正在使用
运行单元测试"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' },startDate: {
value: new NgbDate(2019,2,27),minDate: {
value: new NgbDate(2019,writable: true
}
});
fixture.detectChanges();
});
it('should create',() => {
expect(component).toBeTruthy();
});
it('Should Match Snapshot',async () => {
(expect(fixture) as any).toMatchSnapshot();
});
});
最好的配置
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'],"^@pts-ngx/core/(.*)$": "<rootDir>/dist/pts-ngx/core/$1"
}
};
Jest.library配置
// jest.lib.config.js
const baseConfig = require('./jest.base.config');
module.exports = {
...baseConfig,roots: ['<rootDir>/projects'],};
解决方法
这对我有帮助
- 卸载jest-preset-angular
- 重新安装jest-preset-angular
- 清除玩笑缓存
- 重试
要清除angular.json文件上的缓存,请添加以下脚本
"scripts": {
"clear_jest": "jest --clearCache"
}
然后运行npm run clear_jest
如果以上选项无效,请尝试通过npm install --dev ts-jest
显式安装ts-jest
此错误主要是由于package.json.lock文件中的ts-jest
版本错误造成的。
尝试将“ ts-jest”更新为最新版本(26.3.0)
,运行 npm install --dev ts-jest 解决了我的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。