typescript - Angular için Jest'i yapılandırma
Web'deki çeşitli kılavuzlara göre Angular için Jest yapılandırmak bu kadar kolaydır:
ng new jest-test
cd jest-test
npm i -D jest jest-preset-angular
package.json'i değiştirin:
"test": "jest",
[...]
"jest": {
"preset": "jest-preset-angular",
"setupTestFrameworkScriptFile": "./src/setup-jest.ts"
},
setup-jest.ts:
import 'jest-preset-angular';
npm test çalıştırırken sürekli olarak bu hatayla karşılaşıyorum, ancak:
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
jest-test/src/app/app.component.spec.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { TestBed, async } from '@angular/core/testing';
^
SyntaxError: Unexpected token {
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Web’de bulduğum uygulamayı yapılandırmanın sayısız varyasyonunu denedim, tümü bu hata mesajını verdi.
Ek bilgi sağlamam gerekip gerekmediğini bana bildirin.
Cevap #1
Eğer Angular CLI kullanıyorsanız bu kütüphane çok faydalı olacaktır.
Jest kurulumu ile ilgili kalıp kodlardan çalışma alanınızı temiz tutmanıza ve testlerinizi Angular CLI ile çalıştırmanıza (ng test) olanak tanır.
Bu aynı zamanda Jest yapılandırmasını yapma zahmetinden de sizi kurtarır.
Özel hatanıza gelince, muhtemelen tsconfig.spec.json dosyanızla ilgilidir. module alanını commonjs olarak değiştirdiniz mi? Jest'in çalışması için bu gereklidir.
Daha fazla ayrıntı için bu makaleyi okuyun.
Sorumluluk Reddi: Ben bu kütüphanenin sahibiyim.
Cevap #2
Birkaç hafta önce bu makaleyi takip ederek denedim.
Sorunsuz geçti ama Jasmine/Karma'dan Jest'e geçmemeye karar verdim. Nedenleri:
- UT'leri birden fazla gerçek tarayıcıda çalıştırmam gerekiyor ve Jest bunu yapamıyor. (Jest Reposundaki tartışmayı okuyun)
- Jasmin'i Protractor veya Selenium-Webdriver kullanarak e2e testleri yazmak için de kullanabilirim