I use browserify to bundle all our angular js code into one file. We use karma + jasmine to unit test this one file, app.js. As part of the bundling that browserify does, it injects a single line of code at the beginning of the file:
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
I tried putting a /*instanbul ignore next*/ above that line but causes the whole file to be ignored. this one line is killing my branch coverage numbers. is there anyway to ignore this generated code?
It is always preferred to write the unit test for each file,before the bundle process. If you have a number of files, will be difficult to mock the dependencies and keep track of them. And we have only a number of options available in istanbul. And what you want to do is to skip the function definition header. There is no possible way to ignore that particular line only. But you can solve this issue by separating the unit test's into different files. That is preferred and easy to test.
Related
We are running unit test cases in jest through the nx command :- nx affected --target=test and We have certain test files like :-
1)abc.integration.spec.ts
2)abc.spec.ts
When I am running command :- nx affected --target=test , It is running both test cases.
Is there any way to ignore/avoid/skip "abc.integration.spec.ts" from unit test execution.?
FYI :- I have already used testPathIgnorePatterns flag through the command :-
nx affected --target=test --testPathIgnorePatterns='integration.spec.ts', but it is still running both files. Looks like testPathIgnorePatterns is not supported in nx jest cli
you can do it by modifying the jest.config.ts file and adding "testMatch", "testPathIgnorePatterns" or "testRegex". Consider that the order of precedence impacts whether the files are excluded or not.
For example, if you add:
testMatch: [
"!**/*.integration.spec.ts",
"**/*.spec.ts"
]
The second pattern cancels the first one. You have to write the negations at the end to make it work.
This will skip any test file that ends with .integration.test.ts, and if you want to run them separately, you'll have to add another command in the project.json and create a separated jest.config.integration.ts, including the integration tests and excluding any other .test file, and reference it there. That way, you could run your integration tests with nx affected --target=integration (if the new command is called "integration" of course)
I have a script that looks like this:
angular.module('Game').constant('WHITE_PIECE', '\u26C0')
... more lines ...
.constant('WHITE_KING', '\u26C1')
(end of file)
When I run grunt jasmine, I get the classic error, Module 'Game' is not available! You either misspelled.... Oddly though, I'm positive that module is being declared. If I move the code to the end of the file where the 'Game' module is declared, it works. If I move the code to the end of a different file, which declares a service for the 'Game' module, it works. I get no errors in my browser. Is this phantomjs being dumb? Is it a bug in Grunt-contrib-jasmine?
I put console.log statements before this file and before the file which declares the Game module; this showed that the constants file is being interpreted before the Game file. However, I thought Angular was clever enough to figure that out. Do I need to tell grunt-jasmine-contrib exactly which order to load my scripts?
Based on more Googling, I believe this to be a bug and not a mistake. The only solution is to force the correct order of script interpretation, which unfortunately negates the convenience of the jasmine globstar source pattern (e.g. **/*.js).
Short:
How do I use header only libraries with biicode?
Medium:
When I try to build a block it includes example directories even though I try to set the dependencies explicitly in the biicode.conf of the published block.
Long:
I'm trying to get the unity framework up and running, using biicode.
Unity is great as a unit testing framework for C because you do not need to compile any libraries. If you do your own mocks, you don't even have to run any scripts - there is just a single .c file to include in your compile and you are golden.
I've published the git repo to my biicode block paulbendixen/Unity and since there is no need for any compilation step beyond the c file that accompanies the header that should be included there is nothing else to do.
However, when I include the file, using #include "paulbendixen/Unity/src/unity.h" I get the error when doing bii cpp:build:
Code.c:2:28: fatal error: ProductionCode.h: No such file or directory
#include "ProductionCode.h"
This is in the examples folder and should therefore not be compiled, when I just want to use the unit testing part. Changing the [dependencies] to include unity.h = unity.c unity_internals.h hasn't helped either.
I'm pretty sure the problem should be resolved in the Unity/biicode.conf, but I haven't been able to find a thorough description of this file anywhere.
The simplicity of the Unity library should make it ideal for a build system such as bii, but it seems quite complex to set up.
If it helps, I've used the simple layout and the -r [github for throwtheswitch] option
It is not that simple. Unity uses Rakefiles to build and run the tests, and they have lots of configuration. What can be done for quickly upload it to biicode is just to ignore the tests and publish just the files. This can be done writing a ignore.bii file with the contents:
docs/*
test/*
examples/*
*test*
Wrt to the biicode.conf, the only configuration necessary are the include paths:
[paths]
src
extras/fixture/src
You can check that the manual definition of dependencies is not necessary, if you run $ bii deps --files *unity.h
With these changes, it is possible to publish it. Nothing to build.
Then, to use it in other projects, I have been able to build simple tests:
#include "unity.h"
void testTrue(void){
TEST_ASSERT(1);
TEST_ASSERT_TRUE(1);
}
int main() {
testTrue();
}
Just adding the following to the biicode.conf of the new project:
[requirements]
diego/unityfork: 0
[includes]
unity.h: diego/unityfork/src
It would probably be much easier to make biicode run and build the tests without ignoring them if it used the more typical CMake configuration instead of Rakefiles
I was looking at the unminified version of the 1.2.8 angular library (https://code.angularjs.org/1.2.8/angular.js). There are a number of directives that i will never use in my application, such as:
scriptDirective
ngBindDirective
ngBindHtmlDirective
ngNonBindableDirective
ngPluralizeDirective
ngTranscludeDirective
I removed all of the above, and corresponding code/functions. After recompiling my application, it performs as expected.
However, when i attempted to minify/uglify the file, im left with a file ~ 140kb in size. Yet, the original minified file size was ~98kb.
Ive used minify/uglify from Gulp and various online compressors.
How can i remove elements from the library, minify it, and the result file be smaller than original?
Right now my Karma config has the following files to include:
files: [
'vendor/vendor.js',
'vendor/angular-mocks/angular-mocks.js',
'src/components/security/index.js',
'src/components/security/authentication.js',
'src/components/security/login-controller.js',
'src/components/filters/index.js',
'src/components/filters/toDate.js',
'src/components/services/index.js',
'src/components/services/alert.js',
'src/menu/index.js',
'src/menu/menu-controller.js',
'src/user/index.js',
'src/manage/index.js',
'src/manage/user/manage-user-controller.js',
'src/manage/channel/manage-channel-controller.js',
'src/stream/index.js',
'src/stream/stream-controller.js',
'src/messages/index.js',
'src/messages/messages-controller.js',
'src/app.js',
'src/**/*.spec.js'
],
The file vendor/vendor.js is automatically created by a Gulp task concatenating all vendor files using my bower config. It's all my own Javascript code that's so hard to include because order matters a great deal. The index.js files within a folder define the module (and its routes) and thus must be loaded before the individual files. And app.js always has to be last.
So my question is how I do this a bit smarter, for example with a glob that first includes all the index.js files and then all the others?
This is exactly what RequireJS is for. You can use it in your deployed code, but you can also just use it for your tests.
If you do this, your "karma.conf.js" ends up being a little shorter and less volatile. Your RequireJS config file specifies some dependency mapping. Each test spec ends up specifying what dependencies it needs, either in a "declarative" fashion in the "define" call, or sometimes manually through the "require" function (you sometimes need the latter to deal with circular reference problems).
Hm I guess asking the question gave me the idea I needed :) This works:
files: [
'vendor/vendor.js',
'vendor/angular-mocks/angular-mocks.js',
'src/*/**/index.js',
'src/*/**/*.js',
'src/app.js',
'src/**/*.spec.js'
],
Adding the *.js after index.js results in debug messages like this:
src/manage/index.js ignored. Already in the list.
Excellent!