microsoft / TypeScript-React-Starter

A starter template for TypeScript and React with a detailed README describing how to use the two together.
MIT License
11.09k stars 1.21k forks source link

SyntaxError: Unexpected token import #213

Open samuelrego opened 6 years ago

samuelrego commented 6 years ago

I have created one NPM library by using typescript react starter. In tsconfig file i given module as 'esnext' when i build and import it in another project it throwing error while doing npm run test. but it works fine in run and build time.

if put module as 'commonjs' it works fine for me. but i am not able to import images (import logo from './logo.ppng')

Please let me know how to run test cases by setuping module as 'esnext';

console log:

E:\frontend\-ui-component\src\setupTests.ts:7
    import * as enzyme from 'enzyme';
    ^^^^^^

    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:189:7)

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        1.907s, estimated 2s
Ran all test suites.

tsconfig of lcomponent (npm pack):

{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "build/dist",
    "module": "esnext",
    "target": "es6",
    "lib": ["es6", "dom","es7"],
    "sourceMap": true,
    "allowJs": false,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "importHelpers": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "declaration": true,
  },

  "exclude": [
    "node_modules",
    "build",
    "scripts",
    "acceptance-tests",
    "webpack",
    "jest"   
  ]
}

tsconfig of project (where i imported project):

{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "build/dist",
    "module": "esnext",
    "target": "es6",
    "lib": ["es2015", "es2017", "dom","esnext","es6"],
    "sourceMap": true,
    "allowJs": false,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "importHelpers": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "declaration": true,
  },
  "exclude": [
    "node_modules",
    "build",
    "scripts",
    "acceptance-tests",
    "webpack",
    "jest",
    "src/setupTests.ts"
  ]
}
akopchinskiy commented 5 years ago

I have almost the same error: Unexpected token * In import * as lib from './lib';

module parameters in tsconfig:

"module": "es6",
"target": "es6",
"moduleResolution": "node",