I have a project with 2 functional Suites, the structure is like this:
- /tests
--- /sign
------ sign.js
--- /buy
------ buy.js
--- intern.js
- /node_modules
- ....
On my intern.js I have this
//...
functionalSuites: [ 'tests/sign/sign', 'tests/buy/buy'],
//...
And to run the test intern-runner config=tests/intern
So far so good, 2/2 tests passed.
I would like to know if it's possible to pass some arguments to execute only a part of the functionalSuites
ex:
intern-runner config=tests/intern --sign
And somehow it would run only the functionalSuite tests/sign/sign
And if I pass intern-runner config=tests/intern --all it would run all functionalSuite tests.
Thank you
You should use the grep parameter introduced in Intern 2.1 release announce. With grep you can target one specific test or a class of tests depending on your naming convention.
In our case, the functionSuites settings contains only 'test/functional/**/* to be sure that no functional test is forgotten by the CI process. And we use the grep attribute during the script development.
The simple solution is:
intern-runner config=tests/intern functionalSuites=test/sign/sign
The more complicated (but good for long term) is to write a filter function by yourself (hmm, maybe you can make a request to https://github.com/theintern/intern?). We do have our custom filter, but it's too complicated to write down here. If you make a request to Intern, maybe I will try to spend sometime to make a pullrequest for it (or maybe someone else) Anyway the result I get is:
(I have 2 files named file1_func.js, file2_func.js and some more files)
Related
I am looking to make my automation tests a bit more flexible. I have a QA team that does not know much javascript and possibly have to design the tests for users with no or little programming skills.
I had a few scripts created using mocha test framework and spectron.js (for an app built with electrion.js) test a few features of the product I dont want to run every single test every time I run the script. My temporary solution is bundle the tests in to a function as a "suite". Like this -
function DiagnosticSuite(location, workstation, workflowName){
CreateWorkflow(location, workflowName);
SetWorkFlowToStation(location, workstation, workflowName);
DiagnosticTestFlow();
return;
}
function PowerflowSuite(imei, location, workstation, workflowName){
SetWorkFlowToStation(location, workstation, workflowName);
powerOffFlow(imei);
return;
}
I was thinking of using Inquirer and use a conditional based on input to run one of the tests above. Like this -
inquirer.prompt([
{
type: 'list',
name: 'Which workflow do you want to run?',
choices: ['Power Off', 'Diagnostic']
}
]).then((answers) => {
if(answers == 'Power Off'){
PowerflowSuite(imei, location, workstation, workflowName);
}
})
How ever when I test that Mocha seems to not wait for the user input from inquerior to run the tests and I get an output like this -
$ npm test
> metistests#1.0.0 test C:\Users\DPerez1\Desktop\metis-automation
> mocha
? Which workflow do you want to run?: (Use arrow keys)
> Power Off
Diagnostic
0 passing (0ms)
Seems like it runs and doesnt see a any tests and finishes and when I select the answer the program just closes.
I am wondering why Mocha does this and if its possible to run my existing mocha scripts with a library like inquirer.
So I found a solution to my problem in case anyone stumbles here and is wondering.
I separated the CLI portion and the Mocha portion into different scripts on the package.json file. That way I can use nodes child process library to run the mocha part and pass the information from the CLI part via the process.argv objject.
So the CLI part will ask me what test to run, what env, and what user. And I create a command to put in child process exec function (I think there are others to use but not important.) When mocha tests run I parse the argvs and pass them into the functions so that the test can run based on that.
I made a React app using Create-React-App. I have a testing script in my React app's package.json like this:
"test": "node -r #babel/register -r #babel/polyfill **/*.test.js | tap-color",
This catches and executes the files in src/ like src/App.test.js, but not header.test.js in src/layout/header/header.test.js. And what if I add even more layers of folders? Is there a regex that will catch all folders in src/ and their subfolders no matter how nested with a file ending of .test.js?
Edit: I found this question on StackOverflow. According to that answer you would have to write:
"test": "node -r #babel/register -r #babel/polyfill 'src/**/*.test.js' | tap-color",
which unfortunately matches nothing for me. I'm on Mac.
The question you refer to is not especially useful for your case. My answer there works because Mocha has been designed to pass the patterns you give to it to the glob library for interpretation. So when you do mocha 'src/app/**/*.tests.js' the quotes prevent the shell from interpreting the pattern, Mocha gets src/app/**/*.tests.js as the first pattern given to it, which it gives to glob to get a list of files to actually run. Your case is different at least one crucial way: glob is not involved so there is nothing that can correctly interpret **.
Your first attempt is consistent with what happens when you are using a shell that does not understand **. It is interpreted exactly the same as *. So the shell interprets **/*.test.js as */*.test.js, expands this pattern and passes the result to node.
In your second attempt, you quote the pattern but that does not help you because node does not do pattern interpretation. It tries to load a file at path src/**/*.test.js, interpreted literally. This is not what you want.
I'm not sure what the compatibility implication with Windows are, but you could replace 'src/**/*.test.js' with $(find src -type f -name '*.test.js'). (See the man page for details.) At run-time, the shell will replace this with the result of the find command.
Or for greater simplicity, and less risk of platform issues creeping up, you could use babel-tap like this:
babel-tap 'src/**/*.test.js' | tap-color
If you use babel-tap, there's actually no need for using find because internally babel-tap calls on facilities that use the glob library to interpret the file names passed to it.
I've focused on the file pattern issue but I'm not seeing how what you're trying to do would work, even without the pattern issue. Consider this command, and assume that the files exist:
node -r #babel/register -r #babel/polyfill src/a.test.js src/b.test.js
This is not telling Node to run src/a.test.js and src/b.test.js. Rather, it tells node "run the script src/a.test.js and pass to it the parameter src/b.test.js". I've not used tap very much but I don't recall it working this way. Using babel-tap like I show above also avoids the problem here.
I'm completely new to sails, node and js in general so I might be missing something obvious.
I'm using sails 0.10.5 and node 0.10.33.
In the sails.js documentation there's a page about tests http://sailsjs.org/#/documentation/concepts/Testing, but it doesn't tell me how to actually run them.
I've set up the directories according to that documentation, added a test called test/unit/controllers/RoomController.test.js and now I'd like it to run.
There's no 'sails test' command or anything similar. I also didn't find any signs on how to add a task so tests are always run before a 'sails lift'.
UPDATE-2: After struggling a lil bit with how much it takes to run unit test this way, i decided to create a module to load the models and turn them into globals just as sails does, but without taking so much. Even when you strip out every hook, but the orm-loader depending on the machine, it can easily take a couple seconds WITHOUT ANY TESTS!, and as you add models it gets slower, so i created this module called waterline-loader so you can load just the basics (Its about 10x faster), the module is not stable and needs test, but you are welcome to use it or modify it to suit your needs, or help me out to improve it here -> https://github.com/Zaggen/waterline-loader
UPDATE-1:
I've added the info related to running your tests with mocha to the docs under Running tests section.
Just to expand on what others have said (specially what Alberto Souza said).
You need two steps in order to make mocha work with sails as you want. First, as stated in the sails.js Docs you need to lift the server before running your test, and to do that, you create a file called bootstrap.test.js (It can be called anything you like) in the root path (optional) of your tests (test/bootstrap.test.js) that will be called first by mocha, and then it'll call your test files.
var Sails = require('sails'),
sails;
before(function(done) {
Sails.lift({
// configuration for testing purposes
}, function(err, server) {
sails = server;
if (err) return done(err);
// here you can load fixtures, etc.
done(err, sails);
});
});
after(function(done) {
// here you can clear fixtures, etc.
sails.lower(done);
});
Now in your package.json, on the scripts key, add this line(Ignore the comments)
// package.json ....
scripts": {
// Some config
"test": "mocha test/bootstrap.test.js test/**/*.test.js"
},
// More config
This will load the bootstrap.test.js file, lift your sails server, and then runs all your test that use the format 'testname.test.js', you can change it to '.spec.js' if you prefer.
Now you can use npm test to run your test.
Note that you could do the same thing without modifying your package.json, and typying mocha test/bootstrap.test.js test/**/*.test.js in your command line
PST: For a more detailed configuration of the bootstrap.test.js check Alberto Souza answer or directly check this file in hist github repo
See my test structure in we.js: https://github.com/wejs/we-example/tree/master/test
You can copy and paste in you sails.js app and remove we.js plugin feature in bootstrap.js
And change you package.json to use set correct mocha command in npm test: https://github.com/wejs/we-example/blob/master/package.json#L10
-- edit --
I created a simple sails.js 0.10.x test example, see in: https://github.com/albertosouza/sails-test-example
Given that they don't give special instructions and that they use Mocha, I'd expect that running mocha from the command line while you are in the parent directory of test would work.
Sails uses mocha as a default testing framework.
But Sails do not handle test execution by itself.
So you have to run it manually using mocha command.
But there is an article how to make all Sails stuff included into tests.
http://sailsjs.org/#/documentation/concepts/Testing
I have a grunt task like so:
grunt.registerTask 'test', ['clean:test',
'coffee:test',
'mochaTest',
'clean:test']
If mochaTest returns with a fail code, the last clean won't run and will leave unwanted transpiled files.
It doesn't throw an error so I can't try/catch/finally and google/reading source code doesn't provide me with a solution except for manually running grunt clean:test after each fail.
Am I going about this the wrong way, or is there a nifty way to do something similar to a finally-block?
(I know I can run mocha using coffee-files, this is a simplified example problem)
The reason the final 'clean:test' doesn't run is that Grunt is designed to stop on failures. If you used the --force option (grunt test --force) you could cause it to continue after the failure of mochaTest. This is not a good habit to get into, or would not make sense in cases where your chain of tasks was longer, more interdependent, or more complex.
What I did to solve this was in my Makefile to add
test: grunt test && grunt clean:test
to ensure a proper cleanup even after a failed test.
$make test
to run it.
I have started implementing TDD in my JS project. I've implemented mocha for that purpose. As these are my first steps what I did:
Installed node.js
Installed mocha globally and locally to my project.
Wrote package.json setting dependencies.
Wrote makefile.
Wrote .gitignore to avoid uploading node_modules folder.
Folder structure
project
-- js
----filetotest.js
-- test
---- test.js
What I want to do is to run the command make test in order to run the tests inside test.js that tests the filetotest.js file.
I read about the node.js approach using exports. But is there some way to include the file in the test suite?
I'm stuck here, and I think that my doubt is more about the concept than the tech thing. Will appreciate a lot your help.
To clarify a little bit what I would like to do:
https://nicolas.perriault.net/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/
I would like to get a similar result through the command line.
Thanks so much,
Guillermo
You are doing it right.
Now export your function from filetotest.js, like this:
var f1 = function(params) {
// ...
}
exports.f1 = f1
In test.js, require this file
var f1 = require("./filetotest.js").f1
// test f1
Btw, if you will put your tests in /test directory, mocha will execute them automatically (given that it will be executed from the root of your project)