Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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 如何测试私有而非导出的函数_Javascript_Node.js_Unit Testing_Jestjs_Private - Fatal编程技术网

Javascript 如何测试私有而非导出的函数

Javascript 如何测试私有而非导出的函数,javascript,node.js,unit-testing,jestjs,private,Javascript,Node.js,Unit Testing,Jestjs,Private,我的bin文件夹中有一个www.js文件。它负责启动我的服务器应用程序 const http = require('http'); const app = require('../app'); // This is my express application instance. http.createServer(app).listen(process.env.SERVER_PORT || 3000); 正如你所看到的,这里没有出口。节点将运行此文件(cmd:node-bin/www),

我的
bin
文件夹中有一个
www.js
文件。它负责启动我的服务器应用程序

const http = require('http');

const app = require('../app'); // This is my express application instance.

http.createServer(app).listen(process.env.SERVER_PORT || 3000);
正如你所看到的,这里没有出口。节点将运行此文件(cmd:
node-bin/www
),我的服务器将启动。然而,它仍然有一些逻辑,我需要测试

测试用例I虽然:

  • 它应该使用我的express实例创建服务器
  • 如果没有提供环境变量,它应该从端口3000侦听
  • 它应该从环境变量提供的端口侦听(如果给定)
我使用
jest
进行测试

由于此文件不导出任何内容,因此无法将其导入jest测试文件中。现在如何进行测试?

单元测试解决方案:

www.js

consthttp=require('http');
const-app=require('./app');
http.createServer(app).listen(process.env.SERVER_PORT | | 3000);
app.js

const express=require('express');
常量app=express();
module.exports=app;
www.test.js

consthttp=require('http');
const-app=require('./app');
描述('64768906',()=>{
之后(()=>{
jest.reset模块();
});
毕竟(()=>{
开玩笑。恢复记忆();
});
它('应该用我的express实例创建服务器并从端口3000侦听',()=>{
const server={listen:jest.fn()};
const createServerSpy=jest.spyOn(http,'createServer').mockReturnValue(服务器);
要求('./www');
expect(createServerSpy).toBeCalledWith(app);
expect(server.listen).toBeCalledWith(3000);
});
它(“应该从环境变量提供的端口侦听(如果给定)。,()=>{
const SERVER_PORT=process.env.SERVER_PORT;
process.env.SERVER_PORT=4000;
const server={listen:jest.fn()};
const createServerSpy=jest.spyOn(http,'createServer').mockReturnValue(服务器);
要求('./www');
expect(createServerSpy).toBeCalledWith(app);
expect(server.listen).toBeCalledWith('4000');
process.env.SERVER\u PORT=服务器\u端口;
});
});
单元测试结果:

 PASS  src/stackoverflow/64768906/www.test.js (14.082s)
  64768906
    ✓ should create server with my express instance and  listen from port 3000 (12ms)
    ✓ should listen from port that is provided from environment variable if it's given. (22ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 app.js   |      100 |      100 |      100 |      100 |                   |
 www.js   |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        15.662s, estimated 17s
单元测试解决方案:

www.js

consthttp=require('http');
const-app=require('./app');
http.createServer(app).listen(process.env.SERVER_PORT | | 3000);
app.js

const express=require('express');
常量app=express();
module.exports=app;
www.test.js

consthttp=require('http');
const-app=require('./app');
描述('64768906',()=>{
之后(()=>{
jest.reset模块();
});
毕竟(()=>{
开玩笑。恢复记忆();
});
它('应该用我的express实例创建服务器并从端口3000侦听',()=>{
const server={listen:jest.fn()};
const createServerSpy=jest.spyOn(http,'createServer').mockReturnValue(服务器);
要求('./www');
expect(createServerSpy).toBeCalledWith(app);
expect(server.listen).toBeCalledWith(3000);
});
它(“应该从环境变量提供的端口侦听(如果给定)。,()=>{
const SERVER_PORT=process.env.SERVER_PORT;
process.env.SERVER_PORT=4000;
const server={listen:jest.fn()};
const createServerSpy=jest.spyOn(http,'createServer').mockReturnValue(服务器);
要求('./www');
expect(createServerSpy).toBeCalledWith(app);
expect(server.listen).toBeCalledWith('4000');
process.env.SERVER\u PORT=服务器\u端口;
});
});
单元测试结果:

 PASS  src/stackoverflow/64768906/www.test.js (14.082s)
  64768906
    ✓ should create server with my express instance and  listen from port 3000 (12ms)
    ✓ should listen from port that is provided from environment variable if it's given. (22ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 app.js   |      100 |      100 |      100 |      100 |                   |
 www.js   |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        15.662s, estimated 17s

这是不可能的。这是JS的基础。一个显而易见的解决方案是修复模块以导出您需要的东西。没有办法做到这一点。这是JS的基础。一个显而易见的解决方案是修复模块以导出所需的内容。