Angular error running Karma tests: HTML5 mode requires a <base> tag

后端 未结 3 1594
南笙
南笙 2020-12-06 04:29

I have a single-page Angular app with a Rails backend. I am using a tag in my index.html file, but when I run my front-end unit tests using Karma, I get this:<

相关标签:
3条回答
  • 2020-12-06 04:44

    You can disable the base tag check by doing the following:

    $locationProvider.html5Mode({
      enabled: true,
      requireBase: false
    });
    
    0 讨论(0)
  • 2020-12-06 04:50

    You can specify your base tag in your index.html <head> like that:

    <head>
      <base href="/">
      ...
    </head>
    

    Source: from angularjs documentation

    0 讨论(0)
  • 2020-12-06 04:55

    If you can't or do not want to set requireBase to false, the solution - as others pointed out - is to add <base href="/"> in the header of your primary HTML file. To do this, you will need to specify custom HTML files for Karma, which you can then edit.

    First find and copy node_modules/karma/static/context.html and node_modules/karma/static/debug.html to your unit test folder, and amend the headers of both.

    Then in your karma.conf.js add the following two lines:

    customContextFile: 'test/unit/context.html',
    customDebugFile:   'test/unit/debug.html',
    

    Of course your path may vary.

    0 讨论(0)
提交回复
热议问题