Angular2 tests and RESOURCE_CACHE_PROVIDER global

怎甘沉沦 提交于 2019-12-24 10:58:25

问题


I'm looking a way to set provider RESOURCE_CACHE_PROVIDER as ResourceLoader on initTest phase on karma-test-shim.src.js

We cannot modify single test because we use them also on dist folder where templateUrl is replace with template by gulp-inline-ng2-template

Here some tests we have already run without success:

Promise.all([
    System.import("@angular/core/testing"),
    System.import("@angular/platform-browser-dynamic"),
    System.import("@angular/platform-browser-dynamic/testing")
]).then(function ([testing, browserDynamic, testingBrowserDynamic]) {
       testing.TestBed.initTestEnvironment(
          [testingBrowserDynamic.BrowserDynamicTestingModule],
          testingBrowserDynamic.platformBrowserDynamicTesting()
        );

      // First approach (it doesn't work)
      testing.TestBed.overrideProvider(browserDynamic.RESOURCE_CACHE_PROVIDER);

      // Second approach (it doesn't work)
      testing.TestBed.configureCompiler({
        providers: [ 
            browserDynamic.RESOURCE_CACHE_PROVIDER
        ]
    })

回答1:


We have found a solution but not based on Angular provider.

We developed a simple karma preprocessor just for test as:

  preprocessors: {
            "**/*.component.js": ["generic"]
        },

Then preprocessor just uses gulp-inline-ng2-template parser

genericPreprocessor: {
            rules: [{
                process: function (content, file, done, log) {
                    // Prepare content for parser
                    file.contents = new Buffer(content);
                    // Every file has a parser
                    var parse = require('gulp-inline-ng2-template/parser')(file, { base: "packages/", useRelativePaths: false });
                    // Call real parse function
                    parse(function (err, contents) {
                        // Callback with content with template and style inline
                        done(contents);
                    });
                }
            }]
        },


来源:https://stackoverflow.com/questions/44539485/angular2-tests-and-resource-cache-provider-global

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!