Bypass some of the require() statements in the testing file - javascript

I want to write unit tests for myFunction() in the server.js file:
Main file: server.js
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() { /*...*/ }
Test file: server.test.js:
const server = require('../server'); // <=== one level up in the folder hierarchy
it('should work', () => {
});
Problem:
jest breaks at const config = require('./configuration') and I don't actually need this to test myFunction.
However, I need the second: require('some-module').
I need to mock or bypass the first const config = require('./configuration').
Question:
How can I only import the myFunction() from server.js and somehow mock the first require(...) statements in the server.js ?

Related

Jest TestEnvironment - TypeError: Cannot add property next, object is not extensible

I want to test a node API using Jest. I am testing the routes and websockets. Testing the routes was no problem. I simply started the server using the setupFile option.
To test the websockets I wanted to pass the io object to the tests. This is not possible through the setupFile since the tests are running in their own context. Thus I changed to the testEnvironment option. My testEnvironment file is the following
const NodeEnvironment = require('jest-environment-node');
class CustomEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context);
this.setupServer();
}
async setup() {
await super.setup();
console.log('Setup Test Environment.');
this.global.io = this.io;
this.global.baseUrl = 'http://localhost:' + this.port;
}
async teardown() {
await super.teardown();
console.log('Teardown Test Environment.');
}
getVmContext() {
return super.getVmContext();
}
setupServer() {
// Code for starting the server and attaching the io object
this.port = portConfig.http;
this.io = io;
}
}
module.exports = CustomEnvironment;
This works and the io object is passed to the test. I have multiple test files for different parts of the API. Running those with the setupFile was no problem but now Jest is only able to run one file. All following test suites are failing with the following message
● Test suite failed to run
TypeError: Cannot add property next, object is not extensible
at Function.handle (node_modules/express/lib/router/index.js:160:12)
at Function.handle (node_modules/express/lib/application.js:174:10)
at new app (node_modules/express/lib/express.js:39:9)
I am not able to find any documentation on that error. I tried disabling some of the test files but it always fails after the first one, no matter which one it is.
The structure of the test files is the following if relevant:
const axios = require('axios');
describe('Test MODULE routes', () => {
const baseUrl = global.baseUrl;
const io = global.io;
const models = require('../../../models'); // sequelize models which are used in tests
describe('HTTP METHOD + ROUTE', () => {
test('ROUTE DESCRIPTION', async () => {
const response = await axios({
method: 'get',
url: baseUrl + 'ROUTE'
});
expect(response.status).toBe(200);
});
});
// different routes
});
I fixed the error. It had nothing to do with jest but with an module.exports invocation in the server setup which overwrote the export of the CustomEnvironment with an express server.

Nodejs pass log instance between modules

I’ve logger which I initiate using a constractor in the index.js file. Now I need
To pass the logger instance to other files, and I do it like this
index.js
const books = require(“./books”);
books(app, logger);
logger = initLogger({
level: levels.error,
label: “app”,
version: "0.0.1",
});
app.listen(port, () => logger.info(`listening on port ${port}`));
And inside the books.js file I use it like following, get the logger from the index.js file and use it
inside the books.js file, also pass it to another file with the function isbn.get(books, logger);,
Is it recommended to do it like this? Is there a cleaner way in nodes ?
books.js
const isbn = require(“./isbn”);
module.exports = async function (app, logger) {
…
try {
Let books = await getBooks();
logger.info(“get “books process has started”);
} catch (err) {
logger.error("Failed to fetch books", err);
return;
}
…
// this function is from the file “isbn” and I should pass the logger to it also
try {
let url = await isbn.get(books, logger);
} catch (e) {
res.send(e.message);
}
}
Try creating a module specifically for your logger configuration, then you can import that into your modules instead of using a side-effect of your business module to create a logger.
This will help if you ever need/want to change your logger configuration - instead of following a chain of business methods, you can just update the log configuration.
Example
logger.js
'use strict';
// Any setup you need can be done here.
// e.g. load log libraries, templates etc.
const log = function(level, message) {
return console.log(level + ": " + message);
};
module.exports = log;
business-logic.js
'use strict';
var log = require('./logger');
var stuff = require('./stuff');
const do_stuff = function (thing) {
// do stuff here
log("INFO", "Did stuff");
}
This is a pretty clean way of doing it, however it could be awkward when trying to share more variables or adding more requires. So, you could put all the variables in an object and destructure only the variables you need in books.js:
index.js:
const state = {app, logger, some, other, variables};
require("./books")(state);
require("./another_file")(state);
books.js:
module.exports = async function ({app, logger}) {
};

Load nodejs module dynamically with mocked filesystem in jest unit tests?

