avajs / typescript

Test TypeScript projects using AVA.
MIT License
73 stars 16 forks source link

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

Closed sculpt0r closed 2 years ago

sculpt0r commented 2 years ago

Please provide details about:

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:

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 commented 2 years ago
  • 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 commented 2 years ago

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

sculpt0r commented 2 years ago

Thank you again:

Also, I removed the source-map-support

Time to move to bigger project 🤞

sculpt0r commented 2 years ago

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 https://github.com/microsoft/TypeScript/issues/26722#issuecomment-516935532

Overall - it feels like this issue could be closed?

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

liby commented 2 years ago

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

sculpt0r commented 2 years ago

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

liby commented 2 years ago

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

Oh, sorry, I forgot to attach the link

sculpt0r commented 2 years ago

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 commented 2 years ago

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

I am using the latest version@4.3.3, 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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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

liby commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

liby commented 2 years ago

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).