简体   繁体   English

运行 tslint 时,角度单元测试中的“未使用的表达式,期望赋值或函数调用”

[英]'unused expression, expected an assignment or function call' in an angular unit test when running tslint

Is there a way to fix this error without having to place an ignore in the file?有没有办法解决这个错误而不必在文件中放置一个忽略?

Error when running command:运行命令时出错:

./node_modules/tslint/bin/tslint -p src/tsconfig.json --type-check src/app/app.component.spec.ts
[21, 5]: unused expression, expected an assignment or function call

Test:测试:

let chai = require('chai');
let expect = chai.expect;

import { AppComponent } from './app.component';

import { ComponentFixture, TestBed } from '@angular/core/testing';

describe('AppComponent', function() {
  let testAppComponent: AppComponent;
  let fixture: ComponentFixture<AppComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [AppComponent]
    });
    fixture = TestBed.createComponent(AppComponent);
    testAppComponent = fixture.componentInstance;
  });

  it('should create the correct component', () => {
    expect(testAppComponent instanceof AppComponent).to.be.true;
  });
});

Did you perhaps mean this:你也许是这个意思:

expect(testAppComponent instanceof AppComponent).toBe(true);

The expression you have there just accesses a bunch of properties, you need some kind of function call for it to actually do anything.你在那里的表达式只是访问一堆属性,你需要某种函数调用才能让它真正做任何事情。

In my case I had a dangling ?就我而言,我有一个悬空?

someArray.find(someCheck)?.items.push(someThing)

I just wrote it out as an if statement:我只是把它写成一个if语句:

let ok = someArray.find(someCheck);
if (ok) {
 ok.items.push(someThing);
}

暂无
暂无

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

相关问题 你能在用 typescript/angular 导出它之前创建一个 class 吗? | TSLint:未使用的表达式,需要赋值或 function 调用 - Can you create a class before exporting it in typescript/angular? | TSLint: unused expression, expected an assignment or function call 未使用的表达式,需要赋值或 function 调用(角度) - Unused expression, expected an assignment or function call (Angular) 未使用的表达式,期望赋值或函数调用承诺函数中的 Angular lint 错误 - unused expression, expected an assignment or function call Angular lint error in promise function TSLint在分配变量时返回预期的分配或函数调用 - TSLint returns expected an assignment or function call when assigning variables TSLint返回预期的赋值或函数调用 - TSLint returns expected an assignment or function call 预期分配或 function 调用,而是看到一个表达式:no-unused-expressions - Expected an assignment or function call and instead saw an expression: no-unused-expressions 在进行 Angular 单元测试时如何使用 SpyOn 函数调用服务 - How to call a service with SpyOn function when doing Angular Unit Test 以角度运行单元测试 - Running unit test in angular 角度单元测试 - 错误:<toHaveBeenCalled> : 期待间谍,但得到了函数 - Angular unit test - Error: <toHaveBeenCalled> : Expected a spy, but got Function tslint Angular问题-所有导入均未使用 - tslint issue with angular - All imports are unused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM