Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a private, not exported, function

I have a www.js file in my bin folder. Which is responsible for starting my server application.

const http = require('http');

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

http.createServer(app).listen(process.env.SERVER_PORT || 3000);

As you can see there is no export here. node will run this file (cmd: node bin/www) and my server will start. However it still has some logic in it that I need to test.

Test cases I though:

  • It should create server with my express instance
  • It should listen from port 3000 if there is no environment variable provided
  • It should listen from port that is provided from environment variable if it's given.

I use jest for my tests.

Since this file does not export anything, I cannot import it in a jest test file. How can I test now?

like image 649
Mansur Avatar asked May 07 '26 14:05

Mansur


1 Answers

Unit test solution:

www.js:

const http = require('http');
const app = require('./app');

http.createServer(app).listen(process.env.SERVER_PORT || 3000);

app.js:

const express = require('express');
const app = express();

module.exports = app;

www.test.js:

const http = require('http');
const app = require('./app');

describe('64768906', () => {
  afterEach(() => {
    jest.resetModules();
  });
  afterAll(() => {
    jest.restoreAllMocks();
  });
  it('should create server with my express instance and  listen from port 3000', () => {
    const server = { listen: jest.fn() };
    const createServerSpy = jest.spyOn(http, 'createServer').mockReturnValue(server);
    require('./www');
    expect(createServerSpy).toBeCalledWith(app);
    expect(server.listen).toBeCalledWith(3000);
  });
  it("should listen from port that is provided from environment variable if it's given.", () => {
    const SERVER_PORT = process.env.SERVER_PORT;
    process.env.SERVER_PORT = 4000;
    const server = { listen: jest.fn() };
    const createServerSpy = jest.spyOn(http, 'createServer').mockReturnValue(server);
    require('./www');
    expect(createServerSpy).toBeCalledWith(app);
    expect(server.listen).toBeCalledWith('4000');
    process.env.SERVER_PORT = SERVER_PORT;
  });
});

unit test result:

 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
like image 89
slideshowp2 Avatar answered May 09 '26 02:05

slideshowp2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!