How to pass in arguments to mutant-rspec? - ruby

I'm trying to use the mutant-rspec library on a project that has the following directory structure
app
backend
spec
someclass_spec.rb
src
someclass.rb
frontend
I want to see how good my test coverage is on someclass_spec.rb, but can't seem to figure out what to pass in. I am fairly certain (based on reading, that i need the --include src, but I can't figure out for the life of me what goes into the --require argument.
From the backend folder, I've tried ...
bundle exec mutant --use rspec
bundle exec mutant --include src --use rspec
bundle exec mutant --include src --require someclass --use rspec
bundle exec mutant --include src --require src/someclass --use rspec
bundle exec mutant --include src --require src/someclass.rb --use rspec
bundle exec mutant --include src --require someclass.rb --use rspec
bundle exec mutant --include src --require Someclass --use rspec
But I never manage to get any active subjects
Active subjects: 0
Mutant configuration:
Matcher: #<Mutant::Matcher::Config empty>
Integration: Mutant::Integration::Rspec
Jobs: 4
Includes: []
Requires: []
Subjects: 0
Mutations: 0
Results: 0
Kills: 0
Alive: 0
Runtime: 0.06s
Killtime: 0.00s
Overhead: Inf%
Mutations/s: 0.00
Coverage: 100.00%
What can I pass into the flags to get it to run on my someclass.rb file?
The docs unfortunately don't seem super clear on this front.

From what I see above in Matcher: #<Mutant::Matcher::Config empty> you might be missing mutation subject as a last argument. Provided your subject is Someclass, that would be:
bundle exec mutant --include src --require someclass --use rspec Someclass

Related

Root Hooks in Mocha

I am just experimenting with Mocha and Mochapack and wanting to set up a Root Hook to fire before every test. According to the Mocha docs, I should place my hooks in a module and "require" them with the --require flag.
My NPM command is:
"test": "mochapack --colors --growl --webpack-config=node_modules/laravel-mix/setup/webpack.config.js --require jsdom-global/register --require assets/js/tests/roothooks.js --require assets/js/tests/setup.js assets/js/tests/Unit/**/*.spec.js"
My module where the root hook (roothooks.js) is defined is:
exports.mochaHooks = {
beforeEach(done) {
console.log("before!");
done();
}
};
What am I misssing?
(I added the tag mocha-webpack for this question - just to be clear I am using mochapack)

error loading css when running mocha tests with babel-node and babel-istanbul

I am having a problem testing UI components that import .scss with webpack. I am testing the component code directly, not the exported webpack bundle.
In my SUT
I have some code that imports scss:
import '!style!css!sass!postcss-loader!../style.scss'
This code causes an error when I run tests:
Error: Cannot find module '!style!css!sass!postcss-loader!../../stylesheets/parts/Breadcrumbs.scss'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
Cheap workaround
I've been working around this issue with:
try {
require('!style!css!sass!postcss-loader!../style.scss');
} catch(err) { console.log('Not webpack'); }
But this smells dirty, and I would rather not have this code in my SUT.
Running Tests
I can't figure out how to work in the solutions that I have found for this when using babel-node and babel-istanbul instead of mocha directly. Here is how I am currently running tests:
$ babel-node babel-istanbul cover _mocha -- --require ./test/setup.js --recursive
All of the answers I have found are for mocha directly, but I am not running tests with:
$ mocha --compilers js:babel-core/register --require ./test/setup.js --recursive
?
How can I work in a compiler or setup file to tell mocha to ignore .scss files. I am going to have this problem next with .svg files too I am sure.
What about github.com/css-modules/css-modules-require-hook or if you wanna just ignore the css npmjs.com/package/ignore-styles
EDIT:
If you install ignore-style module and then run:
babel-node babel-istanbul cover _mocha -- --require ./test/setup.js --require node_modules/ignore-styles --recursive
im sure it will work, bare in mind you might need to change the path node_modules/ignore-styles im assuming you have your node_modules in the root of your project.
So I had a similar problem trying to require with a webpack-loader prefix and failing as not in the context of webpack.
prunk was better than rewire etc as covered me for all files as was able to do path matching and replacement.
var prunk = require('prunk');
prunk.alias(/^(your loader prefix)/, '');
Then I modified requires extension handling to replace what was being imported.
require.extensions['.scss'] = function (module, filename) {
module.exports = 'whatever you want';
};
(exactly what style-loader does but style-loader cleans itself up! Also note style loader is misnamed and can handle many extensions))
I added this at the top of my test runner and no unfound modules!
Note I actually went further and used the original loader by itself by reading in the file with fs and passing it to the loader but that may have been over kill and should be using webpack to transpile tests with that sole loader in the first place!

How to get Babel to compile using webpack-style 'modulesDirectories'?

