In my React application I have configure Jest and Enzyme for snapshot testing.
One of my tests failed with an error:
ReferenceError {
message: 'window is not defined',
}
I'm using window (a global object) in a function and calling that function from a test file which contains a window object and that’s producing an error.
How can I resolve this? I tried
"globals":{
"window":true
}
in my package JSON file, but I am still having this issue.
My test script is also running jsdom.
It seems like JSDOM is not properly installed or somehow disabled. See if you can find something like this in your configuration:
"testEnvironment": "node"
https://jestjs.io/docs/en/configuration.html#testenvironment-string
Related
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'm trying to use Jest for unit testing react-native and I'm using the #providesModule successfully in the app, but Jest is failing to load the modules using this format
"import Component from "Component"
jest: version 13.2.3
react-native: 0.29.0
The error is:
FAIL js/components/home/__tests__/TestHome.js (0s)
Runtime Error
- Error: Cannot find module 'Time' from 'TimelineItem.js'
at Resolver.resolveModule (node_modules/jest-cli/node_modules/jest-resolve/build/index.js:197:17)
There is a small reference to 'providesModule and jest' on a blog post here
https://facebook.github.io/jest/blog/2016/06/22/jest-13.html
but it's not clear exactly what needs to be done.
If someone has #providesModule working with Jest and react-native, could you please help by indicating exactly what needs to be done?
with Jest 14.0 you can use Snapshot Test
You have to use React's react-test-renderer to render your component first
When running the tests on master code npm test works fine,
but when I build Fabric locally (with node build.js modules=ALL)
the test breaks with error:
{ [Error: Cannot read property 'navigator' of undefined] message:
'Cannot read property \'navigator\' of undefined' }
Even without any changes to the codebase. I guess I'm missing some build flag.
Try this: node build.js modules=ALL exclude=gestures,cufon,json minifier=uglifyjs.
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.
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.