ignore file extension with Jest - unit-testing

I'm using webpack resolve.extensions to "selectively" bundle my js files.
eg.
App.js
import MyComp from 'comp/myComp';
in comp/ folder I have:
MyComp.web.js
MyComp.ios.js
MyComp.android.js
Now I want to write test for App.js, to test how it render as web. But the problem is Jest keep resolving all 3 .js files and that causes dependency error because I'm not running the command in a mobile environment and most mobile modules will not work.
how can I tell jest to only resolve .js and .web.js files just like webpack?
I added this to my package.json and it keep resolving .ios.js and .android.js
"moduleFileExtensions": ["web.js", "js", "jsx"],
"moduleDirectories": [
"node_modules",
"main"]
I tried as suggested with:
"testPathIgnorePatterns": ["<rootDir>/node_modules/", "^.+\\.(android|ios)\\.js$"],
looks no effects :(

You can also remap files to an empty file
touch empty.js
In your jest config add following
moduleNameMapper: {
'\\.(css|jpg|png|scss|less|sass)$': '<rootDir>/empty.js',
},
Works perfect for me

You can add testPathIgnorePatterns to your package.json file. The value is an array of regex patterns. Any file/path that matches an expression will be ignored/skipped from testing.
See these Jest docs for more detail

Related

Find all files by extension excluding specific directories

I have both node tests and Angular(4x) tests in the same repository and I am using jest as the test framework. Since both type of tests use different transforms, my idea is to use different jest configurations with different testRegex. Below is my folder structure.
--e2e
|-----app.e2e-spec.ts
--src
--api
|-----ping.spec.ts
|-----card.spec.ts
--util
|-----util.spec.ts
--web
--app
|-----app.spec.ts
--test.spec.ts
In my jest configuration for angular tests, I am using the following regex.
"testRegex": "(/src/web/.*\\.spec)\\.ts?$",
The idea is to execute all spec.ts files under src/web/ and its subdirectories.
In my jest configuration for node tests, I am using the following regex.
"testRegex": "(^((?!.*src/web).).*spec\\.ts.*$)",
For my node tests, I want to execute all spec.ts files excluding the ones in src/web and e2e/. My testRegex excludes all files under src/web. How can I modify that to add e2e also to that regex. Since I'm new to jest, any other suggestions on how to implement this are welcome too.
Wrap src/web in parentheses and add |e2e/:
"testRegex": "(^((?!.*(src/web|e2e/)).).*spec\\.ts.*$)",
Example

How to turn on .sass extensions in Ionic 2?

I have set up a basic project and only .scss files are picked up. I would like to write my CSS with the .sass format. How can I turn that on?
You can override the app script config files:
https://github.com/driftyco/ionic-app-scripts#overriding-config-files
In my package.json, I added the following:
"config": {
"ionic_sass": "./config/sass.config.js"
}
I created the sass.config.js (copy from the github project) and added the sass extension:
includeFiles: [
/\.(scss|sass)$/i
],
It's unfortunately impossible to update the watch config file, so I directly edited it in the node module:
Under #ionic/app-scripts/config/watch.config.js
Add the following .sass line below .scss:
'{{SRC}}/**/*.scss',
'{{SRC}}/**/*.sass'

Karma error: require is not defined

I use karma with qunit for test an emberjs application. The karma.conf.js file have this piece of code for link my project libraries
files: [
"app/bower_components/jquery/jquery.js",
"app/bower_components/mockjax/jquery.mockjax.js",
"app/bower_components/handlebars/handlebars.js",
"app/bower_components/ember/ember.js",
"app/bower_components/ember-data/ember-data.js",
"app/bower_components/bootstrap-sass-official/vendor/assets/javascripts/bootstrap.js",
"app/scripts/app.js",
"tests/test.js"
],
and my app.js is this:
var MyApp = window.MyApp = Ember.Application.create();
require('scripts/controllers/*');
require('scripts/store');
require('scripts/models/*');
require('scripts/routes/*');
require('scripts/components/*');
require('scripts/views/*');
require('scripts/router');
but when i start karma with the config file, it report this error
Firefox 30.0.0 (Ubuntu) ERROR
ReferenceError: require is not defined at ~/myApp/app/scripts/app.js:4
I've tried to change the order of the libraries in the karma.conf file but doesn't work.
Try installing requirejs and including it in the files section of karma.conf.js.
npm install requirejs
Running this command will create a node_modules folder, if one doesn't already exist. RequireJS will be probably be in:
node_modules/requirejs/require.js
So, you can try editing your files section like this:
files: [
"node_modules/requirejs/require.js",
...
]

grunt-karma not running the spec file when using shared config

I created a basic project to try and get Gruntjs, Karma and Jasmine to play together. When I setup the karma.conf.js file with all of the neccesary files, everything works and the tests pass.
When I try to split them up in Grunt though, I get problems.
Gruntfile.js
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
karma: {
options: {
configFile: 'karma.conf.js'
},
basicController: {
files: ['/basicController/scBasicControllerCtrl.js', '/basicController/test/ControllersSpec.js']
},
overworkedController: {
src: ['overworkedController/scOverworkedControllerCtrl.js', 'overworkedController/test/ControllersSpec.js']
}
}
});
The documentation at grunt-karma show to use "files:" when splitting up the modules. I did that under the basicController module and when I try to run $ grunt karma:basicController --verbose, I get an error saying
Warning: Cannot use 'in' operator to search for 'src' in /basicController/scBasicControllerCtrl.js Use --force to continue
Aborted due to warnings.
When I run $ grunt karma:overworkedControllers --verbose (using "src" instead of "files", it looks like everything is going to work and the Chrome browser launches but then is says it executed 0 of 0 ERROR.
There should be 3 tests.
Let me know if there's any more info I could post.
My understanding of grunt-karma was incorrect.
I thought I could have the base and source files in the karma.conf.js file. Then in each module, I'd just add the specific files needed for that module and test.
The way it actually works is that the files declared in each module completely overwrite the files property in the karma.conf.js file. Not append to them.
I ended up creating an array in Gruntfile.js that contains all of the source .js files and just concat the necessary files to it in each module.

ember.js unit testing and integration testing

I am exploring ember.js application testing. I came across following blog by Toran Billups
Integration testing your ember.js app with QUnit and Karma
When we do karma start it runs all the test found in test directory.
How can I run say tests in only one .js file?
thanks
As #bk11425 suggested, simply modify the 'files' section in your karma.conf.js file from this
module.exports = function(karma) {
karma.set({
basePath: 'js',
files: [
"vendor/jquery/jquery.min.js",
"vendor/handlebars/handlebars.js",
"vendor/ember/ember.js",
"vendor/jquery-mockjax/jquery.mockjax.js",
"app.js",
"tests/*.js",
"templates/*.handlebars"
]
To something like this instead
module.exports = function(karma) {
karma.set({
basePath: 'js',
files: [
"vendor/jquery/jquery.min.js",
"vendor/handlebars/handlebars.js",
"vendor/ember/ember.js",
"vendor/jquery-mockjax/jquery.mockjax.js",
"app.js",
"tests/mytestfile.js",
"templates/*.handlebars"
]
Note- if you are doing integration testing please include the integration helper just above this single test file I showed above
To run the test from just one JS file, specify the file name in the "files" array of your karma config file.
Here's a sample config file I used recently. My example runs 2 js files for the test portion (the last 2 in the list), but you could easily just do one.
Bryan