Well I'm trying to unit test a module loading script. Part of it is of course creating mocks for the scripts. For this mock-fs library is used.
import mock = require("mock-fs");
describe("getFileConfig", function () {
beforeEach(() => {
jest.resetModules();
});
afterEach(() => {
mock.restore();
});
it("should read and parse .js from ./config", async () => {
const expected = {
abc: 1,
fun: function() {return true;}
};
mock({config: {"myCfg.js": "module.exports = {abc: 1, fun: function(){return true;}}"}});
const test = require("./config/myCfg.js");
expect(test.abc).toEqual(1);
});
});
I "think" this should work? - mock creates a local copy of the file systems right?
However when I test above test the following error occurs:
Error: Cannot find module './config/myCfg.js' from 'test/loader.test.ts'
So this means the module loader is not using the mocked filesystem? How can make it use the mocked filesystem?

How to clear a module mock between tests in same test suite in Jest?

I've mocked some nodejs modules (one of them, for example, is fs). I have them in a __mocks__ folder (same level als node_modules) folder and the module mocking works. However, whichever "between test clearing" option I use, the next test is not "sandboxed". What is going wrong here?
A very simplified example of the mocked fs module is:
// __mocks__/fs.js
module.exports = {
existsSync: jest.fn()
.mockReturnValueOnce(1)
.mockReturnValueOnce(2)
.mockReturnValueOnce(3)
}
I'm simply expecting that in every test, whenever init() is called (see below), existsSync starts again at value 1: the first value of jest.fn().mockReturnValue(). In the testfile I have the following structure:
// init.test.js
const init = require("../init");
const { existsSync } = require("fs");
jest.mock("fs");
describe("initializes script", () => {
afterEach(() => {
// see below!
});
test("it checks for a package.json in current directory", () => {
init();
});
test("it stops script if there's a package.json in dir", () => {
init(); // should be run in clean environment!
});
}
And once again very simplified, the init.js file
const { existsSync } = require("fs");
console.log("value of mocked response : ", existsSync())
I'm getting the following results for existsSync() after the first and second run ofinit() respectively when I run in afterEach():
jest.resetModules() : 1, 2
existsSync.mockReset(): 1, undefined
existsSync.mockClear(): 1, 2
existsSync.mockRestore(): 1, undefined
Somebody know what I'am doing wrong? How do I clear module mock between tests in the same suite? I'll glady clarify if necessary. Thanks!
Reset the modules and require them again for each test:
describe("initializes script", () => {
afterEach(() => {
jest.resetModules()
});
beforeEach(() => {
jest.mock("fs");
})
test("it checks for a package.json in current directory", () => {
const init = require("../init");
init();
});
test("it stops script if there's a package.json in dir", () => {
const init = require("../init");
init();
});
}
I had problems with the solution above. I managed to solve the issue with the next snippet.
afterEach(() => {
Object.keys(mockedModule).forEach(method => mockedModule[method].mockReset())
})
I would prefer to have a native method doing this though. Something like mockedModule.mockReset().
For local variables, the scope of declaration is important.
const mockFunc1 = jest.fn() // possibly bad mock reset/clear between tests
describe('useGetMetaData', () => {
const mockFunc2 = jest.fn() // good mock reset/clear between tests
afterEach(() => {/* reset/clear mocks */})
test.todo('implement tests here')
})

Require not behaving as expected

I'm using the proxyquire library, which mocks packages on import.
I'm creating my own proxyquire function, which stubs a variety of packages I use regularly and want to stub regularly (meteor packages, which have a special import syntax):
// myProxyquire.js
import proxyquire from 'proxyquire';
const importsToStub = {
'meteor/meteor': { Meteor: { defer: () => {} } },
};
const myProxyquire = filePath => proxyquire(filePath, importsToStub);
export default myProxyquire;
Now I want to write a test of a file which uses one of these packages:
// src/foo.js
import { Meteor } from 'meteor/meteor'; // This import should be stubbed
export const foo = () => {
Meteor.defer(() => console.log('hi')); // This call should be stubbed
return 'bar';
};
And finally I test it like this:
// src/foo.test.js
import myProxyquire from '../myProxyquire';
// This should be looking in the `src` folder
const { foo } = myProxyquire('./foo'); // error: ENOENT: no such file
describe('foo', () => {
it("should return 'bar'", () => {
expect(foo()).to.equal('bar');
});
});
Note that my last 2 files are nested inside a subfolder src. So when I try to run this test, I get an error saying that the module ./foo couldn't be found, as it is being looked for in the "root" directory, where the myProxyquire.js file is, not the src directory as expected.
You might be able to work around that (expected) behaviour by using a module like caller-path to determine from which file myProxyquire was called, and resolving the passed path relative to that file:
'use strict'; // this line is important and should not be removed
const callerPath = require('caller-path');
const { dirname, resolve } = require('path');
module.exports.default = path => require(resolve(dirname(callerPath()), path));
However, I have no idea of this works with import (and, presumably, transpilers).

Categories

Resources