Skip to content

feat(jest) update Jest builder to use jest-preset-angular v7#246

Merged
just-jeb merged 17 commits into
just-jeb:masterfrom
wesleygrimes:feature/remove-deprecated-config-option-setup-ts
Mar 21, 2019
Merged

feat(jest) update Jest builder to use jest-preset-angular v7#246
just-jeb merged 17 commits into
just-jeb:masterfrom
wesleygrimes:feature/remove-deprecated-config-option-setup-ts

Conversation

@wesleygrimes

Copy link
Copy Markdown
Contributor

This PR replaces the deprecated setupTestFrameworkScriptFile with the new setupFilesAfterEnv option. I also updated the schema.json with the new option and details per the Jest documentation.

This closes #239 and potentially #193

This is my first PR on your project, so feel free to let me know if things should be done differently, or if I should follow a different naming convention.

@wesleygrimes

Copy link
Copy Markdown
Contributor Author

I see the build failed, but I'm not sure it's related to this code.

Here's the error from Travis:

Failed: "Zone is needed for the async() test helper but could not be found. Please make sure that your environment includes zone.js/dist/zone.js"

@just-jeb

just-jeb commented Feb 19, 2019

Copy link
Copy Markdown
Owner

First of all thanks for your contribution! The error is related to your change.
The failing part is the e2e test for this builder. It fails because the version of Jest it uses won't recognize setupFilesAfterEnv option. The fix for this is to change the Jest version in the corresponding example application.
Another thing to consider here is that the builder uses Jest as peerDependency, therefore it should support both options (old and new) depending on the installed version of Jest in host application.
It would be also cool to add an e2e test that actually tests that both user-specified and builder-defined setup files are executed.

@wesleygrimes

Copy link
Copy Markdown
Contributor Author

First of all thanks for your contribution! The error is related to your change.
The failing part is the e2e test for this builder. It fails because the version of Jest it uses won't recognize setupFilesAfterEnv option. The fix for this is to change the Jest version in the corresponding example application.
Another thing to consider here is that the builder uses Jest as peerDependency, therefore it should support both options (old and new) depending on the installed version of Jest in host application.
It would be also cool to add an e2e test that actually tests that both user-specified and builder-defined setup files are executed.

Thanks, I will take a look at the e2e test.

@wesleygrimes

Copy link
Copy Markdown
Contributor Author

The node 10 build failed, but looks like due to timeout. Can you confirm? The node 8 build did succeed.

@just-jeb

just-jeb commented Feb 20, 2019

Copy link
Copy Markdown
Owner

I restarted the node 10 build. Looks like timeout error indeed. However even if it passes I can't merge this PR yet. As I mentioned, both versions shall be supported and the correct one should be applied depending on host Jest version.
Jest team are deprecating it gradually (showing a deprecation warning), I think we should do the same.

@wesleygrimes

Copy link
Copy Markdown
Contributor Author

I restarted the node 10 build. Looks like timeout error indeed. However even if it passes I can't merge this PR yet. As I mentioned, both versions shall be supported and the correct one should be applied depending on host Jest version.
Jest team are deprecating it gradually (showing a deprecation warning), I think we should do the same.

I'm not exactly sure how to accomplish checking the installed Jest version, but I will dig in and let you know what I find!

Thanks.

@just-jeb

just-jeb commented Mar 11, 2019

Copy link
Copy Markdown
Owner

@wesleygrimes Gave it a second thought. Seems that it's not possible to support both versions of Jest in a single version of the builder, because the builder is tied to a specific version of jest-preset-angular which, in turn, works with a specific version of Jest.
Btw, I've just noticed that in this PR you've changed the version of Jest for the builders project, instead of changing the versions of preset in Jest builder package.
Check out this issue for the proper changes.

@wesleygrimes

wesleygrimes commented Mar 11, 2019 via email

Copy link
Copy Markdown
Contributor Author

@wesleygrimes wesleygrimes changed the title feat(jest) replace deprecated setupTestFrameworkScriptFile feat(jest) update Jest builder to use jest-preset-angular v7 Mar 13, 2019
@wesleygrimes

Copy link
Copy Markdown
Contributor Author

@enten @meltedspark

I have incorporated @enten changes from #266, but it is now failing in the mutliple-app and single-app tests. I have to step away for a while, but welcome any feedback and assistance in getting these to pass.

Thanks!

@enten

enten commented Mar 13, 2019

Copy link
Copy Markdown

@wesleygrimes
I tried to investigate why multiple-app and single-app tests failed.

Unfortunately, I don't found how to fix them.

I didn't succeed to compile AppComponent during test because its template html file can't be load. I encountered next error: Failed: "Failed to load app.component.html".

I wondered if zone-patch works well because beforeEach function doesn't seem to succeed. Eventually due to a silent error (about template html which can't be load).

We removed some option about html but to remain compatible with latest jest-preset-angular version.

@wesleygrimes

wesleygrimes commented Mar 13, 2019 via email

