JEST:moduleName.startsWith不是函数

xyhw6mcr  于 2023-02-17  发布在  Jest
关注(0)|答案(1)|浏览(181)

设置

操作系统:macOS
JEST版本:

$ jest --version
29.4.2

目标

npm install -g node-red
npm install -g node-red-node-test-helper

我正在通过 Package 它的node-red-node-test-helper配套包来测试Node-RED,我用它编写了一个 Package 器类来在内部使用helper:

class FlowTester {
    constructor() {
        
        this.helper = require(Path.resolve(`${globalNodeModuleDir}/node-red-node-test-helper`));
    }

    init() {
        this.helper.init(require(Path.resolve(`${globalNodeModuleDir}/node-red`)));
    }

    defineDefaultFixtureForEach() {
        beforeEach((done) => {
            this.helper.startServer(done);
        });

        afterEach((done) => {
            this.helper.unload().then(() => {
                this.helper.stopServer(done);
            });
        });
    }

...

这里需要澄清的是,由于架构的原因,我们发现node-red-node-test-helper不能作为本地依赖项安装在 Package 器的包中,而必须全局安装,因此我们使用绝对路径来要求它。
然后在我的单个节点中(将其视为NodeJS包)package.json

{

...
 "scripts": {
        "test": "jest"
 },
...

}

最后,我运行测试:

cd my_node
npm test

问题

虽然我的JEST测试全部通过,但弹出一个警告:

console.log
16:03:44       TypeError: moduleName.startsWith is not a function
16:03:44           at Resolver.isCoreModule (/usr/local/lib/node_modules/jest/node_modules/jest-resolve/build/resolver.js:452:20)
16:03:44           at Resolver._getModuleType (/usr/local/lib/node_modules/jest/node_modules/jest-resolve/build/resolver.js:576:17)
16:03:44           at Resolver.getModuleID (/usr/local/lib/node_modules/jest/node_modules/jest-resolve/build/resolver.js:529:29)
16:03:44           at Runtime._shouldMockCjs (/usr/local/lib/node_modules/jest/node_modules/jest-runtime/build/index.js:1815:37)
16:03:44           at Runtime.requireModuleOrMock (/usr/local/lib/node_modules/jest/node_modules/jest-runtime/build/index.js:1145:16)
16:03:44           at NodeTestHelper._initRuntime (/usr/local/lib/node_modules/node-red-node-test-helper/index.js:89:37)
16:03:44           at NodeTestHelper.init (/usr/local/lib/node_modules/node-red-node-test-helper/index.js:148:18)
16:03:44           at FlowTester.init (/Users/me/_util/node/_flowtest.js:18:15)
16:03:44           at Object.init (/Users/me/my_service/node/test/default/flow.test.js:16:9)
16:03:44           at Runtime._execModule (/usr/local/lib/node_modules/jest/node_modules/jest-runtime/build/index.js:1539:24)
16:03:44           at Runtime._loadModule (/usr/local/lib/node_modules/jest/node_modules/jest-runtime/build/index.js:1122:12)
16:03:44           at Runtime.requireModule (/usr/local/lib/node_modules/jest/node_modules/jest-runtime/build/index.js:982:12)
16:03:44           at jestAdapter (/usr/local/lib/node_modules/jest/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:77:13)
16:03:44           at processTicksAndRejections (internal/process/task_queues.js:95:5)
16:03:44           at runTestInternal (/usr/local/lib/node_modules/jest/node_modules/jest-runner/build/runTest.js:367:16)
16:03:44           at runTest (/usr/local/lib/node_modules/jest/node_modules/jest-runner/build/runTest.js:444:34)
16:03:44   
16:03:44         at NodeTestHelper._initRuntime (../../../../../../../../usr/local/lib/n

分析

这个错误看起来像是类型错误,但是调用堆栈中的“mock”关键字很可疑,因为我没有调用任何模拟API。

问题

随便搜索一下SO也没能让我接近它。我不知道一个简单的 Package 器怎么会导致这种情况。是我需要全局包的方式错了吗?

wrrgggsh

wrrgggsh1#

终于解决了,问题是导入node-red模块时误用了require,改了这一行警告就没有了

this.helper.init(require(Path.resolve(`${globalNodeModuleDir}/node-red`)));

this.helper.init(require.resolve(Path.resolve(`${globalNodeModuleDir}/node-red`)));

基本上,在这里导入node-red时,我必须使用require.resolve()

相关问题