我读了很多关于这个问题的帖子,但我不明白如何修复它.我尝试了很多解决方案,但仍然存在错误.
Details: /home/work/project/node_modules/ngx-cookie-service/index.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './cookie-service/cookie.service'; ^^^^^^ SyntaxError: Unexpected token export 15 | this.sessionStorage = sessionStorage; 16 | this.localStorage = localStorageService; > 17 | this.cookieService = cookieService; | ^ 18 | } 19 | 20 | public set(key,value,useSessionStorage = true) { at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17) at Object.<anonymous> (src/myApp/coreModule/services/storageLayer.service.ts:17:30) at Object.<anonymous> (src/myApp/itemmodule/components/category.component.ts:30:32)
所以我把它添加到我的package.json:
"jest": { "transformIgnorePatterns": [ "node_modules/(?!ngx-cookie-service)" ] }
问题仍然存在.有人可以帮忙吗?我应该使用“moduleNameMapper”来模拟ngx-cookie-service吗?
非常感谢你 !
解决方法
我刚刚找到了解决这个问题的方法.
>在node_modules所在的根文件夹中添加一个文件夹__mocks__.
>在__mocks__文件夹中添加ngx-cookie-service.js文件并放置以下代码:
class MockCookieService { constructor() { this._cookieReg = {}; } check(name) { return this._cookieReg[name] ? true : false; } get(name) { return this._cookieReg[name]; } getAll() { return this._cookieReg; } set( name,expires,path,domain,secure,sameSite ) { this._cookieReg[name] = name + '=' + value; } delete(name,domain) { delete this._cookieReg[name]; } deleteall(path,domain) { this._cookieReg = {}; } } module.exports = { CookieService: MockCookieService };
import { CookieService } from 'ngx-cookie-service'; jest.genMockFromModule('ngx-cookie-service'); Testbed.configureTestingModule({ declarations: [Your-Component],providers: [CookieService] })
>现在运行jest,测试运行正常.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。