Node unittests pass with strange warning

I understand that this may be more or less a mixture of JEST and NR issue.
But I just hope someone may shed some light on it.

Setup

OS: macOS

JEST version:

$ jest --version
29.4.2

Goal

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

I'm testing Node-RED by wrapping its node-red-node-test-helper companion package, with which I write a wrapper class to use the helper internally:

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);
			});
		});
	}

...

Here to clarify, due to architecture decisions, we found that node-red-node-test-helper cannot be installed as a local dependency inside the wrapper's package, but have to be installed globally instead. So we use absolaute path to require it.

Then in my individual node (think of it as a NodeJS package) package.json


{

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

}

Finally I run the test:

cd my_node
npm test

Problem

Although my JEST tests all pass, but a warning pops up:

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

Analysis

The error looks like a type error to me. But the "mock" keyword among the callstack is suspicious because I haven't called any mocking API.

Question

A casual internet search didn't get me anywhere close.
I have no clue how a simple wrapper can cause this.
Is it the way I require the global package that's wrong?

Finally solved this.
The problem is that I misused require when importing node-red module.
The warning is gone after I changed this line

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

to

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

Basically I must use require.resolve() when importing node-red here.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.