我读了很多关于这个问题的帖子,但我不知道如何解决它。我尝试了许多解决方案,但仍然有一个错误。
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吗?
非常感谢!
发布于 2019-02-06 18:46:37
我刚刚找到了这个问题的解决方案。
__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,
value,
expires,
path,
domain,
secure,
sameSite
) {
this._cookieReg[name] = name + '=' + value;
}
delete(name, path, 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测试运行正常。发布于 2018-10-17 14:53:52
我添加了
"transformIgnorePatterns": [
"node_modules/(?!@ngrx|ngx-cookie-service|ng-dynamic)"
],添加到package.json,这样就解决了这个问题。
当我从正则表达式中删除ng-dynamic时,由于相同的执行,测试失败。我不知道为什么,但他们不应该,imo,因为我没有那个模块。
发布于 2022-01-06 12:33:14
我已经在jest.config.js文件中使用了相同的方法transformIgnorePatterns: ['/node_modules/?!@angular']并工作。
https://stackoverflow.com/questions/51731016
复制相似问题