Lucas_Santos
Lucas_Santos

Reputation: 4740

Using Jest for unit test in angular 1.6

I'm trying to test my angular service that uses the Marvel API, and I was following this article, but I can't figure it out what I'm doing wrong.

Error

Failed to instantiate module marvel due to:{1}

My app.js

(() => {
  angular.module('marvel', [
    'ui.router',
    'ngAnimate',
    'ngAria',
    'ngMessages',
    'ngMaterial'
  ])
})()

My Test Spec code

require('../../node_modules/angular/angular.min.js')
require('../../node_modules/angular-mocks/angular-mocks.js');
require('../../src/app/app.js')
require('../../src/app/services/marvel.service.js')

describe('\nFail Cases', () => {
  beforeEach(angular.mock.module('marvel'))
  let _marvelservice
  beforeEach(inject((MarvelService) => {
    _marvelservice = MarvelService
  }));

  test('should return false when user do not put the id for details correctly', (done) => {
    _marvelservice.getDetail()
      .catch((err) => {
        expect(err.xhrStatus).toBe('error')
      })
  })
})

Marvel Service

(() => {
  angular.module('marvel')
    .factory('MarvelService', ($http, $q, Config) => {
      /**
       * Get 10 characters from Marvel API.
       * @return {Object} Doc with all character recovered.
       */
      function getCharacters () {
        return request('', 'GET', { ts: 1, limit: 10, apikey: 
       `${Config.MARVEL.PUBLIC_KEY}`, hash: `${Config.MARVEL.MD5}` })
      }

      /**
       * Get details from a characters by consulting the Marvel API.
       * @return {Object} Doc with detail character recovered.
       */
      function getDetail (id) {
        const urlAddress = `/${id}`
        return request(urlAddress, 'GET', { ts: 1, apikey: 
        `${Config.MARVEL.PUBLIC_KEY}`, hash: `${Config.MARVEL.MD5}` })
      }

      /**
       * Responsible for request.
       * @return {Object} Doc with the returned promise.
       */
      function request (path, method, querystring) {
         const options = {
          method,
          url: `${Config.MARVEL.URL}${path}`,
          params: querystring
      }

      return $http(options)
        .then(success => { return success.data }, (err) => {
          return err
        })
      }

      return {
        getCharacters,
        getDetail
      }
    })
})()

Upvotes: 1

Views: 1088

Answers (1)

Estus Flask
Estus Flask

Reputation: 222319

The problem is exactly as the error says. There should be marvel module, and it's not there.

angular.module('marvel') is module getter. It's expected that the module has been already defined with angular.module('marvel', []).

Upvotes: 1

Related Questions