[Unit Testing] What is Unit Testing. 1 - install Karma and test
What is Unit Testing:
Unit tests try to answer questions such as "Did I think about the logic correctly?" or "Does the sort funciton order the list in the right order?"
----------------------Install Karma--------------
1. using cmd, type:
npm install -g Karma
npm install -g karma-cli
2. In webstrom, open Terminal and type:
karma init
3. You will get a karma.config.js file,
put the name of the javascirpt files which you want to test into files array:
// Karma configuration // Generated on Mon Sep 01 2014 12:35:38 GMT+0300 (FLE Daylight Time) module.exports = function(config) { config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) basePath: '', // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['jasmine'], // list of files / patterns to load in the browser files: [ "test/test.js" ], // list of files to exclude exclude: [ ], // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { }, // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ['progress'], // web server port port: 9876, // enable / disable colors in the output (reporters and logs) colors: true, // level of logging // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG logLevel: config.LOG_INFO, // enable / disable watching file and executing tests whenever any file changes autoWatch: true, // start these browsers // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher browsers: ['Chrome'], // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits singleRun: false }); };
4. In test.js we add some code:
describe("Hello",function(){ it("should work",function(){ expect(false).toBe(true) }) })
5. Click karma.config.js to "run" the file. We will get a error, because we expect false to be true.
"C:\Program Files\nodejs\node.exe" "F:\Program Files (x86)\JetBrains\WebStorm 8.0.4\plugins\js-karma\js_reporter\karma-intellij\lib\intellijRunner.js" --karmaPackageDir=C:\Users\Answer1215\node_modules\karma --serverPort=9876 --urlRoot=/ Expected false to be true. Error: Expected false to be true. at null.<anonymous> (C:/Users/Answer1215/WebstormProjects/angular/Kama_Demo/test/test.js:3:23) Process finished with exit code 0
6. Change the code in test.js:
describe("hello",function(){ it("Should work",function(){ expect(false).toBe(false); }) })
7. Run again , we pass the test.
"C:\Program Files\nodejs\node.exe" "F:\Program Files (x86)\JetBrains\WebStorm 8.0.4\plugins\js-karma\js_reporter\karma-intellij\lib\intellijRunner.js" --karmaPackageDir=C:\Users\Answer1215\node_modules\karma --serverPort=9876 --urlRoot=/ Process finished with exit code 0
分类:
Unit Testing
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具