Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 绕过测试文件中的一些require()语句_Javascript_Node.js_Jestjs - Fatal编程技术网

Javascript 绕过测试文件中的一些require()语句

Javascript 绕过测试文件中的一些require()语句,javascript,node.js,jestjs,Javascript,Node.js,Jestjs,我想在server.js文件中为myFunction()编写单元测试: 主文件:server.js const config=require('./configuration');//简短回答:在导入到server.js之后添加jest.mock('./configuration')?是server.test.js的?我曾尝试添加server.test.js,但当它到达require('../server)行时,当我进入该行时,它仍尝试在测试文件中加载实际的.configurationYes。它

我想在
server.js
文件中为
myFunction()
编写单元测试:

主文件:server.js


const config=require('./configuration');//简短回答:在导入到
server.js
之后添加
jest.mock('./configuration')
?是server.test.js的
?我曾尝试添加
server.test.js
,但当它到达
require('../server
)行时,当我进入该行时,它仍尝试在测试文件中加载实际的
.configuration
Yes。它将尝试加载该文件以生成mock。如果文件不存在,则应使用virtual:true传递options对象-检查文档中是否存在该文件
const config = require('./configuration'); // <==== local js file that breaks when unit testing
const other = require('some-module'); // <==== some module that is needed and it does not break when testing.

module.exports { myFunction };

function myFunction() {
  // ...
}

function someOtherFunctions() { /*...*/ }
const server = require('../server'); // <=== one level up in the folder hierarchy

it('should work', () => {
});