
npm init
เพื่อสร้าง npm ให้กับโปรเจคนี้ติดตั้ง Karma แบบ global
npm install karma-cli -g
ติดตั้ง Karma แบบ devDependencies
npm install karma --save-dev
ติดตั้ง Karma - Jasmine แบบ devDependencies
npm install karma-jasmine --save-dev
ติดตั้ง Chrome ให้กับ Karma
npm install karma-chrome-launcher --save-dev
สร้างไฟล์ karma.conf.js
karma init karma.conf.js
คำสั่งข้างบน คือ การสร้าง config file สำหรับ test runner ของเรา ซึ่งเราสามารถสร้างได้เอง โดยศึกษาเพิ่มเติมที่ http://karma-runner.github.io/0.12/intro/configuration.html
Creating karma config file
> karma init karma.conf.js
Answer the prompts as follows:
Which testing framework do you want to use?
> Hit return to accept the default value i.e. jasmine.
Do you want to use Require.js ?
> Hit return to accept the default value i.e. no.
Do you want to capture any browsers automatically ?
> Hit return to accept the default value i.e. Chrome.
What is the location of your source and test files ?
Enter the following value:
> tests/*.test.js
จาก Wizard ข้างบน ส่วนสำคัญ คือ locaton ที่เรากำหนดให้ test file ในตัวอย่างนี้ คือ tests/*.test.js
นั่นหมายความว่า เราจะต้องสร้าง test / spec ไว้ที่ folder ดังกล่าว
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
รัน karma เพื่อเริ่มต้น test spec ที่เราเขียนไป karma start karma.conf.js
จะเห็นได้ว่า report ไม่สวยเท่าไหร่ ให้ติดตั้ง reporter ตัวนี้ดู npm install karma-spec-reporter --save-dev
https://www.npmjs.com/browse/keyword/karma-reporter https://www.npmjs.com/package/karma-spec-reporter
http://karma-runner.github.io/0.12/intro/configuration.html