Skip to content

[Q] Proper setup / Cannot use import statement outside a module #43

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

Closed
sculpt0r opened this issue Jun 8, 2022 · 17 comments
Closed

[Q] Proper setup / Cannot use import statement outside a module #43

sculpt0r opened this issue Jun 8, 2022 · 17 comments

Comments

@sculpt0r
Copy link

sculpt0r commented Jun 8, 2022

Please provide details about:

  • What you're trying to do
  • What happened
  • What you expected to happen

Please share relevant sample code. Or better yet, provide a link to a minimal reproducible example.

We'll also need your AVA configuration (in package.json or ava.config.* configuration files) and how you're invoking AVA. Share the installed AVA version (get it by running npx ava --version) and @ava/babel version (from your package.json file).

I try to setup AVA 4 with TS for a bigger project. But to simplify things I reduce the problem to a simply project: https://github.com/sculpt0r/AVAwithTS

Now I try to run npm run test. The results is:
image

I tried to follow the documentation of AVA & this plugin:

  • installed AVA (4.3.0)
  • installed @ava/typescript (3.0.1)
  • also I found that it should be useful to additionally install source-map-support - so I did it and in cfg file I added:
"require": ['source-map-support/register']
  • in .tsconfig the outDir is set to the same directory (build) as in AVA config rewrite paths
  • also the compile is set to tsc, but if I try to run tests with compile set on false and the build directory already contains js files - I receive the same results as in the screenshot above

I assume I'm missing something - but I'm not sure what? Is it possible to take a look at the sample project: https://github.com/sculpt0r/AVAwithTS, then run npm i and then npm run test.

@novemberborn
Copy link
Member

  • I found that it should be useful to additionally install source-map-support - so I did it

AVA enables the source map support built into Node.js so this shouldn't be necessary.

  • also the compile is set to tsc, but if I try to run tests with compile set on false and the build directory already contains js files - I receive the same results as in the screenshot above

Yea, when set to tsc AVA first does a build, and then runs the tests.


The generated JS code uses ESM syntax:

import test from 'ava';
test('some name', t => {
    t.pass();
});
function someTsSyntax() {
    console.log('anything');
}
//# sourceMappingURL=test.js.map

However this is a test.js file, and there is no "type": "module" in your package.json, which means that AVA will require() it. The file is treated as CommonJS and you cannot use ESM syntax then.

You need to configure TypeScript to output to the CommonJS module syntax, or switch to ESM properly.

@sculpt0r
Copy link
Author

sculpt0r commented Jun 9, 2022

Thank you for your explanation ❤️ I'll try to make a proper setup :)

@sculpt0r
Copy link
Author

sculpt0r commented Jun 9, 2022

Thank you again:

  • I was able to run tests by setting "type": "module" in package.json
  • I was able to run test without changing package.json - but just setting "module": "CommonJS" in tsconfig.json

Also, I removed the source-map-support

Time to move to bigger project 🤞

@sculpt0r
Copy link
Author

sculpt0r commented Jun 9, 2022

Looks like precompiling tests help a lot with time out and speed of tests. Can't confirm 100% - but the only thing that stops me is microsoft/TypeScript#26722 (comment)

Overall - it feels like this issue could be closed?

Edit:
100% - precompiled tests works like a charm 👍

@liby
Copy link

liby commented Aug 29, 2022

Can you help me look at my configuration here? It looks like something went wrong. 🙏 cc @novemberborn

@sculpt0r
Copy link
Author

@liby It is hard to look at your config if you do not share it ;)

@liby
Copy link

liby commented Aug 30, 2022

It is hard to look at your config if you do not share it ;)

Oh, sorry, I forgot to attach the link

@sculpt0r
Copy link
Author

sculpt0r commented Sep 1, 2022

You use old AVA version - is there any reason why you can't upgrade?
Also, you use cfg for the old way of supporting TS. Anyway, we need you to be more informative ;)

What is wrong with your config? Are there any errors? What steps did you try already?

@liby
Copy link

liby commented Sep 1, 2022

You use old AVA version - is there any reason why you can't upgrade?

I am using the latest [email protected], are you reading it wrong?

Anyway, we need you to be more informative ;)

I got the Timed out while running tests error in CI/CD. By searching, I found two solutions:

What is wrong with your config? Are there any errors? What steps did you try already?

To fix the Timed out while running tests error, I made the following changes:
image

But CI/CD started complaining even before the Timed out while running tests error appeared:

  ✖ Unexpected duplicate extensions in options: ’ts’.
Error: Process completed with exit code 1.

So I wonder if I've written my configuration wrong.

@ddanielcruzz
Copy link

@liby I think you got your config wrong, from the link you shared I see that you have

"typescript": {
      "rewritePaths": {
        "src/": "build/"
      },
      "compile": false
}

But your file structure does not match your rewritePaths. Imagine you have your tests at root directory under tests and your TypeScript outDir is set to dist. This would mean that your transpiled tests would be at dist/tests

your-app/
├─ dist/
│  ├─ tests/
│  │  ├─ myTest.js
├─ tests/
│  ├─ myTest.ts
├─ package.json

Then your rewrites would have to be

"typescript": {
      "rewritePaths": {
        "tests/": "dist/tests"
      },
      "compile": "tsc"
}

Note that is required that the value of compile is set to tsc

Looking at your repo you have your tests under __test__, since tsc artifacts are not tracked on Git I don't know the name of your outDir, your configuration would have to be something like

"typescript": {
      "rewritePaths": {
        "__test__":  <your-outDir-path-to-tests>
      },
      "compile": "tsc"
}

@liby
Copy link

liby commented Sep 2, 2022

Note that is required that the value of compile is set to tsc

Is this something that has to be done? I disabled it because @novemberborn mentioned that it said “Would be interesting to try with compile: false and precompile” and wanted to try it out.

After testing, I feel that tsc is not suitable for use in my repo.

@novemberborn
Copy link
Member

With it set to false you need to run tsc yourself before running tests.

@liby
Copy link

liby commented Sep 2, 2022

With it set to false you need to run tsc yourself before running tests.

Thanks for the reminder. So when I get an Timed out while running tests error in CI/CD, is setting compile to false the only solution? Setting timeout to 80s can solve this issue temporarily, but it will lead to too long job time.

@novemberborn
Copy link
Member

Typically from what I've seen, compilation through loader extensions can take so long, or fails quietly, that AVA gives up. I always precompile so that the tests are run with JavaScript.

Looking at the code, the time taken by compile: 'tsc' also counts towards AVAs timeout threshold.

@liby
Copy link

liby commented Sep 4, 2022

I always precompile so that the tests are run with JavaScript.

Because I've been using @swc-node/register to compile.

I also tried to switch to using tsc before, but I encountered a lot of issues not knowing how to solve them, can you show me your config?

@novemberborn
Copy link
Member

@liby
Copy link

liby commented Sep 4, 2022

https://github.com/avajs/get-port/blob/main/ava.config.js

After looking at it, I think there is still a difference between my repo and get-port. My repo works mainly in the node environment. If migrate to tsc and use precompile, I will have to change a lot of config like tsconfig.

There may be some other problems in this process, so I may continue to use [@swc-node/register](https://github.com/swc-project/swc-node/tree/master/packages/register?rgh-link-date=2022-09-04T14%3A12%3A01Z#ava).

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

4 participants