allenhwkim / ngentest

Angular6+ Unit Test Generator For Components, Directive, Services, and Pipes
https://ngentest.github.io
MIT License
144 stars 60 forks source link

[QUESTION]: how to generate for Karma? #28

Closed scheung38 closed 4 years ago

scheung38 commented 4 years ago

in my project/ngentest.config.js:

const fs = require('fs');
const path = require('path');

module.exports = {
   framework: 'karma',
}

Now in project/app/src/my-component:

ngentest my-component.ts

but one of the lines it is still:

jest.fn().mockReturnValue({

and not

karma.fn().mockReturnValue({

?

I simply change from jest to karma in this case?

allenhwkim commented 4 years ago

ngentest respect the existing tests. If you want a new test to console, use -F option.

FYI, currently karma test uses spyOn......

spyOn(component.myForm, 'get').and.returnValue({...

$ gentest -h

Usage: index.js <tsFile> [options]

Options:
  --version         Show version number                                [boolean]
  -s, --spec        write the spec file along with source file         [boolean]
  -f, --force       It prints out a new test file, and it does not ask a
                    question when overwrite spec file                  [boolean]
  -F, --forcePrint  It prints out to console, and it does not ask a question
                                                                       [boolean]
  -m, --method      Show code only for this method                      [string]
  -v, --verbose     log verbose debug messages                         [boolean]
  --framework       test framework, jest or karma                       [string]
  -h                Show help                                          [boolean]

Examples:
  index.js my.component.ts  generate Angular unit test for my.component.ts

$ gentest examples/example2.component.ts --framework=karma -F ...

  it('should run #getAmount()', async () => {
    component.data = component.data || {};
    component.data.amount = 'amount';
    component.data.amount2 = 'amount2';
    component.selected = component.selected || {};
    component.selected.id = 'id';
    component.myForm = component.myForm || {};
    spyOn(component.myForm, 'get').and.returnValue({
      valid: {}
    });
    component.getAmount();
    // expect(component.myForm.get).toHaveBeenCalled();
  });