Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug?] Why moduleFileExtensions: [json] causes transformation error (Unexpected token import)? #194

Closed
lacolaco opened this issue Sep 12, 2018 · 11 comments

Comments

@lacolaco
Copy link
Contributor

Here are weird behaviors happening in my environment.

At first, I tried to set up my Angular CLI project to use jest-preset-angular, but it didn't work.

    /Users/laco/Works/angular-jest-example/src/setupJest.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import 'jest-preset-angular';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token import

My src/setupJest.ts is only one line.

import 'jest-preset-angular';

And my jest.config.js is very simple. Other files are never modified from Angular CLI default.

module.exports = {
  preset: 'jest-preset-angular',
  setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts'
};

But when I added moduleFileExtensions to jest.config.js, it works well.

module.exports = {
  preset: 'jest-preset-angular',
  moduleFileExtensions: ['ts', 'js', 'html'],
  setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts'
};

Notable point is that moduleFileExtensions must not include json.

In jest-preset.json, moduleFileExtensions is defined as below.

"moduleFileExtensions": [
    "ts",
    "js",
    "html",
    "json"
  ],

As well as that, if I add json to my moduleFileExtensions, it doesn't work.

module.exports = {
  preset: 'jest-preset-angular',
  moduleFileExtensions: ['ts', 'js', 'html', 'json'], 
  setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts'
};

json causes a transformation error. It's very weird.

My question:

  • Why json causes a transformation error?
  • Is my workaround (moduleFileExtensions: ['ts', 'js', 'html']) bad solution?

Thanks.

@lacolaco
Copy link
Contributor Author

@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 13, 2018

cc @thymikee

@thymikee
Copy link
Owner

That's weird, I wouldn't expect this kind of error in this case. Do you have a repro?

@lacolaco
Copy link
Contributor Author

I'll make it. 👍

@lacolaco
Copy link
Contributor Author

I tried to create a repro, but I couldn't.
Currently, that error cannot be solved...

https://github.com/lacolaco/jest-preset-angular-issues194

  1. ng new
  2. yarn add jest jest-preset-angular
  3. Add jest.config.js and src/setupJest.ts

Then Unexpected token error happens.
So I followed https://github.com/thymikee/jest-preset-angular#unexpected-token-importexportother

  1. fix src/tsconfig.spec.json ( module:commonjs)

But it isn't solved... I gave up 😇

@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 21, 2018

I think something wrong when reading js config because this doesn't happen to json config in package.json

@lacolaco
Copy link
Contributor Author

@ahnpnl I've changed my tsconfig.spec.json to set module: commonjs as according to README.
In additional, I tried the following approachs, but these didn't work.

  • add allowJS . target: es5, basePath: '' as well as example
  • move configuration from jest.config.js to package.json
  "dependencies": {
    "@angular/animations": "^6.1.0",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",
    "@angular/platform-browser": "^6.1.0",
    "@angular/platform-browser-dynamic": "^6.1.0",
    "@angular/router": "^6.1.0",
    "core-js": "^2.5.4",
    "rxjs": "~6.2.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.8.0",
    "@angular/cli": "~6.2.3",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.1.0",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.3.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "jest": "^23.6.0",
    "jest-preset-angular": "^6.0.1",
    "karma": "~3.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~2.9.2"
  }

@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 21, 2018

your latest changes in your repo worked for me. I think you need to run yarn jest --clearCache first. Jest caches your configuration.

@lacolaco
Copy link
Contributor Author

@ahnpnl Ahhhh, it works well after clearing cache!! Thank you so much!!
And I confirmed the key point is module: commonjs as written in README. That instruction is totally correct.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 21, 2018

also when I move the config to js file, run yarn jest --clearCache and then yarn test it also works for me. I think first of all before you change tsconfig.spec.json, it failed with the error unexpected token import. After that jest already cached your configuration so when you change tsconfig.spec.json it didn't reflect to the new changes.

@lacolaco
Copy link
Contributor Author

You're right. I guess the first problem (about moduleFileExtensions) was caused the same reason...

At now, my situation is solved, so I close this issue. Thank you for your kind replies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants