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

[TS] empty import type declaration makes initial test run fail #3386

Closed
m-ronchi opened this issue Jan 27, 2022 · 8 comments
Closed

[TS] empty import type declaration makes initial test run fail #3386

m-ronchi opened this issue Jan 27, 2022 · 8 comments
Labels
🐛 Bug Something isn't working

Comments

@m-ronchi
Copy link

Summary

if a file contains an import in the form

import type { } from "some module";

then stryker generates an invalid import statement in the mutated ts file:

import type from "some module";

which fails to compile with the error

TSError: ⨯ Unable to compile TypeScript:
src/index.ts(65,13): error TS1005: '=' expected.

Stryker config

{
  "$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json",
  "_comment": "This config was generated using 'stryker init'. Please take a look at: https://stryker-mutator.io/docs/stryker-js/configuration/ for more information",
  "packageManager": "npm",
  "reporters": [
    "html",
    "clear-text",
    "progress"
  ],
  "testRunner": "mocha",
  "coverageAnalysis": "perTest",
  "checkers": [
    "typescript"
  ],
  "tsconfigFile": "tsconfig.json"
}

Test runner config

mocharc.json

{
    "$schema": "https://json.schemastore.org/mocharc",
    "require": [
        "ts-node/register",
        "source-map-support/register"
    ],
    "spec": [
        "test/**/*.spec.ts"
    ]
}

Stryker environment

├── @stryker-mutator/core@5.6.1
├── @stryker-mutator/mocha-runner@5.6.1
├── @stryker-mutator/typescript-checker@5.6.1
+-- mocha@9.2.0

Test runner environment

mocha

Your Environment

software version(s)
node 16
npm 8
Operating System mac

Add stryker.log
stryker.log

code-mutated.zip
code-original.zip

@m-ronchi m-ronchi added the 🐛 Bug Something isn't working label Jan 27, 2022
@nicojs
Copy link
Member

nicojs commented Jan 27, 2022

A small correction

import type { } from 'some-module';

Gets instrumented to:

import type 'some-module';

Which indeed cannot be compiled with tsc.

Will have to investigate further.

@nicojs
Copy link
Member

nicojs commented Jan 27, 2022

issue3386.zip

@nicojs
Copy link
Member

nicojs commented Jan 27, 2022

It seems that both code examples have the exact same AST. So I'm not sure if there is anything we can do about it. @m-ronchi what exactly is your use case for import type {} from 'some-module'?

@m-ronchi
Copy link
Author

it contains an external module augmentation, plus other code that is injected at an upper layer. I need to import the type declarations when I just include the code (for unit tests, external module is mocked).

more specifically, it is a fastify plugin with

import fp from "fastify-plugin";

declare module "fastify" {
    interface FastifyInstance { // add myPlugin to external type declaration
        myPlugin: Foo;
    }
}

export const plugin = fp(function (fastify) {
    fastify.decorate("myPlugin", foo); // add myPlugin to runtime instance
});

import type declarations are seen by tsc but stripped at runtime, while import "module" is retained for runtime side effects

@nicojs
Copy link
Member

nicojs commented Feb 8, 2022

I see. No easy solution I'm afraid. I've opened babel/babel#14250

@nicojs
Copy link
Member

nicojs commented Feb 9, 2022

Some traction there, hopefully a fix soon

@bartekleon
Copy link
Member

supposedly fixed in https://github.com/babel/babel/releases/tag/v7.17.7 babel/babel#14309
it seems we are using 7.18 now, so it should be working now. Could you check if issue still persists?

@m-ronchi
Copy link
Author

looks like it's fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants