Ngx-bootstrap: Error running with JEST for unit testing

Created on 13 Mar 2018  路  1Comment  路  Source: valor-software/ngx-bootstrap

Bug description

When runnig JEST unit tests in a project with Angular 5 and ngx-bootstrap Modal, the following error occurs:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { BsModalRef } from './bs-modal-ref.service'; 
                                                                                         ^^^^^^

    SyntaxError: Unexpected token export

Versions of ngx-bootstrap, Angular, and Bootstrap:

ngx-bootstrap: ^2.0.3

Angular: ^5.2.8

Bootstrap: ^4.0.0

jest: ^22.4.0

jest-preset-angular: ^5.2.1

Build system: Angular CLI, System.js, webpack, starter seed:

Angular CLI: 1.7.3
Node: 8.9.4
OS: win32 x64
Angular: 5.2.8
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.7.3
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.3
webpack: 3.11.0

Most helpful comment

You'll have to do the 3 things mentioned here.

https://github.com/thymikee/jest-preset-angular#unexpected-token-importexportother

Something like

"jest": {
"transform": {
"^.+\.(ts|html)$": "/node_modules/jest-preset-angular/preprocessor.js",
"^.+\.js$": "babel-jest"
},
"transformIgnorePatterns": [
"node_modules/(?!@ngrx|ngx-bootstrap|@progress)"
],
"preset": "jest-preset-angular",
"setupTestFrameworkScriptFile": "/src/setupJest.ts"
},

and
"allowJs": true
in tsconfig.json

>All comments

You'll have to do the 3 things mentioned here.

https://github.com/thymikee/jest-preset-angular#unexpected-token-importexportother

Something like

"jest": {
"transform": {
"^.+\.(ts|html)$": "/node_modules/jest-preset-angular/preprocessor.js",
"^.+\.js$": "babel-jest"
},
"transformIgnorePatterns": [
"node_modules/(?!@ngrx|ngx-bootstrap|@progress)"
],
"preset": "jest-preset-angular",
"setupTestFrameworkScriptFile": "/src/setupJest.ts"
},

and
"allowJs": true
in tsconfig.json

Was this page helpful?
0 / 5 - 0 ratings