Keep Mocha tests alongside source files - node.js

I currently have my NodeJS source files in src and test suites in test, e.g.:
/src/bar/baz/foo.js
/test/bar/baz/foo.spec.js
This leads to awkward require statements like var foo = require('../../../src/bar/baz/foo'). And it's hard to see at a glance which source files are missing tests. I would like to instead keep my test suites in the same directory as the relevant source files:
/src/bar/baz/foo.js
/src/bar/baz/foo.spec.js
But now running mocha --recursive src causes errors as Mocha tries to run my source files as tests.
I've seen suggestions of using find or gulp to filter the file list but I find it surprising that this can't be done with plain Mocha. What's the recommended way of organising files this way?

Just pass the pattern of your test files to mocha, like:
mocha "src/**/*.spec.js"
This is going to run the .spec.js files in all subdirectories of src.

Related

How to Ignore testing file in specs tests folder with Jest?

I have a folder specs which contains test suites and I want to ignore omit.spec.js from testing temporarily.
How could I do that?
you can use testPathIgnorePatterns
npm test -- --testPathIgnorePatterns=omit

Python: Unit Tests/ Integration Tests: Workaround for file paths?

I have some unit tests and integration tests I need to run in a project. The problem I face is that sometimes files are read (as mock data) and dependent on where I start the test, the relative paths has to be changed.
Is there a solution to not changing the paths, even if tests are started from different directories? (like in pycharm sometimes from a directory called 'tests' or at other times from the directory 'unit')?
For example:
I run my tests within Pycharm from a "test" directory. In one of my unit tests I read a file with the path: "../data"
Sometimes I run my tests from a directory called "unit tests" which resides in the "test" directory. Now I would have to adapt my file path from above, otherwise my file would not be found.
There are several options to solve the problem you describe:
Write a wrapper script that first sets the working directory to a fixed path before running the tests.
You could pass the absolute path in an environment variable to your test executable.
Rather than reading test data from a file, incorporate the test data into your test code.

Jest: Files names without .test or .spec in them

I'm migrating from Mocha/Chai to Jest, and I have a lot of test files. The file names are in the format componentTest.js and are all in subdirectories of /test. Jest doesn't seem to like that. Running jest /test doesn't find anything. Running jest /test/components/componentTest.js also says no tests found. Changing the /test director name doesn't work either. It's only when I change the name of the file to component.test.js that it finds the tests, but that's not going to work for my case without a script. What am I missing?
Jest will find any tests that are located inside a __tests__ folder as well.
And then to run your tests, just run: jest

Is there any way to run Cucumber tests from parent folder of features folder?

I am trying to run a git bisect while using our automated tests to check the commit if it is the one causing the defect. The structure of our program is:
app
- cucumber_tests
- features/
- main_features/
- cucumber.yml
- src/
Obviously this is not the default/standard folder structure for running Cucumber tests as you would want the features folder to be at top-level of your app. This was not my choice and cannot be changed.
I can run the tests by cd into cucumber_test and then run. However, in order to run git bisect it must be done at same level as the .git folder.
My question is: is there a way to run the Cucumber tests from a parent directory of the features folder in Cucumber? Being able to read the cucumber.yml file would also be very beneficial.
Is there a way to tell Cucumber that you are not using the standard folder structure?
Edit: I have been able to get the tests started by using cucumber -r cucumber_tests/features cucumber_tests/features/main_features/first.feature. However, it is unable to find some of the step definitions part-way through the test.
It appears that cucumber is looking for files in app/features not app/cucumber_tests/features
You can still use the same folder structure. But you need to change the parameters in order to run your features. Otherwise this will not load step definitions. I hope you have step definitions inside the features folder. So now use this command to run the features: cucumber -r <absolute path of the features folder[C:\users\xyz\project\some_folder\features] absolute_path_feature_file[C:\users\xyz\project\some_folder\features\example.feature]
This way you cucumber will load your step definitions even if you have a different folder structure.

Use jasmine-node to test meteor application with auto-test

I'm using jasmine-node to test my Meteor application and I want to use the auto-test feature so I don't have to rerun the tests all the time by myself.
My meteor application folder structure is like this:
server
foo.coffee
tests
foo.spec.coffee
And with the spec file I want to test code which is located in foo.coffee. I start jasmine-node with this args:
jasmine-node ./ --autotest --coffee --test-dir tests
And now I would assume that the autotest feature will react on all changes in the root folder but it just reacts on changes in the test folder. And I can't start it in the root folder because I get an error in the .meteor files (and I don't want to have jasmine testing/including the meteor code anyway).
So I want to have jasmine rerun the tests even if I change code in the server folder. How can I achieve that?
Use the --watch parameter along with --autotest and specify the directories that contain whatever files you want to have watched.

Resources