Multi-file Programming, Package Management, Testing & Team Project Management多文件编程、包管理、测试与团队项目管理
Last updated: September 2025最后更新:2025年9月
Week 2 builds upon the foundation laid in Week 1, diving deeper into advanced JavaScript concepts, professional development practices, and essential team collaboration skills for software engineering projects. 第2周在第1周基础上深入学习高级JavaScript概念、专业开发实践和软件工程项目必备的团队协作技能。
JavaScript has built-in libraries that come with Node.js, similar to how C has standard libraries. These don't need to be installed separately. JavaScript有内置库随Node.js一起提供,类似于C的标准库。这些库无需单独安装。
#include <stdio.h>
int main() {
printf("Hello\n");
}
import path from 'path';
console.log(path.resolve('./'));
We can separate logic into multiple files and import them, enabling better code organization and reusability. 我们可以将逻辑分离到多个文件并导入它们,实现更好的代码组织和复用性。
// lib.js
function manyString(repeat, str) {
let result = '';
for (let i = 0; i < repeat; i++) {
result += str;
}
return result;
}
export default manyString;
// main.js
import manyString from './lib.js';
// lib.js
function manyString(repeat, str) { ... }
function addBrackets(string) {
return `(${string})`;
}
export {
manyString,
addBrackets,
};
// main.js
import { manyString, addBrackets } from './lib.js';
NPM (Node Package Manager) is automatically installed with Node.js and manages dependencies/modules/libraries for JavaScript projects. It allows you to download and use external libraries from the npmjs website. NPM(Node包管理器)与Node.js一同自动安装,管理JavaScript项目的依赖/模块/库。它允许您从npmjs网站下载和使用外部库。
npm init
Creates package.json - your project's NPM configuration file创建package.json - 项目的NPM配置文件
npm install [package-name]
# Example:
npm install date-fns
Downloads and installs external libraries下载并安装外部库
Project metadata & dependency list项目元数据和依赖列表
✅ Commit to Git提交到Git
Exact version information确切版本信息
✅ Commit to Git提交到Git
Installed dependencies已安装的依赖
❌ DON'T commit不要提交
By committing package.json and package-lock.json (but NOT node_modules), team members can run 'npm install' to get the exact same dependency versions, ensuring consistent development environments. 通过提交package.json和package-lock.json(但不提交node_modules),团队成员可以运行'npm install'来获得完全相同的依赖版本,确保一致的开发环境。
Software failures can be catastrophic and expensive. In 2016, software failures in the US cost $1.1 trillion and affected 4.4 billion customers. Testing helps prevent bugs, reduce development costs, and improve performance. 软件故障可能是灾难性和昂贵的。2016年,美国软件故障损失1.1万亿美元,影响44亿客户。测试有助于预防bug、降低开发成本并提高性能。
Great testing treats functions as "black boxes" - focus on inputs and outputs without worrying about internal implementation. This abstraction allows you to write tests even before implementing the function. 优秀的测试将函数视为"黑盒"——专注于输入和输出,不关心内部实现。这种抽象允许您甚至在实现函数之前就编写测试。
Jest is a popular JavaScript testing framework that provides structured testing capabilities with powerful matchers and descriptive output. Jest是流行的JavaScript测试框架,提供结构化测试能力,具有强大的匹配器和描述性输出。
# Install Jest as development dependency
npm install --save-dev jest
# Add test script to package.json
{
"scripts": {
"test": "jest"
}
}
import { removeVowels } from './lib.js';
describe('removeVowels function', () => {
test('handles strings with no vowels', () => {
expect(removeVowels('bcd')).toEqual('bcd');
expect(removeVowels('xyz')).toEqual('xyz');
});
test('removes all vowels correctly', () => {
expect(removeVowels('hello')).toEqual('hll');
expect(removeVowels('javascript')).toEqual('jvscrpt');
});
});
Yesterday's progress昨天的进展
Current problems当前问题
Today's plan今天的计划
npm init
npm install
npm install [package]
npm run [script]
export { func };
import { func } from './file.js';
export default func;
import func from './file.js';
describe('module', () => {
test('behavior', () => {
expect(result).toEqual(expected);
});
});