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

transformIgnorePatterns and transform options are being ignored for third party library #85

Closed
edwinabt opened this issue Oct 3, 2018 · 9 comments
Labels

Comments

@edwinabt
Copy link

edwinabt commented Oct 3, 2018

Hi, this is a sample repo with the problem happening:

https://github.com/edwinabt/abt-jest-ng-app

Run the regular Angular steps to run an app:

  • npm install
  • ng serve
  • go to http://localhost:4200
  • inspect the console, you should see, Yes, Google API loaded, it means the third party library, ng-gapi, loaded fine:
    image

Now, the problem is with a library called ng-gapi that wraps the Google API SDK to be used in angular. When you run ng test it reports the test failed because of Jest encountered an unexpected token. I've read about this problem and tried the possible solutions, but, none are working for me.

image

Is this a bug? or I am doing something wrong?

@just-jeb
Copy link
Owner

just-jeb commented Oct 4, 2018

I'll take a look, but as far as I can tell it's either configuration issue or ts-jest issue. I remember myself facing the same issue with Angular 5 and ts-jest (which didn't have a builder for it) so most probably it's not a jest-builder issue.
Anyways, as I said, I'll take a look and try to help you with this.

@just-jeb
Copy link
Owner

just-jeb commented Oct 4, 2018

From a quick Googling I found the most probable candidate for the source of the problem.
From you screenshot I can see that the library you use is compiled to ES6 modules, while you're using commonjs modules.
This means that ES6 syntax is not recognized by jest and it has to be transpiled to commonjs.
Here is the similar issue in ts-jest.

@edwinabt
Copy link
Author

edwinabt commented Oct 4, 2018

@meltedspark thanks... will take a look at that.

@edwinabt edwinabt closed this as completed Oct 4, 2018
@jusefb
Copy link

jusefb commented Nov 12, 2018

I am having the same issue, I am using the ngx-cookie-service library and it does not work when using jest. I have tried following instructions mentioned in this issue for jest-preset-angular but it seems that the "transform" property is not extendable when using the jest angular-builder so the code never runs through the babel-jest transform. Any help would be appreciated

@just-jeb
Copy link
Owner

Could you create a repository with a minimal reproduction?

@jusefb
Copy link

jusefb commented Nov 12, 2018

Ok, I have read the ts-jest issue that you have referred to in more detail and managed to pin point the issue to the ts-jest setting. basically when I add the following setting to ts-jest:

    "ts-jest": {
       ...
      "allowSyntheticDefaultImports": true
    }

everything starts working as expected. So my final jest.config.js looks like this:

const esModules = ['ngx-cookie-service'].join('|');

module.exports = {
  globals: {
    "__TRANSFORM_HTML__": true,
    "ts-jest": {
      "tsConfigFile": `${__dirname}\\tsconfig.spec.json`,
      "allowSyntheticDefaultImports": true
    }
  },
  preset: "jest-preset-angular",
  testURL: "https://github.com/@angular-cli-builders",
  transformIgnorePatterns: [`<rootDir>/node_modules/(?!${esModules})`],

  "transform": {
    "^.+\\.js$": "babel-jest"
  }
};

hopefully this will be useful to anyone else who experiences the same issue

@just-jeb
Copy link
Owner

@jusefb That is great, thanks for sharing! I'm thinking of creating a section for frequent problems in the readme. Would you mind issuing a PR for that?

@jusefb
Copy link

jusefb commented Nov 12, 2018

Sure, never done it before :) will you need to create the section first?

@just-jeb
Copy link
Owner

You are welcome to create it yourself.

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

No branches or pull requests

3 participants