JsPsych What kind of unit tests are these? Javascript - javascript

I'm working on an existing project but I am new to Javascript. The project can be found here, it is JsPsych and I've searched the documentation here and cannot find the answer (http://docs.jspsych.org/)
I don't understand how they are doing unit tests. I have found the "testing" folder and you can view it below, here.
JsPsych test folder
Here's an example called "load.test.js"
const root = '../';
require(root + 'jspsych.js');
require(root + 'plugins/jspsych-single-stim.js');
test('jsPsych should be in the window object', function(){
expect(typeof window.jsPsych).not.toBe('undefined');
});
Does anyone recognize this type of unit test? Is this supposed to be paired with some software? Can someone explain how I would actually run this?

The tests are run with Jest. You can see that jest is called as the test command in package.json.
To run the tests, install node and npm on your system. Then run npm install in the repository's main directory. Then you can run npm test.

Related

Different jest results when running via npm/package.json

Can anyone help me to understand that behaviour:
when I ran in terminal jest command: all my tests passed
I have also defined package.json script where I do exactly the same command jest and when i run npm run tests jest command is executed and all my tests passed.
BUT:
I run it on dockerimage. I have copy all project files to Docker, install all depend. and run tests.
npm run tests - failed
jest - passed
I am on the build docker image, in terminal run both commands. I see that they are giving different results. I know that it different OS. maybe different libraries, but npm, node and jest versions are identical.
Looks like running npm run tests behave differently. I'm stuck, don't know what to check, and how to debug it. Where the problem might be?

How to run specific tests with frisby?

We are using frisby to run our integration tests and while developing them, it would be handy to execute one specific one or a group of tests, without having run all of them and see extra noise. Right now I am commenting out all the ones I don't want to run, which is getting tedious.
Is there a way, from the command line, to run specific frisby tests?
So basically, instead of
npm test
I want to be able to say
npm test --name: posts
Or something like that. I found this post about jasmine-only, but I'm not sure it will satisfy my needs.
Thanks so much!
I'm not sure if you're still looking for answer, but this is pretty simple.
Firstly install latest version of jasmine-node from command line: npm install jasmine-node -g
Then to run particular test use: jasmine-node --coffee putTestNameHere
Install jasmine-node module. Execute one file at a time - you can group your test cases in specific file:
jasmin-node moduleTestCases_spec.js
Also, if you want to specify exact test case name to be executed, you can make use of sequenty module. It is a nodejs module, which you can specify the order(and thus the exact test cases to execute).
To run specific test in Frishby just run:
npm test ./folder/filename.js
So lets assume you have an folder say test under that you have a file called api.spec.js
then you will execute like this:
npm test ./test/api.spec.js
Parallely don't forget to specify these below things into your package.json file
"scripts": {
"test": "mocha" }

How to run several QUnit (node.js) test files using a single command?

Scenario
We are trying to use the Node.js QUnit Module aka qunitjs for our Server-Side Testing so that we can write one test and run it on both server and client e.g: https://github.com/nelsonic/learn-tdd/blob/master/test.js
When we run a single file using the command:
node test/my_test.js
It works as expected.
However, when we have more than one test in a /test directory and try to run all the files as a suite using the following command:
node test/*.js
only the first file (alphabetically) gets executed.
see: https://github.com/nelsonic/hapi-socketio-redis-chat-example/tree/master/test
Question
How do we execute multiple test files with a single command?
We tried to dig through existing StackOverflow + GitHub Q/A for this but did not find any match. (any suggestions/help much appreciated!)
QUnit has command-line runner out-of-the-box, so just run:
npm install -g qunit
qunit
It will search for tests in test folder by default automatically and execute all of them. Also you may specify test scripts manually qunit test/only_this_test.js test/also_this_test.js
It's running, but tests are failing!
If the failure is related to undefined functions, that's because you need to tell your tests how to find your functions-under-test.
Modify tests.js:
// Add this in the beginning of tests.js
// Use "require" only if run from command line
if (typeof(require) !== 'undefined') {
// It's important to define it with the very same name in order to have both browser and CLI runs working with the same test code
doSomething = require('../index.js').doSomething;
}
Modify your js scripts:
//This goes to the very bottom of index.js
if (typeof module !== 'undefined' && module.exports) {
exports.doSomething = doSomething;
}
See more details in my other answer here https://stackoverflow.com/a/51861149/1657819
P.S. Nice tutorial in the repo! ;)

Run and add unit tests to nodejs project

I want to run the tests related to this project on github, see folder test on https://github.com/node-opcua/node-opcua . Unfortunately I have no idea which testing framework was used and how to run all the tests?
First, run npm install
This will install the required dependencies.
You can then use
npm test
See here:
https://github.com/node-opcua/node-opcua/blob/master/package.json#L15
That will run the tests using Mocha.

How to create a custom reporter with Mocha

I am sure I am missing something obvious here!
I have read the instructions here (https://github.com/visionmedia/mocha/wiki/Third-party-reporters), and have taken their code and added as a new node module (i.e. it is within node_modules/my-reporter/reporter.js). However, I can't seem to get mocha to load this reporter.
I have tried numerous variations …
mocha allTests.js -R ./node_modules/my-reporter/reporter.js
mocha allTests.js -R my-reporter
But nothing works :-(
I can successfully load my reporter within a JS file:
my_reporter = require('./node_modules/my-reporter/reporter.js')
console.log(my_reporter);
Has anyone got any hints?
u should provide the reporter like this:
mocha allTests.js -R './node_modules/my-reporter/reporter'
You should not provide the .js file extension as that is the normal convention for including modules.
It appears that if mocha is installed globally (which I believe it almost always is), you have to install your reporter the same way.
If you don't want to publish the reporter as a public module, you can just:
npm pack
npm install /path/to/your/module.gz -g
I've tried putting the reporter everywhere else that would make sense, but was getting "invalid reporter" unless it was installed globally.

Categories

Resources