![](/img/trans.png)
[英]Angular Karma Test getting “TypeError: 'undefined' is not a function”
[英]Getting typeError: (options.astTransformers || []).map is not a function while running the test suite in Angular
在 Angular 中運行玩笑測試用例時,我遇到了錯誤。 所有測試套件都因此錯誤而失敗。
獲取typeError: (options.astTransformers || []).map is not a function
在 Angular 中運行測試套件時
此解決方案適用於 Angular 8/9/10
我建議執行以下步驟:
如果上述解決方案不起作用,請嘗試安裝
npm install --dev ts-jest
通過刪除 jest.config.js 文件中的以下行來修復此錯誤。
module.exports = {
...
passWithNoTests: true,
projects: '<rootDir>/libs/now-version' // <--- newly added property. should be removed
};
正如我在 github 上找到的這個鏈接。 https://github.com/nrwl/nx/issues/3885#issuecomment-706620382
在將 Angular 更新為 11 后開始出現此錯誤,當我在 NX 中生成一個新庫並最終通過 CLI 生成新庫時添加此新行
似乎是該軟件包的較新版本。
在package.json
我將jest-preset-angular
從"8.3.2"
更改為"8.2.0"
,錯誤消失了。
如下更新jest.config.js解決了我的問題
module.exports = {
displayName: 'AppName',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
},
coverageDirectory: '../../coverage/libs/appName',
transform: {
'^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
},
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
],
testRunner: 'jest-jasmine2'
};
Angular 的 Jest 配置可能很棘手。
我建議您使用jest-preset-angular這是一個庫,您將導入到您的 Angular 項目中,該庫將管理 Jest for Angular 的配置。
這可能與 Jest 27 的更新有關,該更新將astTransformers as string[]
棄用astTransformers as string[]
。
將你的 Angular 項目配置為使用 Jest 26 和 jest-preset-angular,你應該沒問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.