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

Add back process method for CJS support #71

Merged
merged 20 commits into from
Sep 11, 2021
Merged

Conversation

benmccann
Copy link
Collaborator

Closes #66 #65 #63

It turns out that processAsync is not supported in CJS mode, so we still need the old process method

@sebastianrothe
Copy link
Collaborator

sebastianrothe commented Sep 5, 2021

Can you find some reference in the docs from Jest that this is really the case?

I am not sure about this, because I think I remember having seen asynchronous transformer and CJS (vue-jest or something).

@sebastianrothe
Copy link
Collaborator

sebastianrothe commented Sep 5, 2021

I think, ESM support in Jest is experiment (according to docs and because of all the flags involved), which means it is still written in CJS. Now async transformers were introduced in v27. To me this means Async support in CJS should be superior.

I think we have to look for the root of the problem, once we are sure the transpilation worked.

@mszkb
Copy link

mszkb commented Sep 5, 2021

@benmccann pls add import { execSync } from 'child_process' in transformer.js file.

Another thing: For some reason I need to add the jsdom environment in package.json to run my tests, like:
"scripts": { "test": "jest --env=jsdom" }

In svelte-jester 1.8.2 this was not necessary, do you know why is that?

@benmccann
Copy link
Collaborator Author

Can you find some reference in the docs from Jest that this is really the case?

Unfortunately the docs for processAsync are quite lacking (there were none until I added some and I didn't know about that restriction, so I didn't mention it), but here's a reference for it: jestjs/jest#11458 (comment)

Another thing: For some reason I need to add the jsdom environment in package.json to run my tests, like:
"scripts": { "test": "jest --env=jsdom" }
In svelte-jester 1.8.2 this was not necessary, do you know why is that?

That's not related to svelte-jester. It's because the default environment changed to Node in Jest 27

@benmccann
Copy link
Collaborator Author

pls add import { execSync } from 'child_process' in transformer.js file.

done. thanks for catching that! I'd tested this, but I guess not with the preprocess option, so I missed that

@sebastianrothe
Copy link
Collaborator

sebastianrothe commented Sep 5, 2021

Unfortunately the docs for processAsync are quite lacking (there were none until I added some and I didn't know about that restriction, so I didn't mention it), but here's a reference for it: facebook/jest#11458 (comment)

Hm, this refers only to the require being synchronously. But this is just the module resolution, not the execution of the function. If this is true, then no one using a CJS library would be able to use an async function. Which I don't think is true.

I will have a look at the code from jest calling the transformer now.

@sebastianrothe
Copy link
Collaborator

sebastianrothe commented Sep 5, 2021

@sebastianrothe
Copy link
Collaborator

Hm, still unsure. I am preparing a test case to validate this.

Jest references async transformers a lot with ESM:

@mszkb
Copy link

mszkb commented Sep 5, 2021

pls add import { execSync } from 'child_process' in transformer.js file.

done. thanks for catching that! I'd tested this, but I guess not with the preprocess option, so I missed that

Thanks, tested and new changes on the branch are working

@benmccann
Copy link
Collaborator Author

@sebastianrothe I don't know the Jest internals well enough to have any idea of whether there might be a way to get processAsync to run in CJS mode.

The way the code is being called without this PR is:

    at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:584:7)
    at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:759:40)
    at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:816:19)
    at Runtime.transformFile (node_modules/jest-runtime/build/index.js:1463:47)
    at Runtime._execModule (node_modules/jest-runtime/build/index.js:1380:34)
    at Runtime._loadModule (node_modules/jest-runtime/build/index.js:1034:12)
    at Runtime.requireModule (node_modules/jest-runtime/build/index.js:866:12)
    at jestAdapter (node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:13)
    at runTestInternal (node_modules/jest-runner/build/runTest.js:389:16)
    at runTest (node_modules/jest-runner/build/runTest.js:481:34)

