tlvince / eslint-plugin-jasmine

ESLint rules for Jasmine
https://www.npmjs.com/package/eslint-plugin-jasmine
MIT License
95 stars 58 forks source link

Rule `no-unsafe-spy` is failed when `jasmine.createSpy` is used in function which is defined outside but called inside before/after/it #111

Open Valdermeyder opened 7 years ago

Valdermeyder commented 7 years ago

Do you want to request a feature or report a bug? I suppose it is rather new feature than bug

What is the current behavior?

describe('suite', function () {
    beforeEach(function () {
        initSpies();
    });
});

function initSpies() {
    return {
        params: {},
        getData: jasmine.createSpy('getData'),
        getValidations: jasmine.createSpy('getValidations')
    };
}

For code above next warning will be shown warning Spy declared outside of before/after/it block jasmine/no-unsafe-spy

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior? Warning should not be shown if function initSpies is called only from before/after/it block

Please mention your node.js, eslint-plugin-jasmine and operating system version. node.js v7.2.1 eslint-plugin-jasmine v2.2.0

sebastianzillessen commented 6 years ago

Hello! I have the same issue:

describe("someTest", ()=>{
    it("test1", ()=>{
        createSpy();
    })
    it("test2", ()=>{
        createSpy();
    })
})
function createSpy(){
    return jasmine.createSpy('validate() spy')
}

Causes a

warning  Spy declared outside of before/after/it block  jasmine/no-unsafe-spy

Warning.

Kazhuu commented 5 years ago

I'm also faced the same problem when I tried to declare helper function which will return a spy. I'm ended up disabling this rule for now.

GreenGremlin commented 5 years ago

Same here. Any ideas on a possible fix for this rule?

Kazhuu commented 5 years ago

I just disabled because couldn't figure out any other way to fix this issue at the moment.

guilhermetod commented 4 years ago

Edit: just found out that apparently you can retrieve the beforeEach function itself.

const initSpies = () => beforeEach(() => ({
  params: {},
  getData: jasmine.createSpy('getData'),
  getValidations: jasmine.createSpy('getValidations'),
}));

describe('suite', () => {
  let params;
  let getData;
  let getValidations;

  initSpies();
});