DEV Community

Discussion on: Migrating AngularJS tests From Karma to Jest

Collapse
 
abdoulayektr profile image
Abdoulaye Traoré

Hey, thanks. I didn't include the test files because the tests worked out of the box without any change once the test runner was correctly configured plus the focuse for me was more on Jest than the tests themselves. I'll try to paste some examples below from our test base if that can help you.

Collapse
 
antoniogiroz profile image
Antonio Gil

Thanks!!

Thread Thread
 
abdoulayektr profile image
Abdoulaye Traoré

yo @Antonio, I hope this helps you, it's a simple example

import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
import * as angular from 'angular';
import otListService from 'ot/components/widget/listService';
import otResourceService from 'ot/components/resourceService';
chai.use(sinonChai);

describe('otListService', function () {

  let Test;
  let _otListService;
  let httpBackend;
  let _OT_LIST_ITEMS_PER_PAGE;
  let responseArray = [{
    id: 1,
    name: 'carapuce'
  }, {
    id: 2,
    name: 'salameche'
  }, {
    id: 3,
    name: 'bulbizare'
  }];

  beforeEach(function () {
    angular.mock.module(otListService, otResourceService);
    inject(function (otResource, otListService, $httpBackend, OT_LIST_ITEMS_PER_PAGE) {
      _otListService = otListService;
      httpBackend = $httpBackend;
      Test = otResource('test');
      _OT_LIST_ITEMS_PER_PAGE = OT_LIST_ITEMS_PER_PAGE;
    });

  });

  afterEach(function () {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
  });

  describe('query()', function () {

    it('should be able to request the resource without pagination', function (done) {
      httpBackend.expectGET('test?orderBy=name&reverse=false').respond(JSON.stringify({
        models: responseArray
      }), {
        'X-Count': responseArray.length
      });

      _otListService.query(Test, null, null, null, null, null, function (data, totalItemCount) {

        data.should.be.an.instanceof(Array);
        data.should.have.length(responseArray.length);
        expect(totalItemCount).to.be.equal(responseArray.length);
        done();
      });

      httpBackend.flush();
    });
Thread Thread
 
antoniogiroz profile image
Antonio Gil

Hi!

Thanks!! very useful!

Regards!