Copy link
Copy Markdown
Contributor Author

@just-jeb

just-jeb commented Mar 14, 2019

Copy link
Copy Markdown
Owner

The projects use lock files (yarn lock), so it's not enough to change the dependency version in package.json. In order to apply it correctly you have to run yarn add [-D] dep@dep-version.
Also, I saw that you removed completely the transform property. Why is that?

@wesleygrimes

wesleygrimes commented Mar 14, 2019 via email

Copy link
Copy Markdown
Contributor Author

@just-jeb

Copy link
Copy Markdown
Owner

Is there a contributing readme somewhere? If not maybe we can put one together and put down some notes on how to pull down the repo and test, etc..

That's a great idea, definitely need to have one!

@wesleygrimes

Copy link
Copy Markdown
Contributor Author

The projects use lock files (yarn lock), so it's not enough to change the dependency version in package.json. In order to apply it correctly you have to run yarn add [-D] dep@dep-version.
Also, I saw that you removed completely the transform property. Why is that?

@enten can you speak to why we removed the transform property? Was this a compatibility issue with jest-preset-angular?

@enten

enten commented Mar 14, 2019

Copy link
Copy Markdown

@wesleygrimes @meltedspark
It's important to note that jest-preset-angular/preprocessor.js doesn't exist anymore (since jest-preset-angular v7.0.0-alpha.0).

Previously we override transform to set absolute path to the preprocessor. Now jest-preset-angular uses ts-node directly for ^.+\\.(ts|js|html)$ files.

According to jest-preset-angular v7.0.1 migration guide:

If transform was overridden, remove the entry pointing at preprocessor.js and add "^.+\\.(ts|js|html)$": "ts-jest" to the transform-object.

Initially, I thought that we don't need to override transform property. So we removed the transform property.

Finally, after that @meltedspark reminded me the purpose of Jest builder, I think that we need to override transform property to ensure that a jest custom config doesn't forget to declare "^.+\\.(ts|js|html)$": "ts-jest" again (in case where transform property is overridden).

