Schreiben von Komponententests in TypeScript (am Beispiel von Katzen)

Wie schreibe ich Unit-Tests in ein TypeScript-Projekt? In diesem Artikel werde ich versuchen, diese Frage zu beantworten und Ihnen auch zu zeigen, wie Sie mit TypeScript eine Unit-Test-Umgebung für Projekte erstellen.





Was sind Unit-Tests?

Komponententests - Tests, die auf verschiedene Ebenen einer Anwendung angewendet werden und die kleinste teilbare Logik in einer Anwendung testen: z. B. ein Modul, eine Klasse oder eine Methode.





Das Wesentliche bei Komponententests besteht darin, sie in jede neue Klasse oder Methode zu schreiben und zu überprüfen, ob die nächste Änderung des Codes zum Auftreten von Fehlern (Bugs) in den bereits getesteten Teilen des Programms geführt hat.





. , . . , , . .





. :





project
| node_modules
| src
| package.json
| tsconfig.json
      
      



./src



cat.module.ts



Cat.





export class Cat {
  public name: string;
  public color: string;

  constructor(name: string, color: string) {
    this.name = name;
    this.color = color;
  }

  public move(distanceMeter: number) : string {
    return `${this.name} moved ${distanceMeter}m.`;
  }

  public say() : string {
    return `Cat ${this.name} says meow`;
  }
}
      
      



, . (SUT - system under test).





, test, .





npm :





npm install --save-dev ts-node mocha @testdeck/mocha nyc chai @types/chai







:

ts-node



- TypeScript REPL node.js.





mocha



- , , . . @testdeck/mocha



- testdeck , .





nyc



- CLI Istanbul, .





chai



- (assertions), . . @types/chai



typescript'





, test



, tsconfig.json



, TS .





{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "baseUrl": "./",
    "module": "commonjs",
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "isolatedModules": false,
    "strict": false,
    "noImplicitAny": false,
    "typeRoots" : [
      "../node_modules/@types"
    ]
  },
  "exclude": [
    "../node_modules"
  ],
  "include": [
    "./**/*.ts"
  ]
}
      
      



include test .ts





, register.js



ts-node, .





const tsNode = require('ts-node');
const testTSConfig = require('./test/tsconfig.json');

tsNode.register({
  files: true,
  transpileOnly: true,
  project: './test/tsconfig.json'
});
      
      



, .mocharc.json



, :





{
  "require": "./register.js",
  "reporter": "list"
}
      
      



.nyrc.json



.





{
  "extends": "@istanbuljs/nyc-config-typescript",
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules/"
  ],
  "extension": [
    ".ts"
  ],
  "reporter": [
    "text-summary",
    "html"
  ],
  "report-dir": "./coverage"
}
      
      



, :





project
| node_modules
| src
| test
| --- tsconfig.json
| .mocharc.json
| .nyrc.json
| package.json
| register.js
| tsconfig.json
      
      



package.json







"test": "nyc ./node_modules/.bin/_mocha 'test/**/*.test.ts'"
      
      



,





./test



cat.unit.test.ts



:





import { Cat } from '../src/cat.module';
import { suite, test } from '@testdeck/mocha';
import * as _chai from 'chai';
import { expect } from 'chai';

_chai.should();
_chai.expect;

@suite class CatModuleTest {
  private SUT: Cat;
  private name: string;
  private color: string;

  before() {
    this.name = 'Tom';
    this.color = 'black';
    this.SUT = new Cat(this.name, this.color);
  }
}
      
      



Cat cat.module.ts



, , .





before Cat, .





, .





import { Cat } from '../src/cat.module';
import { suite, test } from '@testdeck/mocha';
import * as _chai from 'chai';
import { expect } from 'chai';

_chai.should();
_chai.expect;

@suite class CatModuleTest {
  private SUT: Cat;
  private name: string;
  private color: string;

  before() {
    this.name = 'Tom';
    this.color = 'black';
    this.SUT = new Cat(this.name, this.color);
  }

  @test 'Cat is created' () {
    this.SUT.name.should.to.not.be.undefined.and.have.property('name').equal('Tom');
  }
}
      
      



npm test :





, 11-15. Cat move



say.







:





import { Cat } from '../src/cat.module';
import { suite, test } from '@testdeck/mocha';
import * as _chai from 'chai';
import { expect } from 'chai';

_chai.should();
_chai.expect;

@suite class CatModuleTest {
  private SUT: Cat;
  private name: string;
  private color: string;

  before() {
    this.name = 'Tom';
    this.color = 'black';
    this.SUT = new Cat(this.name, this.color);
  }

  @test 'Cat is created' () {
    this.SUT.name.should.to.not.be.undefined.and.have.property('name').equal('Tom');
  }

  @test 'Cat move 10m' () {
    let catMove = this.SUT.move(10);
    expect(catMove).to.be.equal('Tom moved 10m.');
  }

  @test 'Cat say meow' () {
    expect(this.SUT.say()).to.be.equal('Cat Tom says meow');
  }
}
      
      



Führen Sie unsere Tests erneut durch und stellen Sie sicher, dass die Cat-Klasse jetzt die vollständige Testabdeckung aufweist.





Ergebnis

Aus diesem Grund haben wir eine Testinfrastruktur für unsere Anwendung erstellt. Jetzt können wir jedes neue Modul oder jede neue Klasse mit Tests abdecken und überprüfen, ob der vorhandene Code nicht fehlerhaft ist.





PS: Basierend auf dem Artikel Einrichten des Unit-Tests mit TypeScript .








All Articles