使用 jest 及 wallaby.js 撰寫測試


透過 wallaby.js 進行 javascript TDD , 利用 jest 作為測試框架則可以省掉wallaby.js的設定檔

Sample Code

可執行的範例檔案放在Github,有需要請自取

建立專案

mkdir lab-jest-wallaby
cd lab-jest-wallaby\
npm init -y

安裝套件

因為要使用 ES6 語法來撰寫測試,所以需要額外加裝babel-jest@babel/preset-env

npm install jest babel-jest @babel/preset-env --save-dev

設定 Jest

在 jest 的部分可以透過指令建立預設的設定檔,所有的設定都用預設直接 Enter,再去修改即可,設定的部分可以參考Jest 官網說明

jest --init

設定 babel

建立babel.config.js內容如下

// babel.config.js
module.exports = {
	presets: [
		[
			'@babel/preset-env',
			{
				targets: {
					node: 'current',
				},
			},
		],
	],
};

Test Code Sample

import DateUtility from '../src/DateUtility';

describe('DateUtility', () => {
	let sut = new DateUtility();
	it('GetBasePath', () => {
		var today = new Date(2020, 10, 1);
		var actual = sut.GetBasePath(today);
		expect(actual).toBe('./2020/202011');
	});

	it('GetMMDD', () => {
		var today = new Date(2020, 10, 25);
		var actual = sut.GetMMDD(today);
		expect(actual).toBe('1125');
	});

	it('GetWeekBegin', () => {
		var today = new Date(2020, 10, 24);
		var actual = sut.GetWeekBegin(today);
		expect(actual).toBe('1123');
	});

	it('GetWeekEnd', () => {
		var today = new Date(2020, 10, 24);
		var actual = sut.GetWeekEnd(today);
		expect(actual).toBe('1127');
	});

	it('GetWeeklyFileName', () => {
		var today = new Date(2020, 10, 24);
		var actual = sut.GetWeeklyFileName(today);
		expect(actual).toBe('1123-1127');
	});

	it('GetWeeklyReportPath', () => {
		var today = new Date(2020, 10, 24);
		var actual = sut.GetWeeklyReportPath(today);
		expect(actual).toBe('./2020/202011/1123-1127.md');
	});

	it('GetMonthlyReportPath', () => {
		var today = new Date(2020, 10, 24);
		var actual = sut.GetMonthlyReportPath(today);
		expect(actual).toBe('./2020/202011/readme.md');
	});
});

Production Code Sample

// src/DateUtility.js
class DateUtility {
	GetBasePath(today) {
		let yyyy = today.getFullYear();
		let mm = this.GetMonth(today);
		return `./${yyyy}/${yyyy}${mm}`;
	}

	GetMMDD(currentDate) {
		return `${this.GetMonth(currentDate)}${this.GetDate(currentDate)}`;
	}

	GetWeekBegin(today) {
		let beginDate = this.AddDays(today, 1 - today.getDay());
		return this.GetMMDD(beginDate);
	}

	GetWeekEnd(today) {
		let beginDate = this.AddDays(today, 1 - today.getDay());
		let endDate = this.AddDays(beginDate, 4);
		return this.GetMMDD(endDate);
	}

	AddDays(date, days) {
		return new Date(date.getFullYear(), date.getMonth(), date.getDate() + days);
	}

	GetWeeklyFileName(today) {
		return `${this.GetWeekBegin(today)}-${this.GetWeekEnd(today)}`;
	}

	GetReportPath(today, type) {
		switch (type) {
			case 'month':
				return this.GetMonthlyReportPath(today);
			case 'week':
				return this.GetWeeklyReportPath(today);
		}
	}

	GetWeeklyReportPath(today) {
		return `${this.GetBasePath(today)}/${this.GetWeeklyFileName(today)}.md`;
	}

	GetMonthlyReportPath(today) {
		return `${this.GetBasePath(today)}/readme.md`;
	}

	GetMonth(today) {
		return this.Left(`0${today.getMonth() + 1}`, 2);
	}
	GetDate(today) {
		return this.Left(`0${today.getDate()}`, 2);
	}
	Left(str, num) {
		return str.substring(str.length - num, str.length);
	}
}

export default DateUtility;

手動執行測試

package.json的 test 指令改成呼叫 jest

// package.json
{
	"scripts": {
		"test": "jest"
	}
}

就可以利用npm或是yarn執行測試,當然這只是方便的做法,你可以選擇打完整指令npx jest

npm run test
yarn test

使用 wallaby.js

透過 F1 執行 wallaby.js