Project structure
将日志记录提取到单独的模块
新建文件夹utils, 新建文件logger.js
const info = (...params) => {
console.log(...params);
}
const error = (...params) => {
console.error(...params)
}
module.exports = {
info, error
}
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为开发依赖:
npm install --save-dev jest
修改package.json中的scripts中的test, 以verbose样式报告测试执行情况:
"test": "jest --verbose"
在package.json末尾添加如下内容,指定jest执行环境为node
"jest": {
"testEnvironment": "node"
}
在utils文件夹创建for_testing.js文件
const palindrome = (string) => {
return string
.split('')
.reverse()
.join('')
}
const average = (array) => {
const reducer = (sum, item) => {
return sum + item
}
return array.length === 0 ? 0 : array.reduce(reducer, 0) / array.length
}
module.exports = {
palindrome,
average
}
新建tests文件夹
新建palindrome.test.js文件(Jest 默认情况下希望测试文件的名称包含 .test. )
const palindrome = require('../utils/for_testing').palindrome
test('palindrome of a', () => {
const result = palindrome('a')
expect(result).toBe('a')
})
test('palindrome of react', () => {
const result = palindrome('react')
expect(result).toBe('tcaer')
})
test('palindrome of releveler', () => {
const result = palindrome('releveler')
expect(result).toBe('releveler')
})
新建average.test.js文件
这里定义了describe块, 描述块可用于将测试分组为逻辑集合。
const average = require('../utils/for_testing').average
describe('average', () => {
test('of one value is the value itself', () => {
expect(average([1])).toBe(1)
})
test('of many is calculated right', () => {
expect(average([1, 2, 3, 4, 5, 6])).toBe(3.5)
})
test('of empty array is zero', () => {
expect(average([])).toBe(0)
})
})
exercise 4.2 - 4.7
list_helper.js
const dummy = (blogs) => {
return 1
}
const totalLikes = (blogs) => {
return blogs.reduce((sum, currrent) => sum + currrent.likes, 0)
}
module.exports = {
dummy,
totalLikes
}
list.test.js
const listHelper = require('../utils/list_helper')
describe('dummy', () => {
test('dummy returns one', () => {
const blogs = []
const result = listHelper.dummy(blogs)
expect(result).toBe(1)
})
})
describe("totalLikes", () => {
const listWithOneBlog = [
{
_id: '5a422aa71b54a676234d17f8',
title: 'Go To Statement Considered Harmful',
author: 'Edsger W. Dijkstra',
url: 'http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html',
likes: 5,
__v: 0
}
]
const blogs = [
{
_id: "5a422a851b54a676234d17f7",
title: "React patterns",
author: "Michael Chan",
url: "https://reactpatterns.com/",
likes: 7,
__v: 0
},
-- snip --
]
test('when list has only one blog, equals the likes of that', () => {
const result = listHelper.totalLikes(listWithOneBlog)
expect(result).toBe(5)
})
test('a lot of blogs', () => {
const result = listHelper.totalLikes(blogs)
expect(result).toBe(36)
})
})
npm test
运行所有测试npm test -- -t 'a log of blogs'
进行单个测试
如果是比较对象的值,用toEqual
const can1 = {
flavor: 'grapefruit',
ounces: 12,
};
const can2 = {
flavor: 'grapefruit',
ounces: 12,
};
describe('the La Croix cans on my desk', () => {
test('have all the same properties', () => {
expect(can1).toEqual(can2);
});
test('are not the exact same can', () => {
expect(can1).not.toBe(can2);
});
});
Lodash
官方文档
中文文档
Lodash 通过降低 array、number、objects、string 等等的使用难度从而让 JavaScript 变得更简单。 Lodash 的模块化方法 非常适用于:
- 遍历 array、object 和 string
- 对值进行操作和检测
- 创建符合功能的函数