Menulis pengujian unit di TypeScript (dengan contoh kucing)

Bagaimana cara menulis pengujian unit dalam proyek TypeScript? Pada artikel ini saya akan mencoba menjawab pertanyaan ini dan juga menunjukkan kepada Anda cara membuat lingkungan pengujian unit untuk proyek menggunakan TypeScript.





Apa itu unit test?

Pengujian unit - Pengujian yang diterapkan di berbagai lapisan aplikasi yang menguji logika terbagi terkecil dalam aplikasi: misalnya, modul, kelas, atau metode.





, , , () .





. , . . , , . .





. :





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 :





Seperti yang Anda lihat, cakupan kami belum lengkap, baris 11-15 masih belum teruji. Ini hanyalah metode kelas Cat move



dansay.







Kami menambahkan dua tes lagi untuk metode ini dan kami berakhir dengan file seperti itu dengan tes:





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');
  }
}
      
      



Jalankan pengujian kami lagi dan lihat bahwa kelas Cat sekarang memiliki cakupan pengujian penuh.





Hasil

Hasilnya, kami telah membuat infrastruktur pengujian untuk aplikasi kami dan sekarang kami dapat mencakup modul atau kelas baru dengan pengujian, memeriksa bahwa kode yang ada tidak rusak.





PS: Berdasarkan artikel Cara menyiapkan pengujian unit dengan TypeScript .








All Articles