I'm using CoffeeScript in a Rails application, and I would like to unit test it. Google didn't turn up anything, is there any way to do it short of writing my own testing framework or testing the JavaScript that CoffeeScript outputs?
You can use any javascript testing framework with CoffeeScript. This will be testing the Javascript that CoffeeScript outputs which is necessary since CoffeeScript itself can't be executed.
Writing your own testing framework for CoffeeScript is fun (I did) but entirely uneccessary.
UPDATE: Jasmine tests can be run on node.js in which case both the tests and the code under test can be CoffeeScript, without the need for any compilation step.
You can use QUnit "as-is", but still only write coffee-script - and no glue-code.
I have a very small, pure coffee-script project on github as an example - rubyann.
The HTML test page rubyann_tests.html, references the rubyann_tests.coffee file which tests jquery.rubyann.coffee. I didn't write any javascript or any other code to make this work.
The tests only run on Chrome on your local machine if you use the command-line argument --allow-file-access-from-files. But it works on Firefox and even IE without issues.
addendum - the tests are also setup to run on the command line via Node/gulp/qunitjs - download the repo and type npm run test
I'm testing CoffeeScript in my Rails app with QUnit, and have written up how I'm doing it here: http://effectif.com/coffeescript/qunit-boilerplate
The most interesting thing in my write-up is the use of the callback to Coffee.load to guarantee that files containing tests get loaded after the files that contain the code under test:
<script type="text/coffeescript">
for file in ['models', 'controllers']
lib = "../../app/assets/javascripts/#{file}.js.coffee"
load_test = ->
test = "#{file}_test.coffee"
-> CoffeeScript.load(test)
CoffeeScript.load lib, load_test()
</script>
The need for currying the test variable is explained in the article...
For Coffee-Script Unit testing you can try Beast-Test it was written from the ground up for coffee-script. FYI i am the own but i think you will like it none the less. It is similar to JUnit
Related
Context
I have a project with following traits
IntelliJ Ultimate 2020.1
Java 13 (with module-info.java)
Kotlin 1.3.72
JUnit (+ truth)
maven (I believe this to be unimportant)
The code base is mixed, some classes are written using plain Java, others with Kotlin, the same is true for tests. Everything works as expected, that is
all code is compiled in proper order and fully interoperable between Kotlin and Java
all test can be executed using either mvn test or IntelliJ "Run Test"
the resulting jar can be run (for the sake of providing context)
but...
apart from the fact that everything works, IntelliJ warns me about a non declared module dependency only if the test class is written in Kotlin. This warning is not displayed for test classes written in plain Java.
The warning:
Error:(9, 6) Symbol is declared in module 'org.junit.jupiter.api' which current module does not depend on
That warning normally allows one to import / require the respective module / dependency, but there is no solution offered in [alt]+[enter] dialog.
Things I have tried so far:
upgrading from JUnit 4 to 5 didn't change the situation
googling to no avail :(
making sure tests written in Kotlin are really executed when mvn test is run by making a test fail
manually running test using IntelliJ "Run Test"
converting tests back and forth from / to Kotlin
explicitly requiring the the JUnit API / truth in module-info
The latter obviously prevents the warning but is no solution since that actually produces a hard dependency. From what I found out while googling, the maven-surefire-plugin makes sure the test-dependencies are included. Also: running mvn test works like charm as stated above, so this does not seem to be part of the problem.
Seeing all the red lines when writing test is really annyoing...
the suspect behavior
same test but in java - everything is fine
Question:
How can I fix that warning for Kotlin Test Classes in IntelliJ?
Note
I have come to believe this is a bug in IntelliJ but I'd be happy to be shown what I overlooked.
Since everything from compiling to running with Maven works like a charm, I excluded details regarding project structure and so on. The issue at hand is about the warning in the IntelliJ, not about a broken build or non-functional jars. I'll glady add those in case they turn out to be necessary.
also since everthing actually works (apart from the annoying warning), I really don't know where to continue researching and hence created a question.
This is a bug in IDEA Kotlin plugin error highlighting: https://youtrack.jetbrains.com/issue/KT-26037
Workaround: add #file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE") to the test file before the package declaration.
I have a project that is using webpack to bundle all code into a single file. The project is using Typescript and it is working fine at the moment.
I've gone to add unit testing and jasmine seems to be the way (one of the many ways) forward. Its actually jasmine-core that is included in the package.json - not sure how much of a difference that makes.
So running a very simple test such as
it('true is true', function(){ expect(true).toEqual(true); });
works fine.
But when I add tests that require the use of an import - eg
import MyService = require('./MyServices');
then when I run the tests it complains as it doesn't know what 'require' is.
Uncaught ReferenceError: require is not defined
Now I'm guessing this is because I need to package up the test module in a similar way that I package up the main project.
So what is the best way to do this?
Should I have multiple entry points in the webpack.config.js file - one for each *.spec.ts file?
Or is there a way to have say accept an unknown number of spec files
entry:[ *.spec.ts ] and have it output a js file for each one - *.spec.js
You can use karma/karma-webpack to run all the tests using webpack for resolving the imports. You can take a look at this repository for a simple configuration.
You can also specify an index.spec.ts as en entry point and make this file require all the spec files if you don't want to make one entry point for each spec.ts in your webpack's configuration file.
I'm working on creating an ember addon, and I'm a bit stuck trying to write tests for it. This addon implements a command line option, rather than shipping components etc. As a result, none of the moduleFor type test helpers are relevant for me in the out of the box qunit tests. I'm not rendering any components, I just want a test runner to exersize the implementation behind my command line option.
To write my tests, I'll need to just require my various source files that are up in my addon. For example, files sitting in root/lib. I can't get a require/import that can find these files in a qunit integration test under root/tests/integration. Is this possible? I need a relative path like:
import foo from '../../../lib/foo'
But nothing up there seems to work. The folder structure created for an addon is like:
root
app
lib (was planning on putting my addon impl here)
tests
dummy
helpers
integration
example-test.js (trying to reference code out of the lib folder from here)
It seems like my options in this case are just to fall back to some plain old JS unit testing (qunit, jasmine etc), based up in the root of the addon, not using any ember magic or the dummy app. I would like to stay on the 'out of the box' path provided by ember generate addon, but it seems like I need to go my own way here, so I can reference my source files.
Use
import foo from 'myApp/lib/foo'
Browserify handles dependencies nicely by requiring them and creating a bundle where all is in order. When unit testing a module that has it's own dependencies it's somewhat more complicated though.
My tests are using mocha, sinon, chai.
My app is built on backbone, marionette, and some jQuery plugins.
I'm putting it together with grunt and browserify, all is written in coffeescript.
For instance, I have a module looking like this:
# Global, because swosh won't know what jQuery is otherwise.
$ = global.jQuery = require 'jquery'
require '../jqueryPlugins/swosh.js'
module.exports =
App.MyView: Mn.ItemView.extend
# Here I make use of jQuery plugin swosh
When I'm testing this piece of code, I'll do it like this:
App = require '../src/swoshViews.coffee'
theView = new App.MyView
describe 'Swooshing'
it 'swooshes', ->
# I create a spy/stub of the jQuery plugin
# and make sure it's called as expected.
Since Browserify isn't a dependency injection framework, the jQuery plugin will not be available for the module, there are some options to make that happen. The most suitable for me seems to be Rewireify.
I'm running my tasks with Grunt, where browserify is run win the transformations ['coffeeify', 'rewireify']. However, and here's my question:
My unit tests, being run with npm test, doesn't use a browser.
package.json extract:
"scripts": {
"test": "mocha"
},
Grunt>Browserify will arrange a .js bundle for me with rewireify baked in, though how do I apply this for my npm test task? My npm test script simply runs mocha, as seen above, which will run tests in all my unit test files, no need for a bundle. How do I make use of rewireify this way?
I started learning how to test Angular apps, and ran into some problems.
I generated an Angular app using Yeoman. yo angular --minsafe AppName
Then generated a service yo angular:service MyService
Wrote a simple method in the service, and a test for it, just to make sure that everything was working. I ran grunt test and the tests passed.
Now it gets interesting, as I added Underscore to the mix using bower install underscore and added a <script> tag for it in the index.html.
Then I added some simple code to the service method, just _.map([1,2,3], function(el){return el+1}); to see if Underscore was working.
I ran the tests again grunt test, and it failed saying that _ is not defined.
I tought that, because Underscore attaches the _ variable to the window object, it would be available for the testing. Am I wrong?
Also, when I ran the application in the browser, Underscore was defined and working.
So, my question is, how do you test an Angular app that uses Underscore? Is this a common problem or am I doing something wrong?
Thanks,
Petar
If you see the karma.conf.js file generated by Yeoman, you will see that bower components are not added automatically.
// list of files / patterns to load in the browser
files = [
JASMINE,
JASMINE_ADAPTER,
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
];
Just add the underscore folder to it and you won't have any issues.
Using constants like JASMINE or JASMINE_ADAPTER is deprecated in Jasmine version before 2.0. Use frameworks: ['jasmine'] in the karma.conf.js file instead.