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

相关推荐
是阿威啊20 小时前
【第二站】本地hadoop集群配置yarn模式
大数据·linux·hadoop·yarn
QC七哥2 天前
基于tauri构建全平台应用
rust·electron·nodejs·tauri
是阿威啊3 天前
【第六站】测试本地项目连接虚拟机上的大数据集群
大数据·linux·hive·hadoop·spark·yarn
亚林瓜子4 天前
AWS Lambda 添加NodeJS依赖库层
npm·云计算·nodejs·node·aws·lambda
GDAL5 天前
腾讯云ubuntu安装nodejs环境
ubuntu·nodejs·腾讯云
Asurplus5 天前
Centos7安装Node.js环境
centos·node.js·nvm·nodesource
weibkreuz10 天前
NVM及Live Server的安装说明(保姆级教程)
nvm·webstorm·live server
LYFlied12 天前
【一句话概括】前端项目包管理器怎么选?
前端·npm·pnpm·yarn
仪***沿12 天前
基于萤火虫算法优化BP神经网络(FA - BP)实现多输出数据回归预测
yarn