nodejs:js-mdict 的下载、安装、测试、build

js-mdict 项目的目录结构:js-mdict 项目教程

js-mdict 下载地址: js-mdict-master.zip 先解压到 D:\Source\

js-mdict 6.0.2 用了 ts (TypeScript) 和 Jest,增加了应用开发的难度,因为先要了解 ts 和 Jest。

参阅:测试与开发:Jest测试框架介绍

Jest 是最流行的 JavaScript 测试框架之一。测试人员广泛使用 Jest 对 JavaScript 函数进行单元测试。Jest 确保 JavaScript 应用程序具有生成正确输出的工作代码。它也非常直观,因为 Jest 语法非常可读。在使用 Jest 测试框架时,我们使用其功能丰富的 API,该 API 快速且用户友好。 由于 Jest 是开源的,因此对测试框架有很好的社区支持。

Win 10 运行 cmd

nvm list available

nodejs 版本选择,请 visit https://nodejs.org/en/download/releases

v22.13.1 , v20.18.2 , v18.20.6 任选一个

复制代码
D:\nvm>nvm install 18.20.6
Downloading node.js version 18.20.6 (64-bit)...
Extracting node and npm...
Complete
npm v10.8.2 installed successfully.

nvm list

nvm use 18.20.6

node -v

npm -v

where yarn

cd \Source\js-mdict-master

dir

复制代码
D:\Source\js-mdict-master> yarn install
yarn install v1.22.19
info No lockfile found.
[1/5] Validating package.json...
[2/5] Resolving packages...
warning @jest/globals > @jest/expect > jest-snapshot > @jest/transform > babel-plugin-istanbul > test-exclude > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning @jest/globals > @jest/expect > jest-snapshot > @jest/transform > babel-plugin-istanbul > test-exclude > glob > inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning jest > @jest/core > jest-config > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning jest > @jest/core > jest-runtime > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning jest > @jest/core > @jest/reporters > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning nyc > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning nyc > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning nyc > istanbul-lib-processinfo > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning nyc > spawn-wrap > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning nyc > rimraf > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning shx > shelljs > glob@7.2.3: Glob versions prior to v9 are no longer supported
[3/5] Fetching packages...
[4/5] Linking dependencies...
[5/5] Building fresh packages...
success Saved lockfile.
Done in 64.70s.

yarn list

输出清单太长

\Source\js-mdict-master\ 生成目录 node_modules 大约70MB

npm show jest

jest@29.7.0 | MIT | deps: 4 | versions: 354

Delightful JavaScript Testing.

https://jestjs.io/

D:\Source\js-mdict-master> where jest

jest -verbose

D:\Source\js-mdict-master> npm run test

Test Suites: 27 failed, 3 passed, 30 total

Tests: 28 passed, 28 total

Snapshots: 0 total

Time: 32.163 s

Ran all test suites.

失败率大约50%,是因为./test/data/ 缺少用来测试的字典文件。


修改 \Source\js-mdict-master\test\max-000-baseline.test.ts

describe('Mdict', () => {

describe('#lookup', () => {

const mdict = new MDX('./test/data/oald7.mdx', {

修改为:const mdict = new MDX('/js/testdict/oale8.mdx', {

npm test baseline

复制代码
      87 |     it("should be 'bad'", () => {
      88 |       const def = mdict.lookup('bad');

      at Object.<anonymous> (test/max-000-baseline.test.ts:85:60)

  ● Mdict › #lookup › should be 'bad'

    expect(received).toBeTruthy()

    Received: false

       95 |       expect(def.definition.length > 0).toBeTruthy();
       96 |       const expect_str = '<head><link rel="stylesheet" type="text/css" href="O7.css"/>';
    >  97 |       expect(def.definition.startsWith(expect_str.trim())).toBeTruthy();
          |                                                            ^
       98 |     });
       99 |   });
      100 | });

      at Object.<anonymous> (test/max-000-baseline.test.ts:97:60)

Test Suites: 1 failed, 1 total
Tests:       7 failed, 7 total
Snapshots:   0 total
Time:        3.105 s, estimated 14 s
Ran all test suites matching /baseline/i.

还是 test 失败。

dir \js\testdict\oale8.mdx

文件路径没有问题。


npm how to run typescript ?

npx -h

npm help exec

npx tsx -h

Node.js runtime enhanced with esbuild for loading TypeScript & ESM

如何使用 npm 运行 TypeScript 文件

为了能够使用 npm 来运行 TypeScript 文件,项目需配置好必要的开发环境。这涉及到创建并编辑几个重要的文件。

创建 package.json 和 tsconfig.json 文件

首先,确保项目的根目录下存在 package.json 文件。此文件定义了项目的元数据以及脚本命令

接着,设置 tsconfig.json 文件来指定编译选项。该文件告诉 TypeScript 编译器如何处理源码中的各种特性:

TypeScript 复制代码
{
  "compileOnSave": true,
  "compilerOptions": {
    "esModuleInterop": true,
    "target": "ES6",
    "module": "CommonJS",
    "strict": true,
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": true,

    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "sourceMap": true,
    "outDir": "dist",
    "lib": ["DOM", "ES2022"],
    "declaration": true,
    "paths": {
      "../src/*": ["./*"]
    }
  },
  "exclude": ["node_modules"],
  "include": ["src/**/*.ts", "tests/**/*.ts", "examples/**/*.ts"],
}

上述配置指定了目标 ECMAScript 版本、模块解析方式以及其他一些有用的标志位;同时设置了输出路径为 dist 并包含了所有位于 src/ 下的 .ts 文件作为输入。

打开 package.json 文件并向其中添加一个或多个方便调用的 script 脚本条目以便于后续快速启动编译流程:

复制代码
"scripts": {
  "build": "tsc"
},

现在可以在终端里仅需输入简单的指令就可以触发整个工程下的所有 .ts 文件被转换成对应的 js 文件: npm run build

复制代码
D:\Source\js-mdict-master> npm -v
10.8.2

D:\Source\js-mdict-master> npm run build

> js-mdict@6.0.2 build
> npm run build-cjs && npm run build-esm


> js-mdict@6.0.2 build-cjs
> tsc --module commonjs --outDir dist/cjs/ && echo '{"type": "commonjs"}' > dist/cjs/package.json


> js-mdict@6.0.2 build-esm
> tsc  --module nodenext --moduleResolution nodenext --outDir dist/esm/ && echo '{"type": "module"}' > dist/esm/package.json

cd D:\Source\js-mdict-master

npx tsx ./example/oald7-example.ts

npx tsx ./example/oale8-mdd-example.ts

相关推荐
Java知识技术分享6 天前
安装yarn 指南(通过 npm)
个人开发·ai编程·yarn
小葛要努力6 天前
安装nvm 管理node.js版本实现vue2和vue3项目共存
node.js·vue·nvm
Qres8216 天前
nodejs安装记录
后端·nodejs
冷色调的咖啡师18 天前
4.大数据高可用
大数据·linux·hadoop·hdfs·zookeeper·yarn
冷色调的咖啡师18 天前
1.大数据架构技术 上——搭建分布式Hadoop集群
大数据·linux·hadoop·分布式·hdfs·架构·yarn
春波petal18 天前
Mac nvm 永久固定 Node 版本(新开终端自动生效)
macos·nvm
linksinke19 天前
Node.js 版本管理管理器的使用注意 - NVM
node.js·nvm·node多版本·node切换
liu_bees1 个月前
nvm 极简教程:告别Node版本冲突!Windows下一键切换Node.js版本nvm安装与常用命令
windows·node.js·nvm
TechExplorer3651 个月前
Node.js安装及配置_基于NVM管理Node版本
nodejs·nvm