Skip to content

Latest commit

 

History

History
52 lines (40 loc) · 1.22 KB

no-unsafe-spy.md

File metadata and controls

52 lines (40 loc) · 1.22 KB

Enforce spies to be defined in before/after/it blocks (no-unsafe-spy).

Make sure named spies are declared only in before/after/it jasmine blocks. Spies created in global scope or directly in define blocks don't get automatically reset/cleaned by the jasmine teardown process, making it possible to get false positives when using toHaveBeenCalled() / toHaveBeenCalledWith().

This rule checks for the following methods: spyOn, jasmine.createSpy(), jasmine.createSpyObj().

Rule details

The following are considered warnings:

// myFile.js
var mySharedSpy = jasmine.createSpy()
spyOn(someObj, "someMethod")

describe(function () {
  var mySharedSpy = jasmine.createSpy()
  spyOn(someObj, "someMethod")
})

The following patterns are not warnings:

beforeEach(function () {
  var mySpy = jasmine.createSpy()
  spyOn(someObj, "someMethod")
})

beforeAll(function () {
  var mySpy = jasmine.createSpy()
  spyOn(someObj, "someMethod")
})

afterEach(function () {
  var mySpy = jasmine.createSpy()
  spyOn(someObj, "someMethod")
})

afterAll(function () {
  var mySpy = jasmine.createSpy()
  spyOn(someObj, "someMethod")
})

it(function () {
  var mySpy = jasmine.createSpy()
  spyOn(someObj, "someMethod")
})