简体   繁体   English

Angular:使用 Speedy Web 编译器 (SWC) 进行 TestBed 测试时,组件未解析

[英]Angular: Component is not resolved when using Speedy Web Compiler (SWC) for TestBed tests

The tests in my Nx Angular 10 repo have been running very slow so I decided to switch from using jest-ts to @swc/jest.我的 Nx Angular 10 存储库中的测试运行速度非常慢,所以我决定从使用 jest-ts 切换到 @swc/jest。

jest.presets.ts开玩笑.presets.ts

const nxPreset = require('@nrwl/jest/preset');
module.exports = {
  ...nxPreset,
  resolver: '@nrwl/jest/plugins/resolver',
  moduleFileExtensions: ['ts', 'html', 'js', 'json'],
  testEnvironment: 'jsdom',
  transform: {
    ['.+\\.js$']: 'babel-jest',
    '^.+\\.(ts|html)$': '@swc/jest',
  },
}

.swcrc .swcrc文件

{
    "jsc": {
      "target": "es2020",
      "parser": {
        "tsx": false,
        "syntax": "typescript",
        "dynamicImport": true,
        "privateMethod": false,
        "functionBind": true,
        "exportDefaultFrom": true,
        "exportNamespaceFrom": true,
        "decorators": true,
        "topLevelAwait": true,
        "importMeta": true
      },
      "transform": {
        "decoratorMetadata": true,
        "legacyDecorator": false,
        "optimizer": {
            "globals": {
                "vars": {
                    "__DEBUG__": "true"
                }
            }
        }
      },
      "keepClassNames": true,
      "externalHelpers": true,
      "loose": true
    },
    "minify": false,
    "module": {
      "type": "commonjs",
      "strict": true,
    "strictMode": true,
    "lazy": false,
    "noInterop": true
    },
    "sourceMaps": "inline",
    "inlineSourcesContent": true
  }

The component is a freshly made Angular component with 0 extra logic, the test that fails looks like该组件是一个新制作的 Angular 组件,具有 0 个额外逻辑,失败的测试看起来像

describe('SpeedTestComponent', () => {
  let component: SpeedTestComponent;
  let fixture: ComponentFixture<SpeedTestComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [SpeedTestComponent],
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(SpeedTestComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

When I run the test it gives me this error当我运行测试时,它给了我这个错误在此处输入图像描述

This only happens for TestBed tests.这只发生在 TestBed 测试中。

The ideal behaviour is for the test to past.理想的行为是通过测试。

packages包裹

"@swc/core": "^1.2.152",
"@swc/helpers": "^0.3.6",
"@swc/jest": "^0.2.20",
"jest": "^26.2.2",

Angular doesn't directly compile using SWC or TS. Angular 不直接使用 SWC 或 TS 编译。 This is likely the cause of the issue.这可能是问题的原因。 It's way we started work on @nxext/angular-swc这是我们开始@nxext/angular-swc 工作的方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Angular 中正确使用 TestBed - Correct using of TestBed in Angular Angular 2 单元测试:如何覆盖 TestBed 中的单个提供程序(用于服务的单元测试,而不是具有服务依赖项的组件)? - Angular 2 unit tests: How to override a single provider in TestBed (for unit tests for a service, not a component, with service dependencies)? Angular 单元测试使用 TestBed.inject 根据测试执行顺序成功/失败 - Angular Unit Tests using TestBed.inject succeed/fail according to tests execution order Angular 4 单元测试 (TestBed) 非常慢 - Angular 4 Unit Tests (TestBed) extremely slow Angular 6变量未在Google Chrome中解析(网络组件) - Angular 6 variable not resolved in Google Chrome (web component) 在 Angular TestBed 中使用 TypeMoq Mock - Using TypeMoq Mock With Angular TestBed 在angular2和jasmine中运行测试时,无法在TestBed.createComponent中读取null的属性'injector' - Cannot read property 'injector' of null in TestBed.createComponent when running tests in angular2 and jasmine Angular 中没有 TestBed 和组件的单元测试指令 - Unit Tesing directives in Angular without TestBed & Component Testbed的angular2测试组件出现错误 - error with angular2 testing component with Testbed 覆盖Angular TestBed中另一个模块中的组件 - Override component in another module in Angular TestBed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM