为什么localStorage无法与打字稿一起使用?

时间:2020-01-01 21:28:15

标签: typescript

我一直在开发自己的使用localStorage的模块 我创建了一个名为LocalStorage的类,其构造函数为:

constructor() {
    this.storage = localStorage;
}

尝试使用mocha并在模块上运行测试时,会引发错误:

ReferenceError: localStorage is not defined
    at new LocalStorage (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\src\storage-managers\local-storage.ts:10:24)
    at Object.<anonymous> (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\test.ts:3:15)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Module.m._compile (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\ts-node\src\index.ts:536:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\ts-node\src\index.ts:539:12)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\mocha\lib\mocha.js:334:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\mocha\lib\mocha.js:331:14)
    at Mocha.run (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\mocha\lib\mocha.js:811:10)
    at Object.exports.singleRun (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\mocha\lib\cli\run-helpers.js:108:16)
    at exports.runMocha (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\mocha\lib\cli\run-helpers.js:142:13)
    at Object.exports.handler.argv [as handler] (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\mocha\lib\cli\run.js:292:3)    at Object.runCommand (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\yargs\lib\command.js:242:26)
    at Object.parseArgs [as _parseArgs] (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\yargs\yargs.js:1087:28)
    at Object.parse (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\yargs\yargs.js:566:25)
    at Object.exports.main (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\mocha\lib\cli\cli.js:68:6)
    at Object.<anonymous> (C:\Users\Dani-PC\Documents\Projects\wide-storage-manager\node_modules\mocha\bin\mocha:154:29)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! Test failed.  See above for more details.

我几个小时都找不到解决方法。

有人可以帮助我吗?

0 个答案:

没有答案