Following this gist here
https://gist.github.com/ryanflorence/daafb1e3cb8ad740b346
I was able to set up a cool folder structure for a pretty complex app using Webpack's 'resolve.modulesDirectories' - I can reference shared components via module-type imports :
import MyModule from 'modules/MyModule';
where the file is in
src/app/modules/Foo/modules/Bar/modules/Baz/component.jsx
and MyModule is in
src/app/shared/modules/MyModule
The trouble is, when I try to run tests
mocha --compilers js:babel-core/register $(find src/app -name *.test.jsx)
the Babel compiler throws saying it can't find anything in 'modules/MyModule'...
Is there an analog to Webpack's 'modulesDirectories' in the Babel compiler config ?
You should be able to use Babel's resolveModuleSource option to essentially rewrite the import paths, but you'd need to use the programmatic API to do it. So instead of using Mocha's --compilers option, use --require to load a script that calls Babel's require hook and passes the resolveModuleSource option. If you're using Babel v6 use the babel-register package and it'd look something like:
require("babel-register")({
resolveModuleSource: function (source, filename) {
return filename.replace(/^modules\/.+/, "/abs/path/to/src/app/shared/$0");
},
});
You'll have to compile your tests using webpack:
https://webpack.github.io/docs/testing.html

Cannot test my model tests

I have tests in the folder:
test/models/person/helper/age_calculator_test.rb
These test should fail because of:
assert_equal false, true
they do fail when I run:
bundle exec m test/models/person/helper/age_calculator_test.rb
But when Im running:
bundle exec m test/models
These Tests are not run! What do I wrong?
How can I test all of my Model-Test with Bundler in one command? Thanks!!
It appears that you use the Metal test runner. I cannot see anywhere on its documentation supporting path testing such as test/models. Its main feature is to run tests by line number.
If you would like to execute all of your model tests you could use Rails'
rake test:models
to do it. See the RoR Guide on Rake Tasks for Running your Tests for more options.

How does one configure Cucumber / Aruba to use SimpleCov?

Ruby-2.0.0p247
ActiveRecord-4.0.1
Cucumber 1.3.10
Aruba-0.5.3
SimpleCove-0.8.2
We use Cucumber with Aruba in a NON-RAILS project that nonetheless uses ActiveRecord. Our cucumber features exercise the code both in-process and out-of-process. The out-of-process code is executed using the same loader sequences as in production through a startup stub in bin:
#!/usr/bin/env ruby
require 'bundler/setup'
Bundler.require
require 'pathname'
my_dir = Pathname.new(
File.join( File.dirname(
__FILE__ ), '../', 'lib/' ) ).realpath.to_s + '/'
require my_dir + File.basename( __FILE__ )
HllThForexRssFetch::Main.new( ARGV ).execute
#EOF
Our features/support/env.rb file contains this:
$ cat features/support/env.rb
# Must load and start simplecov before any application code
require 'simplecov'
SimpleCov.start do
add_filter "/features/"
add_filter "/libexec"
add_filter "/lib/hll_active_record/"
add_filter "/test/"
add_filter "/tmp/"
end
SimpleCov.command_name( "Cucumber Features" )
# Do not use cucumber/rails in standalone projects
#require 'cucumber/rails'
. . .
When our step definitions call the external bin/file through aruba's run command the step definitions work properly and the tests complete as expected but the code coverage is not merged with the rest of the run. What I am seeking are instructions for how to set up simplecov to report the code coverage of the out-of-process tests together with the portions that are run in-process by cucumber directly.
How does one do this?
I have an environment similar to yours, this is how I got it working:
Assuming a directory tree like:
project
|- bin
| |- binary
|- lib
| |- ...
|- spec
| |- ...
|- features
| |- support
| | |- env.rb
| |- ...
Fist check this issue https://github.com/colszowka/simplecov/issues/234
It describes that the binary should start simplecov. It's hackish but I added this header to my binary (project/bin/binary):
if ENV['COVERAGE']
require 'simplecov'
# As described in the issue, every process must have an unique name:
SimpleCov.command_name "binary #{Process.pid}"
# When running with aruba simplecov was using /tmp/aruba as the root folder.
# This is to force using the project folder
SimpleCov.root(File.join(File.expand_path(File.dirname(__FILE__)), '..'))
SimpleCov.start do
filters.clear
# Because simplecov filters everything outside of the SimpleCov.root
# This should be added, cf.
# https://github.com/colszowka/simplecov#default-root-filter-and-coverage-for-things-outside-of-it
add_filter do |src|
!(src.filename =~ /^#{SimpleCov.root}/) unless src.filename =~ /project/
end
# Ignoring test folders and tmp for Aruba
add_filter '/spec/'
add_filter '/test/'
add_filter '/features/'
add_filter '/tmp/'
end
end
Then in the calling of binary inside cucumber the COVERAGE environment variable should be set.
In the feature/support/env.rb at the before clause:
require 'simplecov'
SimpleCov.command_name 'Cucumber'
Before do
# This is using the aruba helper,
# cf. https://github.com/cucumber/aruba/blob/master/lib/aruba/api.rb
set_env('COVERAGE', 'true')
# This could also be accomplished with the "I set the environment variables to:" step
end
If in your environment you have two Frameworks (like RSpec and Cucumber in this example) don't forget to https://github.com/colszowka/simplecov#merging-results
Using the above, setting the command_name based on the PID of the aruba
process causes SimpleCov to accumulate a very large result set and pollutes
the results with old runs.
I had better luck setting the command name as follows:
# As described in the issue, every process must have an unique name:
SimpleCov.command_name ARGV.join(' ')
Which causes only the latest run with the same arguments to get included in
the results.

Resources