我正在尝试将Jest测试框架集成到我的项目中,该项目使用Angular 14和Ionic 6,还使用其他可能会引起冲突的插件,例如firebase和ngrx。
我主要遵循了Tim Deschryver教程和其他一些教程,甚至从stackoverflow上复制并粘贴了一些代码来解决我的Jest测试错误,但是没有任何效果。我甚至尝试了删除所有软件包和修改并重新开始,但是仍然没有成功。
这里是我更新的repo https://github.com/neil89/igloo(它非常易于管理)。但总的来说,我的主要修改如下:
我主要遵循了Tim Deschryver教程和其他一些教程,甚至从stackoverflow上复制并粘贴了一些代码来解决我的Jest测试错误,但是没有任何效果。我甚至尝试了删除所有软件包和修改并重新开始,但是仍然没有成功。
这里是我更新的repo https://github.com/neil89/igloo(它非常易于管理)。但总的来说,我的主要修改如下:
package.json
{
"name": "igloo",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "https://ionicframework.com/",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "jest",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/common": "^14.0.0",
"@angular/core": "^14.0.0",
"@angular/fire": "^7.4.1",
"@angular/forms": "^14.0.0",
"@angular/platform-browser": "^14.0.0",
"@angular/platform-browser-dynamic": "^14.0.0",
"@angular/router": "^14.0.0",
"@briebug/jest": "^1.3.1",
"@ionic/angular": "^6.2.6",
"@ngrx/effects": "^14.3.1",
"@ngrx/store": "^14.3.1",
"firebase": "^9.9.4",
"rxjs": "~6.6.0",
"tslib": "^2.2.0",
"zone.js": "~0.11.4"
},
"devDependencies": {
"@angular-devkit/build-angular": "^14.0.0",
"@angular-eslint/builder": "~13.0.1",
"@angular-eslint/eslint-plugin": "~13.0.1",
"@angular-eslint/eslint-plugin-template": "~13.0.1",
"@angular-eslint/template-parser": "~13.0.1",
"@angular/cli": "^14.0.0",
"@angular/compiler": "^14.0.0",
"@angular/compiler-cli": "^14.0.0",
"@angular/language-service": "^14.0.0",
"@types/jest": "^29.0.1",
"@types/node": "^12.11.1",
"@typescript-eslint/eslint-plugin": "5.3.0",
"@typescript-eslint/parser": "5.3.0",
"eslint": "^7.6.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jsdoc": "30.7.6",
"eslint-plugin-prefer-arrow": "1.2.2",
"jest": "^28.1.3",
"jest-preset-angular": "^12.2.2",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"typescript": "~4.7.3"
},
"jest": {
"preset": "jest-preset-angular",
"setupFilesAfterEnv": [
"<rootDir>/src/setupJest.ts"
],
"globalSetup": "jest-preset-angular/global-setup"
},
"description": "An Ionic project"
}
jest.config.js
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
globals: {
'ts-jest': {
tsconfig: 'tsconfig.spec.json',
isolatedModules: true,
},
},
moduleNameMapper: {
'@angular/compiler-cli/ngcc': '<rootDir>/node_modules/@angular/compiler-cli/bundles/ngcc/main-ngcc.js',
},
testEnvironment: 'jsdom',
modulePathIgnorePatterns: ['examples/.*', 'website/.*'],
snapshotSerializers: [require.resolve('jest-snapshot-serializer-raw')],
testPathIgnorePatterns: ['/node_modules/', '/examples/', '/e2e/.*/__tests__', '\\.snap$'],
transform: {
'^.+\\.(ts|js|mjs|html)$': '<rootDir>/build/index.js',
},
transformIgnorePatterns: ['node_modules/(?!(jest-test|@ngrx))'],
};
setupJest.ts
import 'jest-preset-angular';
以下是我比较简单的失败测试之一: app.component.spec.ts
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [
RouterTestingModule.withRoutes([])
],
}).compileComponents();
});
it('should create the app', (() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it('should have menu labels', (() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const app = fixture.nativeElement;
const menuItems = app.querySelectorAll('ion-label');
expect(menuItems.length).toEqual(7);
expect(menuItems[0].textContent).toContain('Inbox');
expect(menuItems[1].textContent).toContain('Outbox');
}));
it('should have urls', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const app = fixture.nativeElement;
const menuItems = app.querySelectorAll('ion-item');
expect(menuItems.length).toEqual(7);
expect(menuItems[0].getAttribute('ng-reflect-router-link')).toEqual('/folder/Inbox');
expect(menuItems[1].getAttribute('ng-reflect-router-link')).toEqual('/folder/Outbox');
});
});
现在,当我运行npm test
时,我的输出如下:
> igloo@0.0.1 test
> jest
Determining test suites to run...
ngcc-jest-processor: running ngcc
FAIL src/app/fridge/fridge.service.spec.ts
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• 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/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/daniel.rodriguez/Documents/Personal/iGloo/igloo/node_modules/firebase/app/dist/index.esm.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { registerVersion } from '@firebase/app';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
at node_modules/@angular/fire/bundles/angular-fire.umd.js:2:111
at Object.<anonymous> (node_modules/@angular/fire/bundles/angular-fire.umd.js:5:2)
FAIL src/app/fridge/fridge.component.spec.ts
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• 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/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/daniel.rodriguez/Documents/Personal/iGloo/igloo/node_modules/firebase/app/dist/index.esm.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { registerVersion } from '@firebase/app';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
at node_modules/@angular/fire/bundles/angular-fire.umd.js:2:111
at Object.<anonymous> (node_modules/@angular/fire/bundles/angular-fire.umd.js:5:2)
FAIL src/app/folder/folder.page.spec.ts
● FolderPage › should create
Need to call TestBed.initTestEnvironment() first
9 |
10 | beforeEach(async () => {
> 11 | await TestBed.configureTestingModule({
| ^
12 | declarations: [ FolderPage ],
13 | imports: [IonicModule.forRoot(), RouterModule.forRoot([])]
14 | }).compileComponents();
at TestBedRender3.get compiler [as compiler] (node_modules/@angular/core/fesm2020/testing.mjs:26367:19)
at TestBedRender3.configureTestingModule (node_modules/@angular/core/fesm2020/testing.mjs:26290:14)
at Function.configureTestingModule (node_modules/@angular/core/fesm2020/testing.mjs:26126:30)
at src/app/folder/folder.page.spec.ts:11:19
at node_modules/tslib/tslib.js:118:75
at Object.__awaiter (node_modules/tslib/tslib.js:114:16)
at Object.<anonymous> (src/app/folder/folder.page.spec.ts:10:25)
● FolderPage › should create
zone-testing.js is needed for the fakeAsync() test helper but could not be found.
Please make sure that your environment includes zone.js/testing
at resetFakeAsyncZone (node_modules/@angular/core/fesm2020/testing.mjs:273:11)
at Object.<anonymous> (node_modules/@angular/core/fesm2020/testing.mjs:26603:13)
FAIL src/app/app.component.spec.ts
● AppComponent › should create the app
Need to call TestBed.initTestEnvironment() first
7 |
8 | beforeEach(async () => {
> 9 | await TestBed.configureTestingModule({
| ^
10 | declarations: [
11 | AppComponent
12 | ],
at TestBedRender3.get compiler [as compiler] (node_modules/@angular/core/fesm2020/testing.mjs:26367:19)
at TestBedRender3.configureTestingModule (node_modules/@angular/core/fesm2020/testing.mjs:26290:14)
at Function.configureTestingModule (node_modules/@angular/core/fesm2020/testing.mjs:26126:30)
at src/app/app.component.spec.ts:9:19
at node_modules/tslib/tslib.js:118:75
at Object.__awaiter (node_modules/tslib/tslib.js:114:16)
at Object.<anonymous> (src/app/app.component.spec.ts:8:25)
● AppComponent › should create the app
zone-testing.js is needed for the fakeAsync() test helper but could not be found.
Please make sure that your environment includes zone.js/testing
at resetFakeAsyncZone (node_modules/@angular/core/fesm2020/testing.mjs:273:11)
at Object.<anonymous> (node_modules/@angular/core/fesm2020/testing.mjs:26603:13)
● AppComponent › should have menu labels
NG0908: In this configuration Angular requires Zone.js
25 |
26 | it('should have menu labels', (() => {
> 27 | const fixture = TestBed.createComponent(AppComponent);
| ^
28 | fixture.detectChanges();
29 | const app = fixture.nativeElement;
30 | const menuItems = app.querySelectorAll('ion-label');
at new NgZone (node_modules/@angular/core/fesm2020/core.mjs:26180:19)
at R3TestBedCompiler.compileTestModule (node_modules/@angular/core/fesm2020/testing.mjs:25851:24)
at R3TestBedCompiler.finalize (node_modules/@angular/core/fesm2020/testing.mjs:25419:14)
at TestBedRender3.get testModuleRef [as testModuleRef] (node_modules/@angular/core/fesm2020/testing.mjs:26377:49)
at TestBedRender3.inject (node_modules/@angular/core/fesm2020/testing.mjs:26300:29)
at TestBedRender3.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26340:44)
at Function.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26179:37)
at Object.<anonymous> (src/app/app.component.spec.ts:27:29)
● AppComponent › should have menu labels
zone-testing.js is needed for the fakeAsync() test helper but could not be found.
Please make sure that your environment includes zone.js/testing
at resetFakeAsyncZone (node_modules/@angular/core/fesm2020/testing.mjs:273:11)
at Object.<anonymous> (node_modules/@angular/core/fesm2020/testing.mjs:26603:13)
● AppComponent › should have urls
NG0908: In this configuration Angular requires Zone.js
35 |
36 | it('should have urls', () => {
> 37 | const fixture = TestBed.createComponent(AppComponent);
| ^
38 | fixture.detectChanges();
39 | const app = fixture.nativeElement;
40 | const menuItems = app.querySelectorAll('ion-item');
at new NgZone (node_modules/@angular/core/fesm2020/core.mjs:26180:19)
at R3TestBedCompiler.compileTestModule (node_modules/@angular/core/fesm2020/testing.mjs:25851:24)
at R3TestBedCompiler.finalize (node_modules/@angular/core/fesm2020/testing.mjs:25419:14)
at TestBedRender3.get testModuleRef [as testModuleRef] (node_modules/@angular/core/fesm2020/testing.mjs:26377:49)
at TestBedRender3.inject (node_modules/@angular/core/fesm2020/testing.mjs:26300:29)
at TestBedRender3.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26340:44)
at Function.createComponent (node_modules/@angular/core/fesm2020/testing.mjs:26179:37)
at Object.<anonymous> (src/app/app.component.spec.ts:37:29)
● AppComponent › should have urls
zone-testing.js is needed for the fakeAsync() test helper but could not be found.
Please make sure that your environment includes zone.js/testing
at resetFakeAsyncZone (node_modules/@angular/core/fesm2020/testing.mjs:273:11)
at Object.<anonymous> (node_modules/@angular/core/fesm2020/testing.mjs:26603:13)
Test Suites: 4 failed, 4 total
Tests: 4 failed, 4 total
Snapshots: 0 total
Time: 1.861 s, estimated 2 s
Ran all test suites.
我很感激你的帮助,肯定有一些未配置或未初始化的东西。谢谢!