question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jasmine Matchers not working on Angular 7 and Karma

See original GitHub issue

Hi,

I am trying to make the Jasmine Matchers to work with Angular 7 and Karma, but I am getting this error: Uncaught TypeError: expect(...).toBeTrue is not a function on my spec: expect(result.loginSuccess).toBeTrue();.

I followed the configuration steps on the readme:

  • Installed the packages: jasmine-expect, karma-jasmine-matchers and @types/jasmine-expect.
  • Added the jasmine matchers to the Karma config:
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular', 'jasmine-matchers'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-jasmine-matchers')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};
  • Added the types to the tsconfig.spec.json file:
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "types": [
      "jasmine",
      "node",
      "jasmine-expect"
    ]
  },
  "files": [
    "test.ts",
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

But I still get an error. Could you help me?

Thanks, Komyg

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jecraigcommented, May 15, 2019

I had to add require at the top of my spec class to get this working.

`import ‘jasmine-expect’; // <----- import here import { SessionLogger } from ‘./session-logger’;

describe(‘SessionLogger’, () => { it(‘should have initial value’, () => { expect(SessionLogger.fetch()).toBeEmptyArray(); }); }); `

1reaction
komygcommented, Jan 21, 2019

Hi Jamie,

I think I got it. I added an import to jasmine-matcher in my tests.ts file and the matchers were recognized.

Here is my full file:

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
import 'jasmine-expect';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

Thanks for the help, Felipe

Read more comments on GitHub >

github_iconTop Results From Across the Web

jasmine matcher functions not loading in angularjs/karma unit ...
I'm testing a function that should pull objects from firebase, I've installed the jasmine-expect with karma-jasmine-matchers plugin.
Read more >
Unit Testing in Angular App Using Jasmine and Karma Part-2
In this blog, you will learn the remaining things about unit testing in Angular application and how to write unit tests for different ......
Read more >
Component testing scenarios - Angular
The first is a sanity test; it confirms that the stubbed UserService is called and working. The second parameter to the Jasmine matcher...
Read more >
Testing Angular with Jasmine and Karma (Part 1) - DigitalOcean
Probably not. The reason we test our code is to verify that it behaves as we expect it to. As a result of...
Read more >
Chapter 9. Unit-testing Angular applications
The basics of unit testing with the Jasmine framework; The main artifacts from the ... Running unit tests against web browsers with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found