Code Monkey home page Code Monkey logo

Comments (6)

allenhwkim avatar allenhwkim commented on September 15, 2024

Sorry for the late response.
Please provide your test .ts file and your command output.
I think you are missing something related to mock.

from ngentest.

t0n1n3 avatar t0n1n3 commented on September 15, 2024

Command - ngentest data.service.ts

data.service.ts

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

@Injectable()
export class DataService {

  constructor(private _router: Router) {
  }

}

data.service.spec.ts

import { async } from '@angular/core/testing';
import { Injectable } from '@angular/core';
import { Observable, of as observableOf, throwError } from 'rxjs';

import { DataService } from './menu.service';
import { Router } from '@angular/router';

@Injectable()
class MockRouter {
  navigate() {};
}

describe('DataService', () => {
  let service;

  beforeEach(() => {
    service = new DataService({});  // ERROR: Argument of type '{}' is not assignable to parameter of type 'Router'. Type '{}' is missing the following properties from type 'Router': rootComponentType, urlSerializer, rootContexts, location, and 48 more.
  });

});

from ngentest.

allenhwkim avatar allenhwkim commented on September 15, 2024

This is working for me. if this is from your Typescript checking, please make the type checking loose for your .spec files

$ cat ./data.service.ts
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

@Injectable()
export class DataService {

  constructor(private _router: Router) {
  }

  test() {
    this.foo = 1;
  }

}
$ cat ./data.service.spec.ts
import { async } from '@angular/core/testing';
import { Injectable } from '@angular/core';
import { Observable, of as observableOf, throwError } from 'rxjs';

import { DataService } from './data.service';
import { Router } from '@angular/router';

@Injectable()
class MockRouter {
  navigate = jest.fn();
}

describe('DataService', () => {
  let service;

  beforeEach(() => {
    service = new DataService({});
  });

  it('should run #test()', async () => {

    service.test();

  });

});
$ jest ./data.service.spec.ts
 PASS  ./data.service.spec.ts
  DataService
    ✓ should run #test() (2ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.382s, estimated 4s
Ran all test suites matching /.\/data.service.spec.ts/i.
jest-html-reporter >> Report generated (./reports/unit-test-report/index.html)

from ngentest.

t0n1n3 avatar t0n1n3 commented on September 15, 2024

Hi,

After adding the line // @ts-ignore above the error line seems to make the error go away and makes the test runs smoothly.

Also, can you test it in Karma once?

Thanks.

from ngentest.

allenhwkim avatar allenhwkim commented on September 15, 2024

This is tsconfig.json example to ignore spec files.

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@angular/*": [
        "./node_modules/@angular/*"
      ],
      "test/*": [
        "test/*"
      ]
    },
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "allowJs": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  },
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ],
  "preset": "ts-jest"
}

By adding exclude block, you can simply ignore all .spec typescript checking.

  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ],

Regarding Karma, I cannot provide test now.

from ngentest.

t0n1n3 avatar t0n1n3 commented on September 15, 2024

Thanks for the resolution, It is working fine.

from ngentest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.