Unit Testing - test for file count and folder size - javascript

I have homework JS code to check every week.
I have tests for JS logic, but i also has a requirement for overall size and file quantity.
So i was wondering is there any possibility to write tests (i assume mocha - because it runs on node, and has access to FS) for folder size and file count in folder (also has a limit)?
I have no experience in Mocha or Node, so should i dive deeper?

I'm not quiet sure what you are asking, but:
Mocha is a framework to run "unit tests" to test the software you have written and see if it's working ok.
If you just want to count some files in a directory you don't need a testing framework. Just use node's normal filesystem classes in fs https://nodejs.org/api/fs.html to write your code.

If you want to check that files maybe you are talking about integration test and not unit test. So, I believe that if you want to make unit test you will need read something about mock's and stub's. You can search modules like sinon. A few weeks I was fighting with test that checks if dirs or files are created and I reached the conclusion that if I want to make unit tests I need that module. Here's Sinon

Related

Is there a straightforward way in Jest to specify an array/list of tests to run?

Problem
I'm writing several test suites (using Jest and Puppeteer) to automate tests of my AngularJS app's home page. At this point, I have nearly 70 tests in one of my test files (aka test suite). And I'm wondering if it's possible to pass an array of test names to Jest's -t option to run a specific subset of tests in one of my test files. This will be helpful for debugging, as I have some tests that are interdependent and thus always need to be run together, but it would be nice to avoid having to run the entire test suite (which runs in about a minute) every time I want to debug a few tests.
Overview of my testing environment:
Puppeteer version: 1.19.0
Jest version: 24.8.0
Jest commands I've tried
I've been reading the Jest docs and so far I've come across a few CLI options that have the potential to solve this problem, but have not been fruitful so far.
jest name-of-test-file.spec.js: This runs all the tests in a single test file, but I don't want to do that, I just want to run a specific subset of tests in a test file.
jest name-of-test-file.spec.js -t name-of-test: This only runs a single test in a test file, but I want to run a few different interdependent tests.
jest name-of-test-file.spec.js --testPathPattern=<regex>: This runs all the tests whose names match the regex pattern I pass. It could be helpful but so far I haven't been able to come up with a valid regex that matches against three unique strings (i.e. test names). Unfortunately my test names differ greatly so it would be hard for me to come up with a more generalized regex to match the specific tests I want to run.
Final thoughts/ question:
I think option 3 has the most potential, but I haven't been able to find a working regex.
If the regex option does not work for you, you can use it.only (or test.only) to only run specific tests inside a module.
Quote from the docs:
When you are debugging a large codebase, you will often only want to run a subset of tests. You can use .only to specify which tests are the only ones you want to run.
Code Sample
it.only('This test will run', /*... */);
it.only('This test will also run', /*... */);
it('This test will not run', /*... */);
However, you still need to specify which module or file to run in case you are having multiple test files:
jest name-of-test-file-to-run.spec.js

How to run plain Mocha tests within Meteor test framework

