Reputation: 1317
I read a lot of posts about this problem but I didn't understand how to fix it. I tried many solutions and I have still an error.
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)
So I added this to my package.json :
"jest": {
"transformIgnorePatterns": [
"node_modules/(?!ngx-cookie-service)"
]
}
The problem is still there. Anybody can help ? Should I use "moduleNameMapper" to mock ngx-cookie-service ?
Thank you very much !
Upvotes: 4
Views: 2674
Reputation: 1
I've used the same approach transformIgnorePatterns: ['/node_modules/?!@angular']
in jest.config.js
file and worked.
Upvotes: 0
Reputation: 11
I just found a solution to this problem.
add a folder __mocks__
in root folder where the node_modules reside.
add ngx-cookie-service.js
file in __mocks__
folder and place the following code:
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
the tests will run fine.Upvotes: 1
Reputation: 388
I added
"transformIgnorePatterns": [
"node_modules/(?!@ngrx|ngx-cookie-service|ng-dynamic)"
],
to package.json and that fixed the issue.
When I remove ng-dynamic
from the regex the tests fail with the same execption. I have no idea why but they shouldn't, imo, since I don't have that module.
Upvotes: 1