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.

Adding this library to an Angular 2 project using Jasmine

See original GitHub issue

Hi all!

I think this is not an issue but can be an improvement in the doc.

I’m developing an Angular 2 RC5 app with Jasmine and I’m trying to load this library.

What I’ve done is:

In a unit-tests.html file I’ve included the following:


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>Ng App Unit Tests</title>
    <link rel="stylesheet" href="node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
    <!-- #1. Polyfills -->
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <!-- #2. Zone.js dependencies -->
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/zone.js/dist/async-test.js"></script>
    <script src="node_modules/zone.js/dist/fake-async-test.js"></script>
    <!-- #3. Add specs dependencies -->
    <script src="app/treeNode.spec.ts"></script>
    <script src="app/template.spec.ts"></script>
    <script src="app/services/tree.side.service.spec.ts"></script>
    <script src="app/services/tree.service.spec.ts"></script>
    <!-- #4. Add Jasmine dependencies -->
    <script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
    <script src="node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
    <script src="node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
    <script src="node_modules/jasmine-expect/dist/jasmine-matchers.js"></script>
[...]

And then, I’ve created a Spec file, like this:

describe('Service: TreeService', () => {

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                {provide: TreeService, useClass: TreeService},
                [...]
            ]
        })
    });

    it('get nodes',
        inject(
            [TreeService], fakeAsync((service: TreeService, backend: MockBackend) => {
                let res: TreeNode[];
                service.provider = '/test/api/treeNodes';
                service.getNodes();
                service.nodes.subscribe((response) => {
                    res = response as TreeNode[];
                });
                tick(500);
                expect(res).toBeArray();
                expect(res).toBeNonEmptyArray();
                expect(res.length).toBeGreaterThan(0);
            })));
});

But when I run the app, with npm test this error occurs:

app/services/tree.service.spec.ts(39,29): error TS2339: Property 'toBeArray' does not exist on type 'Matchers'.
app/services/tree.service.spec.ts(40,29): error TS2339: Property 'toBeNonEmptyArray' does not exist on type 'Matchers'.

So I don’t know how to import this matcher inside the Spec file.

Any help is appreciate 😉

Thanks!

[EDIT] As I mentioned, I believe this is not an issue, because I finally solve this problem. I’ve missed some important thing to do, considering I’m using TypeScript:

typings install dt~jasmine-expect --save-dev -DG

With this, a globalDevDependencies is added in file typings.json and this library is available to use.

I think that the documentation is great and very useful, but maybe it could be improve with a section explain how to use with TypeScript.

Thanks a lot!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aaronbroadcommented, Sep 29, 2016

Ah, the ones I found were more out of date (v0.2.1): https://github.com/DefinitelyTyped/DefinitelyTyped/blob/types-2.0/jasmine-matchers/index.d.ts

I have not published types to npm before, but best I can tell it involves adding a types property to package.json that includes the path to the declaration file, something like:

"types": "./jasmine-matchers.d.ts"

The file you referenced looks closer to the current API. I’ll submit a pull request reflecting this version and perhaps you could take a look and see which matchers are missing?

0reactions
JamieMasoncommented, Sep 29, 2016

Sounds good, thanks a lot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Components in Angular 2 with Jasmine - Semaphore CI
Dive back into Angular 2 and learn how to test components in a new tutorial in our ... The first source is from...
Read more >
Testing - Angular
The Angular CLI downloads and installs everything you need to test an Angular application with Jasmine testing framework. ... The ng test command...
Read more >
From Jasmine to Jest and Testing library in Angular Projects
Install and configure Jest​​ We will run the following command in our terminal to install jest library, Jest preset for angular, and jest...
Read more >
Testing an Angular Library, both the library components and ...
The library is very simple, it contains just one component component.ts with its tests in component.spec.ts . Together with the library I have ......
Read more >
Testing Angular 2 Services and Http with Jasmine
Learn how to write Angular 2 TestBed tests to mock HTTP backends for various RESTful CRUD methods. Github repo available. By Chariot ...
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