I am trying to have some integration tests on my project using npm test and my directory is like this:
Main_directory==>tests==>integration==>auth.test.js and bar.test.js
On my package.json, I've got this:
"scripts": {
"test": "jest --watchAll --verbose"
},
when I execute the code npm test , I run into this mind-boggling error on terminal:
No tests found
In G:\main_directory
35 files checked.
testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 5 matches
testPathIgnorePatterns: \\node_modules\\ - 35 matches
Pattern: - 0 matches
Do you have any idea on how to bring everything back to normal and run some smooth integration tests?
You are matching for the folder __tests__ but you said that your tests are in the test folder. So either change the folder name where the tests are to __test__ or change the name in your jest config.
Related
I have Jest installed on my machine and typing jest from terminal results in tests from parent folers also getting executed. I want to run tests only from the current folder.
For e.g. if I go to c:/dev/app in terminal and type some-jest-command, it should only run files with .test.js present in the app folder. Currently, running jest command from app folder runs tests in parent folders too, which is not my desired behaviour.
By default, Jest will try to recursively test everything from whatever folder package.json is located.
Let's say you're in c:/dev/app, and your package.json is in c:. If your basic command to invoke Jest is npm test, then try with run npm test dev/app.
If you want to run the tests from a specific folder user the --testPathPattern jest flag. When setting up the npm script add the path to the folder as well. In your package.json add the flag in you npm scripts. Check the bellow code for an example.
"scripts": {
....
"test:unit": "jest --testPathPattern=src/js/tests/unit-tests",
"test:integration": "jest --testPathPattern=src/js/tests/integration"
....
},
If you want to watch as well for changes, use the watch flag:
{
...
"test:unit": "jest --watch --testPathPattern=src/js/tests/unit-tests",
...
}
After that open, the command line, change the directory where your project is and run the unit test.
npm run test:unit
or integration tests.
npm run test:integration
To only run testing in a specific directory and to coerce Jest to read only certain type of files(my example: 'ExampleComponent.test.js' with new Jest version #24.9.0 you must write exact "testMatch" in jest.config.json || package.json in "jest" part next "testMatch": [ "<rootDir>/src/__tests__/**/*.test.js" ],
This testMatch in my case hits all files with the prefix .test.js in tests/subdirectories/ and skips all other files like 'setupTest.js' and other .js files in 'mocks' subdirectory which is placed inside of 'tests' directory,so,my 'jest.config.json' looks like this
{
"setupFiles": [
"raf/polyfill",
"<rootDir>/setupTests.js"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"moduleNameMapper": {
"^.+\\.(css|less|scss|sass)$": "identity-obj-proxy"
},
"testMatch": [
"<rootDir>/src/__tests__/**/*.test.js"
]
}
Just adapt to your needs 'testMatch' regex.
A little note: This is for jest#24.9.0 && enzyme#3.10.0 if it matters to anyone.
I hope it will be useful to someone, cheers all.
--package.json
"scripts": {
"test": "jest"
}
--jest.config.js
module.exports = {
"testMatch": [
"<rootDir>/tests/unit/*.test.js"
]
}
From the root of your project, you can run jest <substring of files> and it will only run the test files which have the substring you added.
$ jest /libs/components
> [PASS] /libs/components/button.tsx
yarn:
yarn test nameoffolder
npm:
npm test nameoffolder
For example, if you have a folder named widget and you only want to run the tests in the widget folder you would run this command.
yarn:
yarn test widget
npm:
npm test widget
In my npm package, I would like to emulate the pattern Meteor follows: a source file (named client.js) has a test file (named client.tests.js) live in a src/ folder. Tests run with the npm test command.
I'm following the usage docs to the 't'. I do not want to use a find in my package test command.
I understand that mocha can recursively execute tests:
mocha --recursive
I understand that mocha can execute tests in a specific subfolder using the --recursive flag:
mocha src --recursive
I also understand that I can specify a glob to filter files by passing *.tests.js:
mocha *.tests.js
But, I want all three. I want mocha to test only files ending in tests.js in the src folder, recursively checking subdirectories.
mocha --recursive *.tests.js
// See the files?
$ > ll ./src/app/
total 168
-rw-r--r-- ... client.js
-rw-r--r-- ... client.tests.js
// Option A
$ > mocha --recursive *.tests.js
Warning: Could not find any test files matching pattern: *.tests.js
No test files found
// Option B
$ > mocha *.tests.js --recursive
Warning: Could not find any test files matching pattern: *.tests.js
No test files found.
// Option C
$ > mocha --recursive src/app/*.tests.js
3 passing (130ms)
3 failing
So...
Why is mocha not picking up the *.tests.js files in the subfolders?
Why DOES it work if I specify the full path to the file?
How do I make it work as desired?
The --recursive flag is meant to operate on directories. If you were to pass a glob that matches directories, then these directories would be examined recursively but if you pass a glob that matches files, like you are doing, then --recursive is ineffective. I would suggest not using --recursive with a glob because globs already have the capability to look recursively in subdirectories. You could do:
mocha 'src/app/**/*.tests.js'
This would match all files that match *.tests.js recursively in src/app. Note how I'm using single quotes around the pattern. This is to quote the pattern so that it is passed as-is to Mocha's globbing code. Otherwise, your shell might interpret it. Some shells, depending on options, will translate ** into * and you won't get the results you want.
Details:
I have a directory structure like this.
myapp
root-one
-web
-app
-tests
root-two
-grunt
node_modules
.bin
intern-runner
selenium-standalone
intern
selenium-standalone
grunt-shell
Gruntfile.js
From my grunt file I am using shell npm to launch the selenium standalone server like so..
shell: {
intern : {
options: { stdout: true},
command: [
"cd node_modules/.bin",
"start selenium-standalone start",
"intern-runner config=tests/intern basePath=../../../../root-one/web"
].join('&&')
}
}
}
grunt.registerTask('intern', ['shell:intern']);
After running my grunt command grunt intern, selenium starts but I get the following error from intern-runner.
Error: Failed to load module tests/intern
from C:/myapp/root-two/grunt/node_modules/.bin/tests/intern.js
Now because I set the path (or so I thought) using basePath=../../../../root-one/web. I would have expected it to try to execute from C:/myapp/root-one/web/tests/intern.js instead of remaining in the .bin directory.
Question:
So really the question is. What is the proper way to set the basePath for intern-runner on the command line? Because this doesn't seem to work. And according to the docs...
You can also specify any valid configuration option as an argument on
the command-line.
Which leads me to believe I probably just have the syntax wrong.
Sounds like this doesn't quite work the way I was expecting it to. As discussed here: https://github.com/theintern/intern/issues/449
So for now the work around was to just install intern globally vs trying to run it locally out of my project. So instead I did..
npm install intern -g
With my project structure like so...
myapp
root-one
-web
-app
-tests
root-two
-grunt
node_modules
.bin
selenium-standalone
selenium-standalone
grunt-shell
Gruntfile.js
Basically just removed intern because it is installed globally now. Which is located at the following on windows..
C:\Users\user\AppData\Roaming\npm\node_modules\intern
And my Gruntfile has been changed too the following. After starting selenium I change directory back to the appropriate location to run the intern-runner command.
shell: {
intern : {
options: { stdout: true},
command: [
"cd node_modules/.bin",
"start selenium-standalone start",
"cd ../../../../root-one/web",
"intern-runner config=tests/intern
].join('&&')
}
}
}
grunt.registerTask('intern', ['shell:intern']);
Hiho,
I've got a problem with my Mocha configuration. I've got ES6 code which should be compiled by Babel and then I want to get coverage (in LCOV format) of this ES6 code.
My approach to this problem was to use mocha, mocha-lcov-reporter, babel and blanket packages. Code structure is:
-- src
----- ...
-- test
----- spec
-------- something.spec.js
-------- ...
----- blanket.js
Where specs are in test/spec directory (matches also *.spec.js pattern) and blanket.js is:
require('blanket')({
pattern: require('path').join(__dirname, '..', 'src')
});
Command which I prepared is:
./node_modules/.bin/mocha $(find test -name '*.spec.js') --recursive --compilers js:babel/register -r test/blanket -R mocha-lcov-reporter
So, it should run Mocha tests for all *.spec.js files, compiling them by Babel and starting test/blanket.js file before.
After starting this command I get Error: Line 1: Unexpected reserved word error from esprima.js. When I run it without requiring test/blanket file it run without problems, but ofc I haven't coverage.
Has anyone tried to do so? Do you have any ideas how to do it?
Okey, problem already resolved, but without Babel (native ES6 instead); I've done it another way. I've used istanbul-harmony and mocha packages. Then the command is:
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- $(find test -name '*.spec.js') -R spec -u exports
Situation : I am currently using QUnit to test a project in TypeScript/Javascript and everything works fine when I'm running them in a browser.
Problem : I'm trying to use grunt to run the QUnit tests in a headless mode (I need it for continuous integration testing) and the tests don't run properly.
Configuration
Here's how I have things currently set up :
Gruntfile.js
package.json
src/
- Ts source files
test/
- config.js
- Test.ts
- Test.js
- test.html
Gruntfile.js
/*global module:false*/
module.exports = function(grunt) {
grunt.initConfig({
connect: {
server: {
options: {
port: 8000,
base: '.'
}
}
},
qunit: {
all: {
options: {
urls: [
'http://localhost:8000/test/test.html'
]
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.registerTask('test', ['connect', 'qunit']);
};
package.json
{
// Name, version, description, repo and author fields...
"engines": {
"node": ">= 0.10.0"
},
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-watch": "~0.6.1",
"grunt-contrib-connect": "~0.9.0",
"grunt-contrib-qunit": "~0.5.2"
}
}
And then I have a .travis.yml file to run all of this. I don't know if it's really important because the tests don't run either in travis or in my local environment, but here is it anyways :
language: node_js
node_js:
- "0.11"
- "0.10"
before_install:
- "npm install grunt --save-dev"
- "npm install -g grunt-cli"
install:
- "npm install"
- "npm install -g typescript"
script:
- "tsc --module amd --target ES5 ./src/*.ts"
- "grunt test --verbose --force"
And here's the part that errors in the travis build : http://puu.sh/eKpWj/35614680e1.png
(I currently have ~20 assertions that pass when I'm running them in a browser. Also, the typescript compilation runs ok.)
Edit : And as someone asked fot it, here's the content of the Test.html file : http://pastebin.com/LN3igmjc
Edit 2 : Here's also the content of config.js :
var require = {
baseUrl: "../src/"
};
Actually I managed to make it work.
I changed two things :
I wasn't compiling the tests, as tsc --module amd --target ES5 ./src/*.ts compiled the files in the src folder, and the test files were in the test folder. I'm bashing myself for this one...
So I simply added tsc --module amd --target ES5 ./test/*.ts in the .travis.yml file
The biggest problem was that the QUnit tests were trying to start before the work of require.js. The solution I used was to tell QUnit to not start tests automatically by using QUnit.config.autostart = false; and make them start when I want with QUnit.start(); I placed this start() at the end of my Test.js file so that the tests start only when QUnit is done loading.