Skip to content

chrome.storage.local.get should return a deep copy of objects #133

@carbureted

Description

@carbureted

Presently it's possible for test behavior and real code behavior to differ because changes to objects returned by chrome.storage.local.get can be "persistently" modified in functions that do not save the result to chrome.storage.

interface Thing {
  array: string[]
}

function saveThing(thing: Thing) {
  return new Promise((resolve) => {
    chrome.storage.local.set({ thing: thing }, () => {
      resolve(true)
    })
  })
}

function getThing(): Promise<Thing> {
  return new Promise((resolve) => {
    chrome.storage.local.get('thing', (data: { thing: Thing }) => {
      resolve(data.thing)
    })
  })
}

function doSomethingWithThing(thing: Thing) {
  thing.array = thing.array.map((string) => {
    return 'bar'
  })
}

describe('Thing should work', () => {
  it('thing works', async () => {
    await saveThing({ array: ['foo'] })
    let thing = await getThing()
    expect(thing.array[0]).toEqual('foo')

    doSomethingWithThing(thing)

    thing = await getThing()
    expect(thing.array[0]).toEqual('foo')
  })
})

Expected: "foo"
Received: "bar"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions