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

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

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.

Run specific jest project

I'm setting up a lerna monorepo with jest, I'm using jest's projects like so: projects: ['<rootDir>/packages/*'].
Running tests work as expected, however, I'm not sure how can I run a specific project? Say I have:
/packages
jest.config.js
/core
jest.config.js
/blog
jest.config.js
Currently jest runs tests in both packages using their specific configs, however, I'm not sure how can I tell jest to just run tests in one of those packages?
Assuming you want to do this with Jest's projects property:
As of Jest v26.1.0, you can now run selected projects with Jest by doing the following:
jest --selectProjects myproj
This will find any "project" in your jest.config.js by it's displayName value.
See:
https://github.com/facebook/jest/issues/7542
https://github.com/facebook/jest/pull/8612
https://github.com/facebook/jest/releases/tag/v26.1.0
You can call jest with the name of a test that you want to run. You can also use just parts of the path to the test, or even a regular expression. So in your case, you could run tests in the core package like this:
jest packages/core
There is currently no clean way of doing it from the CLI (see https://github.com/facebook/jest/issues/6189), but you can use https://github.com/rogeliog/jest-watch-select-projects to achieve it in watch mode

Keep Mocha tests alongside source files

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.

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