I am working on a project which has a lot of client side javascript files
none of these files have any module.export.XXX statements
I understand that jasmine has a stand-alone version which uses a html file to run the tests and a node.js version which uses the cli to run the tests.
When using the stand-alone version of jasmine i can import the js files i want to test with tags, no problem.
When using the node.js version i cannot use import statements because none of the files that i wish to test have exports.
I am very keen on testing on the command line so that running the tests with our continuous integration package is easier.
I have attempted to solve this using this guide
https://www.nfriedly.com/techblog/2013/02/automatically-unit-testing-client-side-javascript-with-jasmine-and-node-js/
however it is very out of date (the jasmine folder structure is different) and i could not fix the JDOM issue which is mentioned.
Is there any way to make this work other than adding export statements?
if no
will adding export statements cause issues in any browsers?
and
most of the existing files are a wrapper functions with all of the functions and other components (variables, event listeners ect) inside it. I intend to call specific individual functions found in the files (not the wrapper functions) from the tests. will this be possible without using prototypes?
will i need to change the architecture
Related
I'm relatively new to modern Javascript dependency management. I'm writing a web app in Typescript and have started to use Jasmine as a unit test framework.
Once I started using Jasmine it became clear I needed to start exporting/importing the classes and functions in my source code files, since there is no HTML page loading all of them via script tags. This has been fine for my own files, but I am using a third-party library that is provided minified:
https://currency.js.org/
https://unpkg.com/currency.js#1.2.2/dist/currency.min.js
I can't seem to get my code running via Jasmine to recognize the existence of the currency function defined in this file. I'm guessing it's because the function seems to be dynamically created.
What would be a proper way to export the currency function above for use in my own modules?
That package actually is published on npm (with TypeScript typings included), and I was able to successfully import it as a module in a test application:
npm install currency.js
Then in your code:
import currency from "currency.js"
console.log(currency(1.23).add(.01).format());
https://www.npmjs.com/package/currency.js
It would be nice if one can run node.js code inside Excel user-defined functions. Something like using js code like VBA.
I googled for solutions but cannot find any. Is it possible to do this?
Yes, if you want to use packages from NPM. You could use webpack to combine all the stuff to one js file, it should work.
webpack as a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles. you could refers to this document.
You could also refer to a sample, Yeoman, the Yeoman generator creates a Node.js Office Add-in project. it use webpack combine all files into one js file.
I am looking to write a React-Native application. I want to be able to download new modules at run-time on the device to extend functionality. There would be some core logic that knows how to request new modules based on some form input like a dbs. I do not want to bundle everything into a single monolithic bundle which is what I believe happens now with the built in packager.
This would something similar to how RequireJS works in browser. What I need to know is:
How do I build independent modules? react-native bundle doesn't seem to allow me to select which root modules to begin with and only works on root project
How can I at run-time request new functionality be injected into the current JavaScript environment?
React native starts by pointing at a JS bundle. This means that you would at least have to restart the app to reload the js bundle (assuming that you're reading it from a server and not from the ios device itself).
If you did have a way to update the js files on the server (through some sort of web service that updated based on things the user does) then restarting the app could theoretically reload the JS and provide new functionality to the app.
I was able to get something pretty close to this in JS only. First I had to expose a few more options in the current react-native bundler, mainly, the url (to change the "main" module) and blacklist (to keep from bundling react-native in second bundle) options to their packager (http://github.com/facebook/react-native/blob/master/packager/README.md).
I then had to write a custom fetcher that would download the second bundle and use eval() to evaluate it in the current environment.
The one rub is that I had to add __d('react-native',[],require('react-native')) in the first bundle which I think work like define() from require.js. This exports 'react-native' as an unmangled name that the modules which I plug in can access via normal require() statements. It's a bit confusing at first but from what I can tell they React-native packager works a little like r.js (see http://requirejs.org/docs/optimization.html).
I have written a self contained angular js module using browserify in order to make use of the commonJS/Node style syntax.
The module works fantastic when tested by itself, so I then use gulp to minify and host that on GitHub.
I've then imported that into another app that is also using browserify. When I run browserify it seems to try and rebrowserify the module and causes no end of problems.
I believe this is because the module requires angular and jquery and qtip2. So it's obviously trying to re parse these.
Is there a standard to not parse modules, or is there a way to exclude the browserifying of the modules? Or is it best to not include things like angular and jquery within your modules? I was trying to make them perfectly stand alone, maybe that's unwise?
Many thanks!
I would suggest providing both options, if it is important for you to have a standalone version that includes angular. This will provide people using your code with a total of three ways of using your code: Using the standalone version, the version that only includes the module, and cloning the repository directly and including the source files as part their build process.
I generally use the third option, but people who don't have build processes will likely prefer the first or second.
In Node.js, you can dynamically "require()" any javascript file likewise to PHP's require. I'd like to use this in my client-side code just for ease of development but not actually call a javascript function, but have a compiler replace the line with the contents of the respective file; effectively concatenating the files, not one after another, but inline within the code of one of the files. The closest thing I have found to this is smash. Are there any compilers, minifiers, etc that can do this?
Browserify might not be exactly what you want but it does definitely help with the ease of development issue. When you use Browserify, your code is your build tool. Browserify gives you all the benefits of writing code in node (no anon functions to avoid globals, npm, simple requires, imports instead of namespaced globals) and it allows you to package that code to run on the client with one command and only load one file.
You can checkout my open source js framework Luc JS for an example. It runs on node and IE6. I'm able keep the code modular and build the single browser file with a one line command.