Continuous Integration, Static Verification, Linting, and HTTP Servers in action 持续集成、静态验证、代码检查与HTTP服务器的实战应用
main branch green using runners and staged jobs.
配置 GitLab CI 流水线,使用 runner 与多阶段任务确保 main 分支始终为绿色状态。
sync-request-curl.
设计并测试 HTTP/Express 服务器,通过 Jest + sync-request-curl 自动化发起请求。
Add a .gitlab-ci.yml that runs on every push. GitLab takes the commit, spins up a runner, executes the defined stages (e.g. npm install, npm run test) and reports a green tick when successful.
在每次 push 时执行 .gitlab-ci.yml。GitLab 获取提交、启动 runner 执行各阶段(如 npm install、npm run test),成功后给予绿色勾选。
checks, deploy etc. (Lecture 3.1)阶段:checks、deploy 等(3.1 讲)TypeScript guards against unexpected inputs. Moving runtime checks into compile-time prevents silent failures and documents expectations.
TypeScript 可防止意外输入,将运行时检查前移到编译期,避免无声失败并记录预期。
zod or manual guards with TS types.结合 zod 或手动 guard 与 TS 类型。ESLint/Prettier enforce style rules so code remains human-friendly—indentation, naming, unused variables, maximum complexity.
ESLint/Prettier 可强制风格规则,让代码更易读——缩进、命名、未使用变量、复杂度上限。
Express servers respond to HTTP requests; use sync-request-curl and Jest to script integration tests for each endpoint.
Express 服务器响应 HTTP 请求;通过 sync-request-curl 与 Jest 编写每个端点的集成测试。
image: comp1531/basic:latest
stages:
- checks
install:
stage: checks
script:
- npm install
unit-tests:
stage: checks
script:
- npm run test
only:
- main
- merge_requests
Keep each stage idempotent and fast; merge requests must show a green pipeline before approval.
保持每个阶段幂等且快速;合并请求需在审批前显示绿色流水线。
type RepeatArgs = {
repeat: number;
value: string;
};
export function manyString({ repeat, value }: RepeatArgs): string {
if (!Number.isInteger(repeat) || repeat < 0) {
throw new Error('repeat must be a non-negative integer');
}
return value.repeat(repeat);
}
Prefer descriptive arguments and guards instead of silently returning unexpected values.
使用描述性参数与 guard,避免静默返回异常结果。
// server.ts
app.post('/auth/login', (req, res) => {
const { email, password } = req.body;
if (!isValid(email, password)) {
return res.status(400).json({ error: 'Invalid credentials' });
}
return res.status(200).json({ token: issueToken(email) });
});
// requests.test.ts
import request from 'sync-request-curl';
describe('POST /auth/login', () => {
test('rejects wrong password', () => {
const res = request('POST', `${BASE_URL}/auth/login`, {
json: { email: 'test@unsw.edu.au', password: 'wrong' },
});
expect(res.statusCode).toBe(400);
});
});
Automated requests keep regressions out of the server. Status codes form part of assessment marking.
自动请求可防止服务器回归。状态码也是评分要点。
src/dataStore.ts and expose typed helpers.将数据存储逻辑拆入 src/dataStore.ts 并暴露类型化 helper。sync-request-curl for scripted HTTP calls.安装 sync-request-curl 用于脚本化 HTTP 请求。dotenv to manage secrets locally.用 dotenv 管理本地密钥。npm run lint, npm run test) for CI.新增 npm 脚本(如 npm run lint、npm run test)供 CI 调用。test.only/test.skip while iterating.迭代开发时使用 test.only/test.skip。addUser, addSong, listPlaylist.为 addUser、addSong、listPlaylist 设计类型接口。npm run lint, record two warnings, and commit the fixes.
Lint 修复: 执行 npm run lint,记录两条警告并提交修复。
sync-request-curl tests covering both success & failure responses for each endpoint.
HTTP 测试工具: 使用 Jest + sync-request-curl 为每个端点编写成功与失败两类测试。
Head to Week 3 Quiz and look for questions labelled CI, TS, Lint, HTTP. 前往 第3周测验,关注标记为 CI、TS、Lint、HTTP 的题目。
Recommended reading: Lecture 3.1 Slides, Lecture 3.2 Slides, Lecture 3.3 Slides, Lecture 3.4 Slides. 推荐阅读:第3.1讲 PPT、第3.2讲 PPT、第3.3讲 PPT、第3.4讲 PPT。