How to test chrome extensions?

前端 未结 6 1212
别那么骄傲
别那么骄傲 2020-12-07 07:03

Is there a good way to do this? I\'m writing an extension that interacts with a website as a content script and saves data using localstorage. Are there any tools, framework

6条回答
  •  忘掉有多难
    2020-12-07 07:32

    While sinon.js seems to work great, you can also just use plain Jasmine and mock the Chrome callbacks you need. Example:

    Mock

    chrome = {
      runtime: {
        onMessage : {
          addListener : function() {}
        }
      }
    }
    

    Test

    describe("JSGuardian", function() {
    
      describe("BlockCache", function() {
    
        beforeEach(function() {
          this.blockCache = new BlockCache();
        });
    
        it("should recognize added urls", function() {
          this.blockCache.add("http://some.url");
          expect(this.blockCache.allow("http://some.url")).toBe(false);
        });
    } // ... etc
    

    Just modify the default SpecRunner.html to run your code.

提交回复
热议问题