Jn_Lch
Jn_Lch

Reputation: 123

Karma + Jasmine + Angular2 + Webpack: coreTesting.setBaseTestProviders is not a function

Trying to set up some test system via this article. It's all more or less understandable, but I've got an error which I currently can't resolve.

My karma.entry.js:

require('es6-shim');
require('reflect-metadata');
require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/jasmine-patch');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
require('zone.js/dist/sync-test');


const browserTesting = require('@angular/platform-browser-dynamic/testing');
const coreTesting = require('@angular/core/testing');


coreTesting.setBaseTestProviders(
   browserTesting.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
   browserTesting.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
);

const context = require.context('..\\srs\\', true, /\.spec\.ts$/);
context.keys().forEach(context);
Error.stackTraceLimit = Infinity;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000;

Running "npm test" results in error:

Uncaught TypeError: coreTesting.setBaseTestProviders is not a function
at webpack:///karma/karma.entry.js:15:0 <- karma.entry.js:61

As far as I can guess, require('@angular/core/testing'); doesn't return an object I need. I've seen the similar topics here but so far nothing works. I understand, that somewhere the dependency is missing but can't pinpoint where exactly.

My karma.conf.js:

'use strict';

module.exports = (config) => {
config.set({
    autoWatch: true,
    browsers: ['Chrome'],
    files: [
        '../node_modules/es6-shim/es6-shim.min.js',
        'karma.entry.js'
    ],
    frameworks: ['jasmine'],
    logLevel: config.LOG_INFO,
    phantomJsLauncher: {
        exitOnResourceError: true
    },
    preprocessors: {
        'karma.entry.js': ['webpack', 'sourcemap']
    },
    reporters: ['dots'],
    singleRun: false,
    webpack: require('..\\webpack\\webpack.test'),
    webpackServer: {
        noInfo: true
    }
});
};

My package.json seems to have all necessary dependencies:

{
 ...
  "dependencies": {
    "@angular/common": "^2.2.4",
    "@angular/compiler": "^2.2.4",
    "@angular/core": "^2.2.4",
    "@angular/forms": "^2.2.4",
    "@angular/http": "^2.2.4",
    "@angular/platform-browser": "^2.2.4",
    "@angular/platform-browser-dynamic": "^2.2.4",
    "@angular/router": "^3.2.4",
    "es6-shim": "^0.35.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "^5.0.0-beta.12",
    "zone.js": "^0.6.12"
   },
  "devDependencies": {
    "@types/core-js": "^0.9.35",
    "html-webpack-plugin": "^2.24.1",
    "jasmine-core": "^2.5.2",
     "karma": "^1.3.0",
   "karma-chrome-launcher": "^2.0.0",
    "karma-jasmine": "^1.0.2",
    "karma-phantomjs-launcher": "^1.0.2",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^1.8.0",
    "phantomjs-prebuilt": "^2.1.13",
    "raw-loader": "^0.5.1",
    "ts-loader": "^1.2.2",
    "tslint": "^4.0.2",
    "tslint-loader": "^3.2.1",
    "typescript": "^2.0.10",
     "typings": "^2.0.0",
    "webpack": "^1.13.3",
    "webpack-dev-server": "^1.16.2"
  }
}

Could someone please kindly inform me where exactly I have misconfigured the whole thing?

Upvotes: 2

Views: 573

Answers (2)

Jn_Lch
Jn_Lch

Reputation: 123

Indeed, I have used a deprecated method.

Upvotes: 0

Mateusz Kocz
Mateusz Kocz

Reputation: 4602

The testing API changed multiple times. The current version of the testing entry file is available in the repository linked from this article.

Basically, what you should now do is to use the exposed TestBed class from the core/testing module, rather than directly calling the now removed setBaseTestProviders method.

...
coreTesting.TestBed.resetTestEnvironment();
coreTesting.TestBed.initTestEnvironment(
    browserTesting.BrowserDynamicTestingModule,
    browserTesting.platformBrowserDynamicTesting()
);

The TestBed is a class that deals with requirements of the testing environment. You can learn more about it in the Angular's documentation.

Upvotes: 2

Related Questions