(line number might be off by a line or two as I'm running a locally-modified version)

There are async versions of many of these methods. I.e. transformSourceAsync, _transformAndBuildScriptAsync, transformAsync, transformFileAsync. But for some reason those aren't being called.

Maybe you can file an issue to ask in the Jest issue tracker or something. I'm afraid I don't have any insight other than seeing the comment where he said "async transform is only supported when using ESM"

@sebastianrothe
Copy link
Collaborator

sebastianrothe commented Sep 6, 2021

Maybe you can file an issue to ask in the Jest issue tracker or something. I'm afraid I don't have any insight other than seeing the comment where he said "async transform is only supported when using ESM"

I did ask him in the Jest issue (jestjs/jest#11458 (comment))

Nonetheless, I do have a test which consumes the CJS build and it is passing. Let my try to create a new Svelte project and try it there as well.

@mihar-22
Copy link
Collaborator

mihar-22 commented Sep 8, 2021

At this point both you @benmccann and @sebastianrothe have a better understanding of what's happening. I'm completely hands off so feel free to merge and I'll release as PR's go through. If you need any permissions let me know. We could even automate release with GitHub CI. Happy to set it up if needed.

@sebastianrothe
Copy link
Collaborator

jestjs/jest#11458 (comment) means, we need to bring the old code back for the CJS version.

I would like to bring back the tests for the syncTransformer as well. I will try to add those commits tomorrow.

@sebastianrothe
Copy link
Collaborator

I'm having some problems with the typescript test. We should really use the Github Actions running our tests on every PR.

@benmccann
Copy link
Collaborator Author

We could possibly merge this PR as is (all tests are passing with it) to get people unbroken and then handle adding the tests in a new PR. I agree it'd be nice to run the tests on the CI

@sebastianrothe
Copy link
Collaborator

I'm having some problems with the typescript test. We should really use the Github Actions running our tests on every PR.

Found it.

@sebastianrothe
Copy link
Collaborator

We could possibly merge this PR as is (all tests are passing with it) to get people unbroken and then handle adding the tests in a new PR. I agree it'd be nice to run the tests on the CI

Well, the tests run only for the async build. So I think it is important to have them pass for the sync build as well.

I will have some more time for this in the evening (in 3 hours).

@sebastianrothe
Copy link
Collaborator

We could possibly merge this PR as is (all tests are passing with it) to get people unbroken and then handle adding the tests in a new PR. I agree it'd be nice to run the tests on the CI

I added my commits to yours. Can you please have a look: https://github.com/mihar-22/svelte-jester/pull/71/files/4c72f25507b1fc5c457ef2da80bd62524990cfa5..75c83c690de2156f37fc577912c81fc05d2937df

  • removed esbuild, because with Jest supporting ESM it is not needed anymore for the tests
  • svelte-preprocess > 4.7.4 gave me errors on the test with Typescript -> needs more investigation, because they changed the typescript support
  • for cjs, we can't export const process because this variable is already taken by node and we need it to pass env-vars to the child process

@@ -6,8 +6,7 @@
"module": "dist/transformer.mjs",
"exports": {
"import": "./dist/transformer.mjs",
"require": "./dist/transformer.cjs",
"./package.json": "./package.json"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some plugins like rollup-plugin-svelte will log a warning if package.json is not exported, so I added it back. I updated the default export, so if you were having any issue hopefully that will solve it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, vscode gave me an error on this line.

package.json Outdated
"release": "npm run validate && standard-version"
},
"peerDependencies": {
"jest": ">= 27",
"svelte": ">= 3"
"svelte": ">= 3",
"svelte-preprocess": "<=4.7.4"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will force a dependency on svelte-preprocess even for projects written purely in JS that don't need to do any preprocessing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sent a PR to svelte-core that allowed me to track down this issue: sveltejs/svelte#6722

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will force a dependency on svelte-preprocess even for projects written purely in JS that don't need to do any preprocessing

Ups, you are right.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for the svelte-preprocess issue is here: sveltejs/svelte-preprocess#407. We can unpin svelte-preprocess in this project after that's committed

@benmccann benmccann merged commit fe188c4 into svelteness:master Sep 11, 2021
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.

Support components mocks
4 participants