The mocha hooks like before(), after(), beforeEach(), afterEach() are not working. Also the only method too not working. None of the beforeEach is called. I get an error has no method 'only'. Below is the code.
describe('Array', function(){
beforeEach(function(){
console.log('before every test')
})
describe.only('#indexOf()', function(){
beforeEach(function(){
console.log('before every test')
})
it.only('should return -1 unless present', function(){
assert.equal(1,2)
})
it('should return the index when present', function(){
assert.equal(1,2);
})
})
})
beforeEach(function(){
console.log('before every test')
})
Your code works for me. You need to add:
var assert = require('assert');
at top. And, be sure you npm install -g mocha.
Then, I get:
$ mocha -R spec temp.js
Array
#indexOf()
◦ should return -1 unless present: before every test
before every test
before every test
1) should return -1 unless present
◦ should return the index when present: before every test
before every test
before every test
2) should return the index when present
0 passing (16ms)
2 failing
1) Array #indexOf() should return -1 unless present:
AssertionError: 1 == 2
at Context.<anonymous> (/Users/dan/Dropbox/Documents/dev/spock/temp.js:12:16)
at Test.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:355:10)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:401:12
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:281:14)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:290:7
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:234:23)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:253:7
at Hook.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:213:5)
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:246:10)
at Object._onImmediate (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:258:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
2) Array #indexOf() should return the index when present:
AssertionError: 1 == 2
at Context.<anonymous> (/Users/dan/Dropbox/Documents/dev/spock/temp.js:15:16)
at Test.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:355:10)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:401:12
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:281:14)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:290:7
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:234:23)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:253:7
at Hook.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:213:5)
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:246:10)
at Object._onImmediate (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:258:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
Note that your it.only is being ignored because the describe.only is encountered first. If you aren't able to replicate this output, please report what version of node and npm you're using.
Related
am trying to skip all other tests from all spec files if one test fails and found a working solution over here Is there a reliable way to have Cypress exit as soon as a test fails?. However, this looks to be working only if the test fails in it() assertions. How can we skip the tests if somethings fails in beforeach()
For eg:
before(() => {
cy.get('[data-name="email-input"]').type(email);
cy.get('[data-name="password-input"]').type(email);
cy.get('[data-name="account-save-btn"]').click();
});
And if something goes wrong (for eg: CypressError: Timed out retrying: Expected to find element: '[data-name="email-input"]', but never found it.) in above code then stop/ skip all tests in all spec files.
Just in case anyone is looking answer for the same question. I have found a solution and would like to share.
To implement the solution I have used a cookie that I will set to value true if something fails and before executing each test cypress will check the value of cookie. If the value of cookie is true it skips the test.
Cypress.on('fail', error => {
document.cookie = "shouldSkip=true" ;
throw error;
});
function stopTests() {
cy.getCookie('shouldSkip').then(cookie => {
if (cookie && typeof cookie === 'object' && cookie.value === 'true') {
Cypress.runner.stop();
}
});
}
beforeEach(stopTests);
Also to note: Tests should be written in it() block and avoid using before() to write tests
As of Cypress 10, tests don't run if before or beforeEach hook fails.
We're using mocha. At the end of a test run, there is the following output.
22 passing (1m)
2 pending
2 failing
1) Some message and stack trace.
2) Some message and stack trace.
++++++++++++++
warn: Some condition happened. <---- How do we do ths?
++++++++++++++
We want to add our own, custom message at the end there.
For instance, we have a condition in some tests like this:
it(..., () => {
if(someConndition) {
log("Some condition happened.");
}
});
Our log function currently outputs to console, but we want to have it output at the end of running all the tests.
function log(message) {
console.warn("++++++++++++++");
console.warn(message);
console.warn("++++++++++++++");
}
Given the following test.js
var someCriticalFunction = function() {
throw 'up';
};
describe('test 1', () => {
it('is true', () => {
expect(true).toBe(true);
})
});
describe('test 2', () => {
it('is ignored?', () => {
someCriticalFunction();
expect(true).toBe(false);
})
});
Running Jest will output
Using Jest CLI v0.9.2, jasmine2
PASS __tests__/test.js (0.037s)
1 test passed (1 total in 1 test suite, run time 0.795s)
Which pretty much gives you the impression everything is great. So how do I prevent shooting my own foot while writing tests? I just spent an hour writing tests and now I wonder how many of them actually run? Because I will certainly not count all of them and compare with the number in the report.
Is there a way to make Jest fail as loud as possible when an error is thrown as part of the test suite, not the tests itself (like preparation code)? Putting it inside beforeEach doesn't change anything.
This is fixed in 0.9.3 jest-cli https://github.com/facebook/jest/pull/812
Which for some reason is unpublished again (I had it installed minutes ago). So anyway, throwing strings is now caught. You should never throw strings anyway and thanks to my test suite I found one of the last places I actually still threw strings...
I am getting this error:
Error: Spies must be created in a before function or a spec
My Test code should be sound:
describe 'A spy', ->
foo = undefined
bar = null
beforeEach ->
foo = setBar: (value) ->
bar = value
return
spyOn foo, 'setBar'
foo.setBar 123
foo.setBar 456, 'another param'
return
it 'tracks that the spy was called', ->
expect(foo.setBar).toHaveBeenCalled()
Turnsout I had mocha and jasmine declared inside my karma.conf.js. As a result the frameworks interpreted both beforeEach differently.
Dropping mocha from my karma config file worked.
You need to create your spy inside the beforeEach block.
I'm using mocha with chai.assert for my tests. Errors are caught and reported, but they don't show a file/line number where they happen. I'm used to having location information with tests in other languages, it's otherwise hard to figure out which assert failed.
Is there some way to get location information with mocha/chai/assert?
From version 1.9.1 onwards, if you set the includeStack flag to true, you'll get a stack trace on assertion failures:
var chai = require("chai");
chai.config.includeStack = true;
var assert = chai.assert;
describe("test", function () {
it("blah", function () {
assert.isTrue(false);
});
});
In versions prior to 1.9.1 you had to set chai.Assertion.includeStack = true. From 1.9.1 onwards this method of getting stack traces is deprecated. It is still available in 1.10.0 but may be removed in 1.11.0 or 2.0.0. (See here for details.)
The example above will show a stack trace where assert.isTrue fails. Like this:
AssertionError: expected false to be true
at Assertion.<anonymous> (.../node_modules/chai/lib/chai/core/assertions.js:193:10)
at Assertion.Object.defineProperty.get (.../node_modules/chai/lib/chai/utils/addProperty.js:35:29)
at Function.assert.isTrue (.../node_modules/chai/lib/chai/interface/assert.js:242:31)
at Context.<anonymous> (.../test.js:7:16)
[... etc ...]
(I've truncated the trace to what is only relevant and truncated the paths.) The last frame shown in what I've included above is the one where the error happened (.../test.js:7:16). I do not think that chai allows having only the file name and line number of the assertion call.
chai.Assertion.includeStack is now deprecated. Use chai.config instead
var chai = require("chai");
chai.config.includeStack = true;
var assert = chai.assert;