chai

Loading existing HTML file with JSDOM for frontend unit testing

Deadly 提交于 2019-12-06 09:09:45
问题 I'm new to unit testing, and I'm aware my tests may not be valuable or following a specific best practice, but I'm focused on getting this working, which will allow me to test my frontend code using JSDOM. const { JSDOM } = require('jsdom'); const { describe, it, beforeEach } = require('mocha'); const { expect } = require('chai'); let checkboxes; const options = { contentType: 'text/html', }; describe('component.js', () => { beforeEach(() => { JSDOM.fromFile('/Users/johnsoct/Dropbox

Mocha and Chai test fails when testing function with setInterval

試著忘記壹切 提交于 2019-12-06 07:30:17
问题 I'm new to TDD and working with Mocha and Chai. I have created a test that passes when a value is increased, but when that increase is put within a setInterval, it fails. The objective of this code is to have something move across the screen. function startMovingThing(){ var position = setInterval(function() { moveThing(10); }, 100); } function moveThing(number){ thing.position += number; thingOnScreen.style.left = thing.position + 'px'; } test: describe('Thing', function() { it('should

How to sinon spy module export utility functions

痞子三分冷 提交于 2019-12-05 22:53:19
In javascript (ES6), I have a utility module which just contains some functions, and then in the end of the file, I export them like so: module.exports = { someFunction1, someFunction2, someFunction3, } Then I want to write unit tests for those functions. Some of the functions depend on each other; they call each other in a way, that for example, someFunction1 might call someFunction2. There's no circular problems. Everything works well, until I need to spy that one of the functions is called. How can I do it? Currently I'm using Chai and Sinon. In the test file, I have imported the whole file

“UnhandledPromiseRejectionWarning” error even when catch is present

时间秒杀一切 提交于 2019-12-05 20:17:08
So I have this code, createExampleDir is not yet implemented, so the test fails: let chai = require('chai'); let expect = chai.expect; let homeDir = require('home-dir'); let lib = require('../lib.js'); chai.use(require('chai-fs')); let dir = homeDir('/example-dir'); describe('lib', () => describe('createExampleDir', () => it('should find ~/example-dir', () => lib.createExampleDir() .then(() => expect(dir).to.be.a.directory()) .catch(() => {throw Error('Who cares!');}) ) ) ); The problem is that I'm getting an UnhandledPromiseRejectionWarning error, the error "Who cares!" is throw, so the catch

How to test functions within Javascriptcore?

时间秒杀一切 提交于 2019-12-05 19:22:51
I have seen many examples where someone takes a JavaScript function and evaluates it by doing something like: JSContext *context = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]]; [context evaluateScript:@"var add = function(a, b) {return a + b;}"]; NSLog(@"%@", [context[@"add"] callWithArguments:@[@20, @30]]); //Output is 50 But how can we run test cases on the function? By calling arguments we can see the result (assuming it doesn't throw an exception). In the event it does throw an exception we can set an exception handler on the context prior to calling the code.

Hubot Unit Testing Not Receiving Response

杀马特。学长 韩版系。学妹 提交于 2019-12-05 19:18:03
I'm trying to set up a simple unit test for my hubot code and I am not receiving responses back. I have simplified this down to: test.coffee: Helper = require('hubot-test-helper') chai = require 'chai' expect = chai.expect helper = new Helper('../hubot-scripts/something.coffee') describe 'PING', -> beforeEach -> @room = helper.createRoom() afterEach -> @room.destroy it 'should PONG', -> @room.user.say 'alice', '@hubot PING' expect(@room.messages).to.eql [ ['alice', '@hubot PING'], ['hubot', 'PONG'] ] and something.coffee: module.exports = (robot) -> robot.response /PING$/i, (msg) -> msg.send

Mock a class method using mockery and sinon

☆樱花仙子☆ 提交于 2019-12-05 08:12:16
I'm learning to unit test using the node module mockery with sinon. Using only mockery and a plain class I'm able to inject a mock successfully. However I would like to inject a sinon stub instead of a plain class but I'm having a lot of troubles with this. The class I am trying to mock: function LdapAuth(options) {} // The function that I want to mock. LdapAuth.prototype.authenticate = function (username, password, callback) {} And here is the code I'm currently using in my beforeEach() function: beforeEach(function() { ldapAuthMock = sinon.stub(LdapAuth.prototype, "authenticate", function

Error: [$injector:unpr] Unknown provider: $stateProvider <- $state

北城以北 提交于 2019-12-04 23:35:09
Executing below unit test gives "Error: [$injector:unpr] Unknown provider: $stateProvider <- $state". I have attached the angular-ui-router.min.js in karma file. describe("Unit tests", function() { var $rootScope, $injector, $state; console.log("hello"); beforeEach(inject(function(_$rootScope_, _$state_, _$injector_, $templateCache) { console.log("hello1"); $rootScope = _$rootScope_; $injector = _$injector_; $state = _$state_; })); describe("states", function() { it("verify state configuration", function() { var config = $state.get("DRaaS"); console.log(config, "cc"); }); }); }); You haven't

Uncaught SyntaxError: Unexpected token {

梦想与她 提交于 2019-12-04 18:34:53
I am trying to write a chai test where all I do is just stream some audio and get a simple response back: {} , for some reason I'm getting this error Uncaught SyntaxError: Unexpected token { when ever I pipe my fs stream to req , if I remove the piping and I don't have that stream the test works fine. server code: router.post('/', function (clientRequest, clientResponse) { clientRequest.on('end', function () {//when done streaming audio console.log('im at the end>>>>>'); clientResponse.setHeader('Content-Type', 'application/json'); //I've tried removing that: same result clientResponse.json({}

Loading existing HTML file with JSDOM for frontend unit testing

帅比萌擦擦* 提交于 2019-12-04 17:22:03
I'm new to unit testing, and I'm aware my tests may not be valuable or following a specific best practice, but I'm focused on getting this working, which will allow me to test my frontend code using JSDOM. const { JSDOM } = require('jsdom'); const { describe, it, beforeEach } = require('mocha'); const { expect } = require('chai'); let checkboxes; const options = { contentType: 'text/html', }; describe('component.js', () => { beforeEach(() => { JSDOM.fromFile('/Users/johnsoct/Dropbox/Development/andybeverlyschool/dist/individual.html', options).then((dom) => { checkboxes = dom.window.document