How to test bootstrap methods in node? - node.js

I have a react component that has the following line in componentDidMount():
$('[data-toggle="tooltip"]').tooltip();
When I run my test, I get:
TypeError: $(...).tooltip is not a function
I have global.$ defined, but I wonder if there is a way to stub out the tooltip() function when my tests are run. I don't care if the tooltip() is stubbed, I just need my test to run.
Any help would be appreciated!

ask global.$ to return object with a .tooltip function
global.$ = function () {
return {
tooltip: function() { console.log(42) }
}
}

Related

importing and calling a function in Typsescript which returns nothing

Hi I have a function in typescript which returns nothing. When I try to import and call this function in another part of my app, I am getting errors. I am fairly new to typescript and am struggling with how to fix this issue.
Here's my code. I am trying to set up a few scripts to do some simple tests (would prefer not to use any testing frameworks).
My helper functions for testing are here
//File: helper.ts
type validator = () => void;
export const it = (desc: string, fn: validator) => {
try {
let res = fn();
console.log("\x1b[32m%s\x1b[0m", `\u2714 ${desc}`);
} catch (error) {
console.log("\n");
console.log("\x1b[31m%s\x1b[0m", `\u2718 ${desc}`);
console.error(error);
}
};
My tests use the helpers and are defined like this;
// File: dummytests.ts
import { strict as assert } from 'node:assert';
import { it } from "src/spec/helper";
export const check_if15_eqls_15 = it("shoulld check if something is true", () => {
assert.strictEqual(15, 15);
});
and i finally am running the tests as such;
// File: testRUnner.ts
import {check_if15_eqls_15} from 'src/spec/frontend/dummyTests';
console.log ("This test harness is for the frontend tests.\n");
check_if15_eqls_15();
this throws the error;
error TS2349: This expression is not callable.
Type 'void' has no call signatures.
5 check_if15_eqls_15();
~~~~~~~~~~~~~~~~~~
The line
export const check_if15_eqls_15 = it("shoulld check if something is true", () => {
assert.strictEqual(15, 15);
});
already calls the method.
This means check_if15_eqls_15 is already the return value of the method and unless the return value is not another function (which it isn't in this case), you can't call it again.
The same thing would happen in pure JS, since TypeScript does not change how the code is run
Something that might work for your example would be this:
export const check_if15_eqls_15 = () => it("shoulld check if something is true", () => {
assert.strictEqual(15, 15);
});

Failing to mock declared functions with jest.spyOn

I'm under the impression that jest.spyOn.mockImplementationOnce can mock a function as long as it is implemented as a function expression, but might fail to mock when the function is written as a function declaration. Why is it behaving like that?
I have one TS file in the folder */this_works with the following code:
export function myFunction() {
return myOtherFunction();
}
export const myOtherFunction = function() {
return 'something';
};
And then another TS file in the folder */this_doesnt with the following code:
export function myFunction() {
return myOtherFunction();
}
export function myOtherFunction() {
return 'something';
}
The only difference between them is that myOtherFunction is either a function declaration or a function expression.
Both files are submitted to the same test with jest.
import * as myModule from '../index';
describe('myFunction', () => {
it('does something', () => {
jest
.spyOn(myModule, 'myOtherFunction')
.mockImplementationOnce(
function() {
return 'hello jest';
},
);
const result = myModule.myFunction();
expect(result).toEqual('hello jest');
});
});
All code can be found at my playground repo's folder
The result is that the function expression is mocked and the test passes, while the function declaration is not mocked and the test fails.
Anecdotally, this seems to only happen if the function being mocked is not being called directly, instead it is being called by another function. When the declared function gets called directly, the test passes.
I would like to understand why that is so I can write better tests.

node.js call function inside function class object

i have very good js background but i am new to node.js
i dont undersand why simple class object function is not calling
function functions () {
function test () {
console.log("function ok");
function test2 () {
console.log("function inside function is ok");
}
return {
test2 : test2
};
}
return {
test : test
};
}
var test_function = new functions();
functions.test.test2();
i get error
TypeError: Cannot read property 'test2' of undefined
thanks
Try calling test_function.test().test2(). You need to invoke test() before you can invoke test2(). Also, in your example you correctly invoked functions() and assigned it to test_function, but then you did not do anything with it.
Call it like this:
var test_function = new functions();
test_function.test().test2();

Jasmine - Spy on a function that is called in same file

This has bothered me for a while. I have two functions in the same file.
//fun.ts
export function fun1(){
let msg = fun2();
return msg;
}
export function fun2(): string{
return "Some message";
}
I have a typescript spec that stubs fun2 and calls fun1.
//fun.spec.ts
import * as Fun from 'fun';
describe('Stubing', () => {
it('should stub the return value', () => {
spyOn(Fun, 'fun2').and.returnValue("A different message");
expect(Fun.fun1()).toEqual("A different message")
});
});
But when I run the spec, the output I get is
Failures:
1) Stubing should stub the return value
1.1) Expected 'Some message' to equal 'A different message'.
I wrote the tests in typescript and then I have a gulp script that successfully transpiles and runs the jasmine specs. Everything works, the only thing that I can't figure out is why the spy is not working. An explanation would be appreciated.
I finally figured this out. In fun.ts, I am directly calling the fun2 object, but my Jasmine spec has no access to that object. The only object the Jasmine spec can access is the exports object. If I want the spy to work I need to call fun2 on the exports object.
//fun.ts
export function fun1(){
let msg = exports.fun2();
console.log(msg);
}
export function fun2(): string{
return "Some message";
}
Now when the spec executes I see
.
1 spec, 0 failures

Sinon function stubbing: How to call "own" function inside module

I am writing some unit tests for node.js code and I use Sinon to stub function calls via
var myFunction = sinon.stub(nodeModule, 'myFunction');
myFunction.returns('mock answer');
The nodeModule would look like this
module.exports = {
myFunction: myFunction,
anotherF: anotherF
}
function myFunction() {
}
function anotherF() {
myFunction();
}
Mocking works obviously for use cases like nodeModule.myFunction(), but I am wondering how can I mock the myFunction() call inside anotherF() when called with nodeModule.anotherF()?
You can refactor your module a little. Like this.
var service = {
myFunction: myFunction,
anotherFunction: anotherFunction
}
module.exports = service;
function myFunction(){};
function anotherFunction() {
service.myFunction(); //calls whatever there is right now
}

Resources