I am trying to run Mocha tests within the Meteor test framework for a certain subset of code in a Meteor project. In particular, this is an internal library that does not actually use any Meteor features, and the test just needs to read in a file, do some computations, and compare results (no server or database or anything). So in theory, this could be its own package with its own testing framework. However, I would still like to use the Meteor test framework if possible just so that I don't need to run two sets of tests. Also, it would be nice to avoid maintaining the dependencies associated with a second test framework. So I have one general and one specific question along these lines:
Which testing package (see http://guide.meteor.com/testing.html#mocha or recommend another) is most appropriate for running "plain" Mocha tests, i.e. most similar to just running mocha from the command line? It seems to be dispatch:mocha, but it "only runs server tests", which doesn't seem ideal. It would help to get some clarification on exactly what "only runs server tests" means and how dispatch:mocha differs from plain mocha.
I was able to get a test sort of working with dispatch:mocha, but there were two problems:
I had to put the test file in the server directory, even though it is shared client and server code.
In order to read test data from a file, I had to put the test data in the private directory and use Assets.getText(). I initially tried (and would prefer) to put the test data in the same directory as my test, but the test gets built in some kind of way that ignores my test data in that case (I can give more details if this is supposed to work).
Is there some way I can avoid the above?

Node.js Code Coverage of Certain Test Files

I just came into a Node.js project that has a ton of unit tests, most of which, unfortunately, are outdated and don't run. There's no test runner or any kind of code coverage tool implemented, and it is certainly not in my scope of work to refactor everything.
That being said, that doesn't stop me from wanting to check the code coverage of my work. I've looked at both Istanbul and Blanket and can't find a way to only have them run certain unit tests. Surely, there is a way so that I can get a report that, obviously, reports a very low percentage of coverage, but that I can dive into certain files and such (those that my tests hit) to ensure my code is throughly tested.
With istanbul.js, you can easily get the coverage information this by for example specifying the following command (Following example uses mocha but it will be similar for any test framework):
istanbul cover node_modules/.bin/_mocha -- -u exports -R spec test/test1.spec.js test/test2.spec.js
You can also specify all the tests in particular sub-directory eg: test/yourfeaturetest/*.spec.js. You also have something like test/myfeature/**/*.spec.js to cover all the tests in the test/myfeature directory including tests that could have been created recursively in sub-directories.
As for me, I use gulp and thus utilize plugins such as gulp-istanbul and run tests and coverage via gulp tasks.

How do I run tests for a single model using mocha/mean.js?

I've been looking into mean.js testing. There's a grunt test task that runs all app with a single and easy grunt test, but I don't want to run all tests everytime.
I've installed mocha globally, and want to run tests for a single model using it, but doing...
mocha 'app/tests/mymodelname.server.model.js'
Throws me back a
"MissingSchemaError: Schema hasn't been registered for model
MyModelName"
What would be the easiest way to run single tests for the server?
Thanks!
D
Previous replies are completely correct, there's a mongoose loader that is defined within grunt and passed into the 'grunt test' task. Take a look at gruntfile.js:grunt.registerTask('test', ['env:test', 'mongoose', 'mochaTest', 'karma:unit']);
I've looked around for nice solutions to this, and there should be a '--grep' switch to use with Mocha, but getting it passed in through grunt is beyond my current skillset - suggestions welcome!
I'm sure there's a really great way to do this, but for now I've just restricted the test file configuration list when wanting to work with just one test. I've modified 'config\assets\test.js' to point to just one file. It's not pretty, and I'll probably get burned for suggesting it, but it works for me right now.
As robertklep mentioned, the best way to do this is by using the --require option in mocha to require server.js file (which includes all the required initialization for database module and additional stuff).
So in this case if you are running a mean.js app, the right way to do it would be:
mocha 'app/tests/mymodelname.server.model.js' -r server.js
Hope this helps!

Unit testing in RequireJS and QUnit basics

I am just trying to get my head round unit testing in Javascript and RequireJS. I am building a web-app and obviously only want to have tests run in development not production builds.
Questions:
Do you just test when you want to, or do you have JS tests running
on every page load when in development?
If tests are only on demand
then how do you trigger your tests to run? Query strings (eg.
?testing=true) or something like that?
I just need an idea of how people go about testing in development. I am using BackboneJS, RequireJS and jQuery on the front end with a NodeJS/ExpressJS server on the backend.
For a Backbone project at work we have a maven build process that runs our automated javascript tests through jsTestDriver, and we read the results with Sonar. I usually run the tests manually (with 'mvn test'), but I could easily tell maven every time I save a file, for example. I wrote a post that shows how to integrate QUnit, Requirejs, and code coverage with JSTD that is independent of Maven: js-test-driver+qunit+coverage+requirejs. It also contains links to a QUnitAdapter that is way more up-to-date and developed than the one on the jsTestDriver site. I'll update this post when I manage to write about how I got jsTestDriver working with Maven and Sonar. Hope it helps.
Grunt is a popular JS build tool. There's something called grunt-watch that can monitor certain files for change, and execute tasks accordingly. You could easily run unit tests with something like this on every save.
Usually end-to-end tests take longer, and we use the CI for that. I've seen a presentation on Meteor TDD that does end-to-end tests after every save though.
There are many end-to-end test frameworks, and they can run in a headless browser like phantom js using a build tool like grunt. Some frameworks open an actual browser to run the tests, but run via command line and report results using XML.
If you break out your components enough, the tests could have a small enough scope to run on each save.
For some core code I use JsUnit + Rhino on build server. For more complex bits (usually interface) I use selenium (it also runs on build server). I don't test anything on page load, I only use not-compressed versions of scripts.
I don't any solution for integration tests.

Categories