coderman
coderman

Reputation: 1514

TypeError: should is not a function

I have recently upgraded to Angular 6 and all the tests that ran fine fail with the following error

TypeError: should is not a function

I am using Angular-Cli 6 and my best guess is this is because appropriate typings aren't available for 'should' (npm-package: https://www.npmjs.com/package/should )

I have the following two lines added to every file that uses the should function.

import should = require('should');
const persist = should; // need to use should library for tests to run https://stackoverflow.com/a/25563303/1373856

Also, I am using "typings": "2.1.1" Anyone faced a similar problem?

Upvotes: 0

Views: 572

Answers (1)

user4676340
user4676340

Reputation:

That's not how you include a dependency into your tests (at least, that's not how I learnt it).

Open karma.conf.js. Under the plugins export, add your dependency :

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('should'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      ...

Now, in your test files, you just have to declare a variable as a reference to your global function :

declare var should: any;

If you have typings, you can also type it to have intellisense in your IDE.

Upvotes: 0

Related Questions