Skip to content

Commit

Permalink
fix: [#1347] LocalStorage.setItem non-string value should to be string (
Browse files Browse the repository at this point in the history
  • Loading branch information
dr2009 committed Mar 23, 2024
1 parent b5af02b commit 44dd9ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/happy-dom/src/storage/Storage.ts
Expand Up @@ -31,7 +31,7 @@ export default class Storage {
* @param item Item.
*/
public setItem(name: string, item: string): void {
this[name] = item;
this[name] = String(item);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions packages/happy-dom/test/storage/Storage.test.ts
Expand Up @@ -51,6 +51,16 @@ describe('Storage', () => {
expect(storage['key2']).toBe('value2');
storage['key1'] = 'value3';
expect(storage.getItem('key1')).toBe('value3');

// @ts-expect-error
storage.setItem('key1', 1);
expect(storage.getItem('key1')).toBe('1');
expect(storage['key1']).toBe('1');

// @ts-expect-error
storage.setItem('key1', {});
expect(storage.getItem('key1')).toBe('[object Object]');
expect(storage['key1']).toBe('[object Object]');
});
});

Expand Down

0 comments on commit 44dd9ce

Please sign in to comment.