我正在尝试为包含简单提示的 Oclif 挂钩编写单元测试。我想测试钩子的输出,给出对提示的“Y”或“N”响应。
import {Hook} from '@oclif/config'
import cli from 'cli-ux'
const hook: Hook<'init'> = async function () {
const answer = await cli.prompt("Y or N?")
if(answer === 'Y') {
this.log('yes')
}
else {
this.log('no')
}
}
export default hook
Run Code Online (Sandbox Code Playgroud)
我正在使用此处描述的“fancy-test”和“@oclif/test”测试框架:https ://oclif.io/docs/testing
我试过存根提示和模拟标准输入,但都没有工作 - 存根函数不可用或输出是空字符串。
这是一个测试的尝试(不起作用,因为“cli.prompt 不是函数”):
import {expect, test} from '@oclif/test'
import cli from 'cli-ux'
import * as sinon from 'sinon';
describe('it should test the "configure telemetry" hook', () => {
test
.stub(cli, 'prompt', sinon.stub().resolves('Y'))
.stdout()
.hook('init')
.do(output …
Run Code Online (Sandbox Code Playgroud)我有一个用Typescript编写的Node.js项目,它应该作为CLI运行,并且无法node_modules
使用绝对路径导入位于目录外的模块(相对路径工作正常).可能很重要的是,我正在使用oclif框架来构建我的CLI.
我的项目安排如下:
cli
|--node_modules
|--src
|--my-module.ts
|--subdir
|--index.ts
Run Code Online (Sandbox Code Playgroud)
在my-module.ts
我有:
export class MyClass {
myClassFcn(s: string) {
return 'result'
}
}
Run Code Online (Sandbox Code Playgroud)
该index.ts
脚本包含以下内容:
import {MyClass} = require('my-module')
Run Code Online (Sandbox Code Playgroud)
当我尝试用ts-node执行我的应用程序时,我明白了
(node:10423) [MODULE_NOT_FOUND] Error Plugin: cli: Cannot find module 'my-module'
module: @oclif/config@1.6.17
task: toCached
plugin: cli
root: /home/eschmidt/Workspace/cli
Error Plugin: cli: Cannot find module 'my-module'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:571:15)
at Function.Module._load (internal/modules/cjs/loader.js:497:25)
at Module.require (internal/modules/cjs/loader.js:626:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/home/eschmidt/Workspace/cli/src/commands/create/index.ts:5:1)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Module.m._compile (/home/eschmidt/Workspace/cli/node_modules/ts-node/src/index.ts:403:23)
at …
Run Code Online (Sandbox Code Playgroud) 我正在使用 typescript、oclif 和 d3.js 等开发一个节点项目。Typescript 是在使用ocif创建项目时由 npx 配置的,因此我无法控制它的所有方面(特别是关于 TS,我不是专家。TS 很棒,但配置方面,嗯)。
不管怎样,一切都工作正常,但我最近添加了 d3 作为依赖项并尝试构建文档,但在运行它时出现此错误:
(node:22554) [ERR_REQUIRE_ESM] Error Plugin: dbacl [ERR_REQUIRE_ESM]: require() of ES Module /dbacl/node_modules/d3/src/index.js from /dbacl/src/tree/graph-tree.ts not supported.
Instead change the require of index.js in /Users/nico/Furo/Dev/dbacl/src/tree/graph-tree.ts to a dynamic import() which is available in all CommonJS modules.
Run Code Online (Sandbox Code Playgroud)
在我使用 d3 的文件中,我按照文档导入它:
(node:22554) [ERR_REQUIRE_ESM] Error Plugin: dbacl [ERR_REQUIRE_ESM]: require() of ES Module /dbacl/node_modules/d3/src/index.js from /dbacl/src/tree/graph-tree.ts not supported.
Instead change the require of index.js in /Users/nico/Furo/Dev/dbacl/src/tree/graph-tree.ts to a dynamic …
Run Code Online (Sandbox Code Playgroud) node.js ×3
oclif ×3
typescript ×2
d3.js ×1
es6-modules ×1
prompt ×1
ts-node ×1
unit-testing ×1