stryker-js icon indicating copy to clipboard operation
stryker-js copied to clipboard

SyntaxError: Cannot use import statement outside a module

Open yxtao opened this issue 1 year ago • 4 comments

Question

Can Stryker.conf.json file override the default transformIgnorePattern in Jest ?

In Stryker.conf.json ,

testRunnerNodeArgs": ["--experimental-vm-modules"], 
"jest":{ 
     "projectType" : "custom",
      "config" : {
              "testEnvironment":"jest-environment-jsdom-sixteen",
               "transform":{}
       }
}

When I run npx stryker run, it shows that import and export are not supported. SyntaxError: Cannot use import statement outside a module. This points to several packages in node modules. So I added { "transformIgnorePatterns": ["/node_modules/(?!(nodePackage1|nodePackage2)/)"] } to the stryker.conf.json file under the “jest”/“config” section, but it didn’t solve the issue.

testRunnerNodeArgs": ["--experimental-vm-modules"], 
"jest":{ 
     "projectType" : "custom",
      "config" : {
              "testEnvironment":"jest-environment-jsdom-sixteen",
               "transform":{},
                "transformIgnorePatterns": ["/node_modules/(?!(nodePackage1|nodePackage2)/)"]
       }
}

Stryker environment

+-- @stryker-mutator/[email protected]

+-- @stryker-mutator/jest-runner@ 6.2.0-beta.0 

Additional context

It is a react project

yxtao avatar Aug 12 '22 00:08 yxtao

What is your normal npm test command? And what is your jest config (if you have any)

If it's a normal "react-scripts" project, than this config should work:

{
  "jest": {
    "projectType": "create-react-app",
    "config": {
        "here-you-can-override-any-jest-option":{}
     }
  }
}

The error about import and export is coming from the wrong babel transpiling would be my guess. This means that you should not use --experimental-vm-modules or override any options, setting the project type to "create-react-app" should be enough.

nicojs avatar Aug 15 '22 06:08 nicojs

I have the same problem in the stryker generated file test.js:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import path from 'path';
                                                                                  ^^^^^^
SyntaxError: Cannot use import statement outside a module

and this configuration:

{
  "$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",
    "dashboard"
  ],
  "testRunner": "jest",
  "testRunner_comment": "Take a look at https://stryker-mutator.io/docs/stryker-js/jest-runner for information about the jest plugin.",
  "coverageAnalysis": "perTest",
  "ignorePatterns": ["dist", "coverage", "tests", "runtime", "http", "config", "v8coverage"]
}

The application is a server written in TypeScript. Normal testing works using the following command:

npx jest --config jest.config.json --runInBand

with the following configuration:

{
  "rootDir": "..",
  "testEnvironment": "node",
  "verbose": true,
  "testMatch": ["**/tests/*.test.ts"],
  "moduleFileExtensions": ["ts", "js"],
  "extensionsToTreatAsEsm": [".ts"],
  "transformIgnorePatterns": ["!../common/", "!server/", "!./"],
  "transform": {
    "^.+\\.ts$": ["ts-jest", {
      "tsconfig": "tests/tsconfig.json",
      "diagnostics": {"ignore": [1343]}
	  }]
  },
  "setupFilesAfterEnv": ["jest-extended/all"],
  "collectCoverage": false,
  "collectCoverageFrom": [
    "server/*.ts",
    "common/*.ts"
  ],
  "coveragePathIgnorePatterns": [".d.ts", "iie.ts", "node_modules", "save", "WordStatistics.ts"],
  "coverageDirectory": "coverage",
  "coverageReporters": ["text", "json-summary", "html"]
}

(Windows 11, jest 29.3.1, "@stryker-mutator/core": "^6.3.1", node 18.13.0) It is my first attempt at stryker. Any idea? Thanks! mario

crystalfp avatar Jan 25 '23 09:01 crystalfp

Red face. Was a test file outside the project source directory. Added to ignorePatterns solved the problem. Sorry for the noise! mario

crystalfp avatar Jan 25 '23 11:01 crystalfp

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 17 '24 11:03 stale[bot]