--- a/packages/jest/src/default-config.resolver.ts
+++ b/packages/jest/src/default-config.resolver.ts
@@ -12,6 +12,9 @@ export class DefaultConfigResolver {
   resolveGlobal(workspaceRoot: Path): any {
     const jestPresetFullPath = join(workspaceRoot, jestPresetRootPath);
     return {...defaultConfig,
+      transform: {
+        '^.+\\.(ts|js|html)$': 'ts-jest'
+      },
       snapshotSerializers: [
         getSystemPath(join(jestPresetFullPath, snapshotSerializer)),
         getSystemPath(join(jestPresetFullPath, htmlCommentSerializer))

--

On another side, I wonder if our issue may is related to the preprocessor. Because it was responsive to fix templateUrl and styleUrls properties before process source with ts-node:

Now ts-node stringifyContentPathRegex is used to export html content and ts-node astTransformers is used to declare an AST Transformer which is responsive to fix templateUrl and styleUrls properties.

Conclusion: we need to investigate to ensure that the ast transformer works as expected.

@enten

enten commented Mar 14, 2019

Copy link
Copy Markdown

@wesleygrimes @meltedspark
My apologies: I forget to adjust global property when its value was overriden (according to the jest-preset-angular migration guide).

Sadly, that doesn't fix issue Failed to load app.component.html. I think that the ast transformer used to fix templateUrl and styleUrls properties in sources isn't called.

Even with add moduleFileExtensions: ['ts', 'html', 'js', 'json'] to jest config: tests still failed.

@wesleygrimes
Can you apply changes below and try to run multiple-apps and single-app tests?

--- a/packages/jest/src/default-config.resolver.ts
+++ b/packages/jest/src/default-config.resolver.ts
@@ -12,6 +12,9 @@ export class DefaultConfigResolver {
   resolveGlobal(workspaceRoot: Path): any {
     const jestPresetFullPath = join(workspaceRoot, jestPresetRootPath);
     return {...defaultConfig,
+      transform: {
+        '^.+\\.(ts|js|html)$': 'ts-jest'
+      },
       snapshotSerializers: [
         getSystemPath(join(jestPresetFullPath, snapshotSerializer)),
         getSystemPath(join(jestPresetFullPath, htmlCommentSerializer))
@@ -23,7 +26,10 @@ export class DefaultConfigResolver {
     return {
       globals: {
         'ts-jest': {
-          tsConfig: getSystemPath(join(projectRoot, tsConfigName))
+          tsConfig: getSystemPath(join(projectRoot, tsConfigName)),
+          stringifyContentPathRegex: '\\.html$',
+          astTransformer: [ require.resolve('jest-preset-angular/InlineHtmlStripStylesTransformer') ]
         }
       },
       testMatch: [

@just-jeb

just-jeb commented Mar 16, 2019

Copy link
Copy Markdown
Owner

Hi guys. Thanks for your efforts. I almost forgot, but there actually is a version of builder that incorporates the necessary changes (it existed when the preset was alpha).
It is available in this branch with all the necessary changes. Should have brought it up at the very beginning, but completely forgot about it.
I think it should solve you the mystery.

@just-jeb

Copy link
Copy Markdown
Owner

Related issue: #182

@just-jeb

just-jeb commented Mar 16, 2019

Copy link
Copy Markdown
Owner

I created a pull request for you to highlight the changes that should be made.
Most of it already exists here so it's just adding the missing parts.

@enten

enten commented Mar 17, 2019

Copy link
Copy Markdown

@wesleygrimes @meltedspark
Great! Thanks for fixing the issue sirs.

May we can close this PR (#246) and merge PR #269?

@just-jeb

Copy link
Copy Markdown
Owner

I'd rather merge this PR and delete the other for a few reasons:

  • Keep the work you've done
  • The other PR is created from a branch that wasn't meant to be merged to master but released as an alpha (it also depends on alpha version of the preset).
    On the other hand this PR is supposed to be more mature and handle all the stuff like updating the tests dependencies etc.

I created the other PR for a reference to make things in this PR work.
So you guys can make the necessary changes and we'll merge this PR. Or I'll make the changes when I have time.

@wesleygrimes

wesleygrimes commented Mar 18, 2019 via email

Copy link
Copy Markdown
Contributor Author

@wesleygrimes

Copy link
Copy Markdown
Contributor Author

OK best I could tell from the PR #269 the only thing needing changed was updating packages\jest\src\jest-config\default-config.ts to add the globals property. Is that correct? If so, I have pushed a new commit to reflect that.

@wesleygrimes

Copy link
Copy Markdown
Contributor Author

@meltedspark looks like the build passed this time. Want to have a look and see if we can get this merged?

@just-jeb

Copy link
Copy Markdown
Owner

Looks like we're almost there, the only thing that is missing (according to this comment) is adding

 transform: {
       '^.+\\.(ts|js|html)$': 'ts-jest'
 },

to the config. Since it doesn't involve any path calculation it can be added right to the default config (packages\jest\src\jest-config\default-config.ts).

Also, looks like packages/jest/yarn.lock is not updated (it should be). It shouldn't matter for the final version as yarn.lock is not is not published, but for the local development it is better to be in sync.

@wesleygrimes

Copy link
Copy Markdown
Contributor Author

Looks like we're almost there, the only thing that is missing (according to this comment) is adding

 transform: {
       '^.+\\.(ts|js|html)$': 'ts-jest'
 },

to the config. Since it doesn't involve any path calculation it can be added right to the default config (packages\jest\src\jest-config\default-config.ts).

Also, looks like packages/jest/yarn.lock is not updated (it should be). It shouldn't matter for the final version as yarn.lock is not is not published, but for the local development it is better to be in sync.

  • Pushed a commit adding the transform property
  • Regarding the yarn.lock file, this should have been updated previously when I updated the package.json to jest-preset-angular 7.0.0 ? I normally just use npm so I am not as familiar with updating yarn files. Can you help me with next steps on that?

Thanks,
Wes

@just-jeb

just-jeb commented Mar 20, 2019 via email

Copy link
Copy Markdown
Owner

@wesleygrimes

Copy link
Copy Markdown
Contributor Author

Sure, in order to update "yarn.lock" you have to run "yarn add -D jest-preset-angular" (D for dev).

On Wed, Mar 20, 2019, 17:30 Wes Grimes @.***> wrote: Looks like we're almost there, the only thing that is missing (according to this comment <#246 (comment)>) is adding transform: { '^.+\.(ts|js|html)$': 'ts-jest' }, to the config. Since it doesn't involve any path calculation it can be added right to the default config ( packages\jest\src\jest-config\default-config.ts). Also, looks like packages/jest/yarn.lock is not updated (it should be). It shouldn't matter for the final version as yarn.lock is not is not published, but for the local development it is better to be in sync. - Pushed a commit adding the transform property - Regarding the yarn.lock file, this should have been updated previously when I updated the package.json to jest-preset-angular 7.0.0 ? I normally just use npm so I am not as familiar with updating yarn files. Can you help me with next steps on that? Thanks, Wes — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#246 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AJXjb1hsG5pVK6_rFyubNeskZtNuw8Piks5vYlQegaJpZM4bCCHo .

Ok, all set, it did update jest-preset-angular to 7.0.1. Hopefully that's ok.

@just-jeb
just-jeb merged commit 15d62aa into just-jeb:master Mar 21, 2019
@just-jeb

Copy link
Copy Markdown
Owner

@all-contributors please add @wesleygrimes for code

@allcontributors

Copy link
Copy Markdown
Contributor

@just-jeb

I've put up a pull request to add @wesleygrimes! 🎉

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

Successfully merging this pull request may close these issues.

Jest 'setupTestFrameworkScriptFile' Deprecation Warning

3 participants