I'm learning to use sinon with nodeunit, specifically to do mocking. The recommended approach is to use sinon-nodeunit. According to the documentation, mocks should be available via the mock method of the test parameter to each test function.
However, this doesn't seem to work. If I run nodeunit on a file containing the following —
require('sinon-nodeunit');
exports['test sinon-nodeunit'] = function (test) {
mock = test.mock({});
test.done();
};
— I get this:
$ nodeunit test/test-sinon-nodeunit.js
test-sinon-nodeunit.js
✖ test sinon-nodeunit
TypeError: Object #<Object> has no method 'mock'
at /home/malkovich/test/test-sinon-nodeunit.js:4:17
at Object.runTest (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:54:9)
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:90:21
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:508:13
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:118:13
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:134:9
at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:507:9
at Object.concatSeries (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:147:23)
at Object.runSuite (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:79:11)
at Object.runModule (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:114:13)
FAILURES: 1/1 assertions failed (84ms)
I've installed current versions of both sinon (1.1.1) and sinon-nodeunit (0.1.2) via npm install.
require.path was removed in node 0.5.x and higher, which sinon-nodeunit uses. Christian Johansen just pushed a change in sinon-nodeunit which should be compatible with node 0.6.2 (and 0.5.x-series) which should fix this... It's already available on sinon-nodeunits github page, still hasnt hit npm though...
update: should be mentioned that sinon-nodeunit has been discontinued, as the creator is full focused on BusterJS which IMHO is a much better framework.
Related
I've recently installed dirty-chai, which states:
The following built-in assertions are modified by this plugin to now use the function-call form:
The idea being, that hopefully, we get a fail in a test trying to run expect(true).to.be.true as it doesn't call the final property being accessed as a function.
But writing that test, gives me no such test failure.
I do note that the same doc also says:
These forms can also be mixed, but the chain must always be terminated in the function form or assertions up to that point in the chain will not execute.
emphasis mine
However I have the code running with all of the below, and no test failures:
expect(true).to.be.true;
expect(true).to.be.ture;
expect(true).to.be.true();
I have dirty-chai installed like so, with Mocha 9.1.2, Chai 4.1.2, and the latest version of dirty-chai:
const Chai = require('chai');
const dirtyChai = require('dirty-chai');
....
Chai.use(dirtyChai);
this is all wrapped up in a javascript file that's passed to mocha via mocha <other options> -r ./test/chaiPlugins.js
Is dirty-chai not behaving as expected, or am I expecting it to behave in a way it doesn't?
I read all the related mocha "describe" is not defined posts but none of them seem to be suitable for my situation.
I use meteor and installed the "mocha": "^3.5.0" package by npm
I have created a /testfolder in my meteor root directory.
and a sample test mochatest.js
var assert = require("assert"); // node.js core module
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
assert.equal(-1, [1,2,3].indexOf(4)); // 4 is not present in this array so indexOf returns -1
})
})
});
When i run mocha the test passes.
But when i start my normal server i get: ReferenceError: describe is not defined
.meteor/packages/meteor-tool/.1.3.5_1.1wj76e8++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280
throw(ex);
^
ReferenceError: describe is not defined
at meteorInstall.test.mochatest.js (test/mochatest.js:3:1)
at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:153:1)
at require (packages/modules-runtime/.npm/package/node_modules/install/install.js:82:1)
at project-i18n.js:6:1
at .meteor/local/build/programs/server/boot.js:297:10
at Array.forEach (native)
at Function._.each._.forEach (.meteor/packages/meteor-tool/.1.3.5_1.1wj76e8++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
at .meteor/local/build/programs/server/boot.js:133:5
I have a feeling that meteor wants to run the test on startup but can't find mocha stuff.
So what to do?
You need to rename the /test folder to /tests.
From the official Meteor Testing Guide:
The Meteor build tool and the meteor test command ignore any files
located in any tests/ directory. This allows you to put tests in this
directory that you can run using a test runner outside of Meteor’s
built-in test tools and still not have those files loaded in your
application.
You could also consider renaming your test files with extensions like:
*.test[s].*, or *.spec[s].* -- So your file could be named mocha.test.js.
Such files are ignored by Meteor build tool as well.
Looks like the test file is somehow invoked when the server runs. It should not be included anywhere in the server code.
If you can't figure out what's happening, you can try to debug the server and put a breakpoint at the "describe" line - try to see how you got there.
I have a unit test that use jasmin.clock.install
I have the following error using jest-cli 20.0.4
TypeError: jasmine.clock is not a function
What package should I have in order to have this line work in my unit test :
jasmine.clock().install();
I managed to make it work by downgrading to jest-cli 19.0.1. it would be nice to know the upgrade procedure.
From the docs jasmine.clock().install(); is needed to mock out setTimeout calls. So this can be done in Jest by using jest.useFakeTimers();. Have a look at the docs on how to mock timer in Jest. Also have a look at the announcement of v20 to see why the Jasmine stuff does not work anymore
I have updated my protractor version to 5.1.1 and node.js to 6.10.2 and now all my tests from the project fail with following error:
TypeError: browser.driver.isElementPresent is not a function
As you can see, changing all 'browser.driver.isElementPresent' is not a solution for me as the project is large and I have there lots of lines that use this.
Do you have any ideas why this error occurs and how can it be fixed?
browser.driver.isElementPresent has been removed.
browser.driver is an instance of webdriver. The instance created comes from the selenium-webdriver node module. Protractor 5.1.1 uses selenium-webdriver v3.0.1 and as of v3.0.0-beta-1, the method isElementPresent was removed. In the changelog, Protractor documented a workaround.
Before:
let isPresent = browser.driver.isElementPresent(By.tagName('a'));
After:
let isPresent = element(By.tagName('a')).isPresent();
I'm trying to use sinon.js in testing of a backbone application. But unfortunately I cannot use spy method due to error:
TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')
Here is the steps to reproduce the error:
Create an empty project with backbone yeoman generator
Install sinon: cd test && bower install sinon
Include in test/index.html <script src="bower_components/sinon/lib/sinon.js"></script>
Create spy in test/spec/test.js:
describe('Give it some context', function () {
describe('maybe a bit more context here', function () {
it('should run here few assertions', function () {
var spy = sinon.spy();
spy.should.be.ok;
});
});
});
Run the test with grunt: grunt test
The test will fail with a described error.
Could anyone help to find out what is wrong?
I'll just leave here the list of files that sinon conveniently forgets to load if it is loaded as a <script> or with require.js (as AMD module) - basically anything else than in node.js:
"sinon/lib/sinon.js",
"sinon/lib/sinon/spy.js",
"sinon/lib/sinon/call.js",
"sinon/lib/sinon/behavior.js",
"sinon/lib/sinon/stub.js",
"sinon/lib/sinon/mock.js",
"sinon/lib/sinon/collection.js",
"sinon/lib/sinon/assert.js",
"sinon/lib/sinon/sandbox.js",
"sinon/lib/sinon/test.js",
"sinon/lib/sinon/test_case.js",
"sinon/lib/sinon/match.js"
Feel free to skip any of those but expect sinon to fail in curious ways.
It turned out that such functionality as spies, stubs, etc should be added manually by including scripts from lib/sinon folder. This fact is mentioned in Installation section. And due to the code of the core sinon.js file only in Node.js environment it is done automatically.
I encountered the same problem with sinon 1.17.2 and Chrome 47.0. After trying the above solutions and variations of those, I ended up using the nuclear option and switching to Jasmine.
For my test suite, it only took about 15 minutes of some global find-and-replace to convert my chai 'expects' into Jasmine ones and some differences around mocha before syntax; Jasmine flagged the unexpected syntax clearly. Jasmine spy objects were a fine substitute for sinon.
Unlike the other answers, I didn't install simon manually by including each individual source file. Instead I followed the advice of How To Install Sinon.JS In The Browser With Bower.
bower install http://sinonjs.org/releases/sinon-1.17.6.js
then
bower list -p
'sinon-1.17.6': 'bower_components/sinon-1.17.6/index.js'
And
<script src="bower_components/sinon-1.17.6/index.js"></script>
Worked for me.