Project structure

将日志记录提取到单独的模块

新建文件夹utils, 新建文件logger.js

  1. const info = (...params) => {
  2. console.log(...params);
  3. }
  4. const error = (...params) => {
  5. console.error(...params)
  6. }
  7. module.exports = {
  8. info, error
  9. }

exercise 4.1-4.2

创建一个bloglist应用
npm init 初始化
npm install express
npm install —save-dev nodemon
在scripts添加 “dev”:”nodemon index.js”
npm install cors
npm install mongoose
npm install dotenv

Testing Node applications

Jest 是一个由 Facebook 内部开发和使用的测试库,它可以很好地测试后端,并且在测试 React 应用时表现出色。 (Windows 用户: 如果项目目录的路径所包含的目录名称含有空格, Jest 可能无法工作。)

安装Jest为开发依赖:

  1. npm install --save-dev jest

修改package.json中的scripts中的test, 以verbose样式报告测试执行情况:

  1. "test": "jest --verbose"

在package.json末尾添加如下内容,指定jest执行环境为node

  1. "jest": {
  2. "testEnvironment": "node"
  3. }

在utils文件夹创建for_testing.js文件

  1. const palindrome = (string) => {
  2. return string
  3. .split('')
  4. .reverse()
  5. .join('')
  6. }
  7. const average = (array) => {
  8. const reducer = (sum, item) => {
  9. return sum + item
  10. }
  11. return array.length === 0 ? 0 : array.reduce(reducer, 0) / array.length
  12. }
  13. module.exports = {
  14. palindrome,
  15. average
  16. }

新建tests文件夹
新建palindrome.test.js文件(Jest 默认情况下希望测试文件的名称包含 .test. )

  1. const palindrome = require('../utils/for_testing').palindrome
  2. test('palindrome of a', () => {
  3. const result = palindrome('a')
  4. expect(result).toBe('a')
  5. })
  6. test('palindrome of react', () => {
  7. const result = palindrome('react')
  8. expect(result).toBe('tcaer')
  9. })
  10. test('palindrome of releveler', () => {
  11. const result = palindrome('releveler')
  12. expect(result).toBe('releveler')
  13. })

新建average.test.js文件
这里定义了describe块, 描述块可用于将测试分组为逻辑集合。

  1. const average = require('../utils/for_testing').average
  2. describe('average', () => {
  3. test('of one value is the value itself', () => {
  4. expect(average([1])).toBe(1)
  5. })
  6. test('of many is calculated right', () => {
  7. expect(average([1, 2, 3, 4, 5, 6])).toBe(3.5)
  8. })
  9. test('of empty array is zero', () => {
  10. expect(average([])).toBe(0)
  11. })
  12. })

运行命令 **npm test**,控制台会显示测试结果

exercise 4.2 - 4.7

list_helper.js

  1. const dummy = (blogs) => {
  2. return 1
  3. }
  4. const totalLikes = (blogs) => {
  5. return blogs.reduce((sum, currrent) => sum + currrent.likes, 0)
  6. }
  7. module.exports = {
  8. dummy,
  9. totalLikes
  10. }

list.test.js

  1. const listHelper = require('../utils/list_helper')
  2. describe('dummy', () => {
  3. test('dummy returns one', () => {
  4. const blogs = []
  5. const result = listHelper.dummy(blogs)
  6. expect(result).toBe(1)
  7. })
  8. })
  9. describe("totalLikes", () => {
  10. const listWithOneBlog = [
  11. {
  12. _id: '5a422aa71b54a676234d17f8',
  13. title: 'Go To Statement Considered Harmful',
  14. author: 'Edsger W. Dijkstra',
  15. url: 'http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html',
  16. likes: 5,
  17. __v: 0
  18. }
  19. ]
  20. const blogs = [
  21. {
  22. _id: "5a422a851b54a676234d17f7",
  23. title: "React patterns",
  24. author: "Michael Chan",
  25. url: "https://reactpatterns.com/",
  26. likes: 7,
  27. __v: 0
  28. },
  29. -- snip --
  30. ]
  31. test('when list has only one blog, equals the likes of that', () => {
  32. const result = listHelper.totalLikes(listWithOneBlog)
  33. expect(result).toBe(5)
  34. })
  35. test('a lot of blogs', () => {
  36. const result = listHelper.totalLikes(blogs)
  37. expect(result).toBe(36)
  38. })
  39. })

npm test运行所有测试
npm test -- -t 'a log of blogs' 进行单个测试
image.png
如果是比较对象的值,用toEqual

  1. const can1 = {
  2. flavor: 'grapefruit',
  3. ounces: 12,
  4. };
  5. const can2 = {
  6. flavor: 'grapefruit',
  7. ounces: 12,
  8. };
  9. describe('the La Croix cans on my desk', () => {
  10. test('have all the same properties', () => {
  11. expect(can1).toEqual(can2);
  12. });
  13. test('are not the exact same can', () => {
  14. expect(can1).not.toBe(can2);
  15. });
  16. });

Lodash

官方文档
中文文档
Lodash 通过降低 array、number、objects、string 等等的使用难度从而让 JavaScript 变得更简单。 Lodash 的模块化方法 非常适用于:

  • 遍历 array、object 和 string
  • 对值进行操作和检测
  • 创建符合功能的函数