I need to use a k-nn algorithm in my application. I am using node.js to run my application and I would like to use this implementation of k-nn: https://github.com/aschuch/node-nearest-neighbor .
The problem is, how can I call the function to execute the k-nn in my angular.js controller function instead of in my app.js?
I already tried to do knn = require('nearest-neighbor') but didn't worked. I tried knn = document.createElement('nearest-neighbor') but didn't worked too. Sometimes I got error in a exports inside the nearest-neighbor.js and sometimes it says that findMostSimilar is not a function.
How can I execute this algorithm?
Related
This question already has answers here:
How to mock functions in the same module using Jest?
(10 answers)
Closed 1 year ago.
I'm a novice JavaScript developer, and I'm having trouble with unit tests.
I am using Jest. I run the tests with Node.js 13.14.0 (I use the older version because I have Windows 7).
I had the following problem: I needed to mock a function from the same module where the function I'm testing is located. Simply put, the function calls another function from the same module, and I want to make sure it gets called. After searching on Google, I didn't find a complete solution. I liked the option of using the rewire plugin, but that breaks code coverage.
I export functions from the module as follows:
module.exports = {
functions...
}
Then I import them into the test file using require.
I saw that people who encountered this problem were advised some methods using ES6 modules, but I haven't been able to figure out how to make them work and I don't have a pressing need to use them in my project. So still, is there any full-fledged solution? Maybe I should use a different testing framework? And I don't really understand why this problem exists at all. Is using one function per module a common practice?
Anyway, I'm looking for some simple way, like using rewire:
const otherFuncMock = module.__set__(otherFunc, jest.fn())
module.func() // calls otherFunc()
expect(otherFuncMock).toHaveBeenCalledTimes(1)
If otherFunc is exported by the module then simply using jest.fn() to mock it should work
const otherFuncMock = just.fn(x => module.otherFunc);
module.func();
expect(otherFuncMock).toHaveBeenCalledTimes(1);
If it isn't exported then I would suggest you don't need this test. The fact it calls another function is an internal implementation concern and you would be better focusing on testing the outcome of the combined function execution.
I am working with Typescript 2.1.5 and trying to write some newer sections of code with it.
I have a JS library that i am not ready to update in Typescript. The problem is that i need to consume some of that Javascript code in a simple Typescript component.
I have been fetching the web for a way to get my Javascript functions to get called and work in a .TS file. Unfortunatly informations are either outdated(mostly), not clear or not explained at all. I allready tryed a whole lot of tricks but none works.
So my question is : What is the easiest way and simplest way to consume javascript code in a TS file (ES6) ? There must be a protocol to follow to get this done.
Any tutorial, link or explanations will be much appreciated and i'am sure will help others in the same situation.
For better understanding here are some code snippets :
I start by doing an import of my JS file in the component.ts
var ZP = require('./zp.js');
In my js file i have two types of functions. Respectively a basic one and another which are methods from an object:
**export** function logTest(){
console.log("Responding");
}//Simple function
(*i just found while editing, that by appending the keyword "export" to the simple function makes it avaible to call in TS file. Wich doesn't work with methods *).
DObject.stringtest = function(){
return "Responding";
} //Object Method
Back to my component.ts file i call each one. The simple function with the export keyword now gets called, but the DObject method remains undefined.
(I made dummy calls for the DObject just to show what doesn't work even if it can seem obvious to some).
testFunc(event){
console.log("event is okay");
console.log(ZP.logTest()); <--- Now Works
console.log(ZP.stringTest()); <--- Don't Work
console.log(ZP.DObject.stringTest()); <--- Don't Work
console.log(ZP.DObject.stringTest); <--- Don't Work
}
Here are the console logs :
DObject.stringTest is not a function
( For the first two calls to stringTest() )
Undefined
( For the last call to DObject.stringTest )
( I just found something about Declaration files, i ll give it a shot, hopefully if it works i ll edit a full explained step by step tutorial. In the mean time any help would be much appreciated. )
In your case your existing javascript files are not detected in typescript because typings are not available.
you can create a type definition (index.d.ts) file and keep adding your typings so that it will be available to your tsc compiler.
Below are few thing you need to understand when coming to the typescript world. Some of you might know, some may not.
every typescript file transpiled to java script into specific version you want in tsconfig.json
typescript understand typescript ( for javascript type definition is required) or type definition.
there are many typing available in 2 forms :
typings install
#types namespace package
import and export makes available your members and properties to others
I have a nodejs server (sockjs) not fully developed by me. At a certain point a function is called, but I could go back to where the function is defined (if it belongs to a certain module, official or custom)
There's a programmatical way to know where a certain function is defined?
You should use a debugger. This would allow you to put breakpoints, so you can locate where that specific function is called and go into.
There are plenty of IDEs, I am familiar with Webstorm (the debugger works well).
I am working on developing a module for Titanium project. Now I need to call the migration.up and migration.down call backs from my own function. Is it possible to do ? I tried migration.down(AnyObject) and migration.up(AnyObject), but both didn't work.
Also I tried to log the object migration, it is NULL. So any other options to get the call back implementations ?
I did it by adding the following code to the migration javascript file.
Alloy.Globals.migration = migration;
And was able to call Alloy.Globals.migration.up() from alloy.js.
Let me know, if any alternatives available.
I'm writing a test using a LiveServerTestCase, django-casper, and casperjs for a view that includes javascript. Half way through a client side script I have a jQuery.post(url, callback_function(r){}) line.
When callback_function is called during a test r is null. However, when I run the application normally and step through the same javascript when callback_function is called r has the expected value.
This makes me think that there is a detail about LiveServerTestCase I'm missing to get jQuery.post to work with it. Can anybody please shed light on what I should do next to debug this problem?
I'm guessing it's because the static files aren't around. In Django 1.7, LiveServerTestCase no longer supported serving up the static files. It was moved into testing.StaticLiveServerTestCase
Try changing your test classes to subclass StaticLiveServerTestCase.