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

Clarify use #24

Merged
merged 4 commits into from May 2, 2020
Merged

Clarify use #24

merged 4 commits into from May 2, 2020

Conversation

adam-lynch
Copy link
Contributor

Related to #5

@novemberborn
Copy link
Member

Can I ask about your project which led you to get stuck? Is it that you have "type": "module" in the package.json, or did the compile code include ESM syntax?

The former situation @ava/typescript errors about (IIRC!). But with the latter one I imagine you get syntax errors?

I think we should move this paragraph to the Enabling TypeScript support section. But rather than saying "use require" we should suggest you compile down to CommonJS. What do you think?

@adam-lynch
Copy link
Contributor Author

I don't think either option for your question is what I found. Maybe I had a misunderstanding. Let me explain.

My project is an Electron (main/Node-side) runtime library, so basically a Node module. It was already written in TypeScript using imports & exports, then I added some tests. I started adding tests, ESLint, and Prettier all in one go and I spent more time than I'd like to realise this package doesn't like imports. I just wanted to point it out in the README to save someone else time. I wrote the tests in JS for the time being.

Looking at the compiled output, I see module.exports = 🤔 . I don't have type: module in my package.json either. The error I was seeing was some kind of SyntaxError like import wasn't understood/expected.

I had configured ava + babel. I remember that I had two identical test files, one TS, one JS, and the JS ran fine.

Does this make sense?

@novemberborn
Copy link
Member

The error I was seeing was some kind of SyntaxError like import wasn't understood/expected.

Yes, which suggests that some code somewhere was not compiled down to CJS. The question is, was that your code, or a dependency?

I'm happy to clarify in the README that this only works if the module compiler option is set to CommonJS.

@adam-lynch
Copy link
Contributor Author

I think it was the import at the top of my test. I'll try to reproduce the situation again over the next couple of days and I get back to you

@adam-lynch
Copy link
Contributor Author

Hey @novemberborn, check this simplified version of my project: https://github.com/adam-lynch/ava-typescript-import-issue. What am I missing? As far as I can tell everything is compiled down to CJS.

This paragraph in this project's README has me wondering:

In other words, say you have a test file at src/test.ts. You've configured TypeScript to output to build/. Using @ava/typescript you can run the test using npx ava src/test.ts.

I.e. why the test.ts is in src. Does this mean that I have to pre-compile the tests myself? I doubt that's it... the sentence above that paragraph is:

It allows AVA to load the compiled JavaScript, while configuring AVA to treat the TypeScript files as test files.

@novemberborn
Copy link
Member

I.e. why the test.ts is in src. Does this mean that I have to pre-compile the tests myself?

Yes. The current state of @ava/typescript assumes you have precompiled all your code.

In my projects, I have the tests files next to the source code, nested in a src directory. So instead of running npx ava build/test/feature.js, I can run npx ava src/test/feature.ts. Internally that gets rewritten to require of build/test/feature.js, which has relative imports of other build files.

@adam-lynch
Copy link
Contributor Author

OK, so just to make sure I understand... you could not use this project, run npx ava build/test/feature.js, and it would be the same? (Assuming all paths in your tests are relative / point to build)

@novemberborn
Copy link
Member

Yes. But then you have to configure the files globs differently and it's just a little awkward. But I did that for a long time 😄

@adam-lynch adam-lynch changed the title README: pointing out that ES modules aren't supported yet README: pointing out that tests need to be precompiled May 2, 2020
@adam-lynch
Copy link
Contributor Author

Ha OK cool. I updated the PR to make it more clear but feel free to just close it, it might just be me

@novemberborn
Copy link
Member

feel free to just close it, it might just be me

No, it's valuable feedback, thank you. I've pushed some more edits. If you can put yourself back into yesterday's headspace, does that better describe what this package does?

@adam-lynch
Copy link
Contributor Author

Definitely better. I wasn't sure about "while configuring AVA to use the TypeScript files" because they're not used. It makes AVA think it's using the TypeScript files, I guess. But having said that, the following paragraph makes it extra clear.

Would this be incorrect?

- while configuring AVA to use the TypeScript files
+ while configuring AVA to use the TypeScript paths

@novemberborn novemberborn changed the title README: pointing out that tests need to be precompiled Clarify use May 2, 2020
@novemberborn novemberborn merged commit 96b4ae4 into avajs:master May 2, 2020
@novemberborn
Copy link
Member

Thanks for the feedback @adam-lynch!

@adam-lynch
Copy link
Contributor Author

👍

@adam-lynch adam-lynch deleted the patch-1 branch May 2, 2020 13:36
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.

None yet

2 participants