letters-social icon indicating copy to clipboard operation
letters-social copied to clipboard

Got "SyntaxError: Unexpected token import" when run test in "chapter-9" branch

Open nguyen190887 opened this issue 6 years ago • 1 comments

I check out to "chapter-9", then run npm test and got error below

FAIL  test/pages/home.test.js
  ● Test suite failed to run

    /Users/tringocnguyen/working/letters-social/test/setup.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){/* istanbul ignore next */var cov_1f0qdrgp0z = function () {var path = '/Users/tringocnguyen/working/letters-social/test/setup.js',hash = 'e68af4693e3d16586c1ad1795d5a6923e30fff61',global = new Function('return this')(),gcv = '__coverage__',coverageData = { path: '/Users/tringocnguyen/working/letters-social/test/setup.js', statementMap: { '0': { start: { line: 4, column: 0 }, end: { line: 4, column: 45 } }, '1': { start: { line: 6, column: 0 }, end: { line: 9, column: 2 } }, '2': { start: { line: 12, column: 0 }, end: { line: 32, column: 2 } }, '3': { start: { line: 16, column: 12 }, end: { line: 19, column: 14 } }, '4': { start: { line: 26, column: 8 }, end: { line: 30, column: 10 } } }, fnMap: { '0': { name: '(anonymous_0)', decl: { start: { line: 15, column: 21 }, end: { line: 15, column: 22 } }, loc: { start: { line: 15, column: 27 }, end: { line: 20, colum
    
    SyntaxError: Unexpected token import
      
      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
      at handle (node_modules/worker-farm/lib/child/index.js:44:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:51:3)
      at emitTwo (events.js:126:13)

I googled and figured out that the root cause is JEST automatically set environment to test, while we only have development and production environment in .babelrc (see https://github.com/react-in-action/letters-social/blob/chapter-9/.babelrc).

Then I added below snippet to .babelrc and it worked

        "test": {
            "presets": ["react-app", "env"],
            "plugins": ["transform-class-properties"]
        }

Could you have a look at this issue? I was wondering if my workaround is good enough 😄

nguyen190887 avatar Oct 13 '18 23:10 nguyen190887

It's ok, thanks.

flyonok avatar Jul 28 '21 03:07 flyonok