wallabyjs / public

Repository for Wallaby.js questions and issues
http://wallabyjs.com
759 stars 45 forks source link

Unexpected token export​​/import jest-runtime script_transform #1935

Closed jzena closed 5 years ago

jzena commented 5 years ago

Issue description or question

I'm running into an issue where my test fails to run with a message like this: However when I run npm run test the test works as expected, could you help with that please.

​​[Error] Runtime error: C:\projects\02 autodesk\projects\quark-ui\node_modules\@adsk\bim360-matrix-react-components\es\components\SimpleModal\index.js:1​​
​​[Error] ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { default } from './SimpleModal';​​
​​[Error]                                                                                          ^^^^^^​​
​​[Error] SyntaxError: Unexpected token export​​
​​[Error]     at ScriptTransformer._transformAndBuildScript (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\script_transformer.js:316:17)​​
​​[Error]     at ScriptTransformer.transform (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\script_transformer.js:346:19)​​
​​[Error]     at Runtime._execModule (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\index.js:433:53)​​
​​[Error]     at Runtime.requireModule (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\index.js:265:14)​​
​​[Error]     at Runtime.requireModuleOrMock (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\index.js:337:19)​​
​​[Error]     at Object.<anonymous> (.\src\components\common\CreateProjectModal\CreateProjectModal.js:38:59)​​
​​[Error]     at Runtime._execModule (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\index.js:448:13)​​
​​[Error]     at Runtime.requireModule (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\index.js:265:14)​​
​​[Error]     at Runtime.requireModuleOrMock (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\index.js:337:19)​​
​​[Error]     at Object.<anonymous> (.\src\components\common\CreateProjectModal\test\CreateProjectModal.test.js:11:65)​​
​​[Error]     at Runtime._execModule (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\index.js:448:13)​​
​​[Error]     at Runtime.requireModule (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runtime\build\index.js:265:14)​​
​​[Error]     at C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-jasmine2\build\index.js:114:13​​
​​[Error]     at Generator.next (<anonymous>)​​
​​[Error]     at step (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-jasmine2\build\index.js:144:191)​​
​​[Error]     at C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-jasmine2\build\index.js:144:437​​
​​[Error]     at new Promise (<anonymous>)​​
​​[Error]     at C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-jasmine2\build\index.js:144:99​​
​​[Error]     at jasmine2 (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-jasmine2\build\index.js:122:17)​​
​​[Error]     at C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runner\build\run_test.js:73:28​​
​​[Error]     at Generator.next (<anonymous>)​​
​​[Error]     at step (C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runner\build\run_test.js:147:191)​​
​​[Error]     at C:\projects\02 autodesk\projects\quark-ui\node_modules\jest-runner\build\run_test.js:147:361​​
​​[Error]     at <anonymous>​​
​​[Error]     at process._tickCallback (internal/process/next_tick.js:188:7)​​
​​[Error] Runtime error: C:\projects\02 autodesk\projects\quark-ui\node_modules\@adsk\bim360-matrix-react-components\es\components\SearchBox\index.js:1​​
​​[Error] ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { default } from './SearchBox';​​

babel.config File

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current'
        }
      }
    ],
    '@babel/preset-react'
  ],
  plugins: [
    '@babel/plugin-syntax-dynamic-import',
    '@babel/plugin-proposal-class-properties',
    '@babel/plugin-proposal-object-rest-spread'
  ],
  env: {
    test: {
      presets: [
        '@babel/preset-env',
        '@babel/preset-react'
      ],
      plugins: [
        '@babel/plugin-transform-runtime',
        '@babel/plugin-proposal-object-rest-spread',
        'dynamic-import-node-babel-7',
        'require-context-hook'
      ]
    }
  }
};

jest.config.json

{
  "testURL": "http://localhost",
  "setupFiles": [
    "raf/polyfill",
    "<rootDir>/src/tests/setupTests.js"
  ],
  "snapshotSerializers": [
    "enzyme-to-json/serializer"
  ],
  "moduleNameMapper": {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/src/tests/__mocks__/fileMock.js",
    "^@quark(.*)$": "<rootDir>/src$1",
    "^@core(.*)$": "<rootDir>/src/core$1",
    "^configApp$": "<rootDir>/src/app.config",
    "\\.(css|less|scss)$": "<rootDir>/src/tests/__mocks__/styleMock.js",
    "^relationships.config$": "<rootDir>/src/model/relationships.config.js"
  },
  "transformIgnorePatterns": [
    "<rootDir>/node_modules/(?!@adsk).+",
    "<rootDir>/node_modules/(@adsk/forge-hfdm).+"
  ],
  "coveragePathIgnorePatterns": [
    "<rootDir>/node_modules/.+",
    "<rootDir>/src/.*\\.mock\\.js"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/",
   "<rootDir>/src/.*\\.mock\\.js",
    "/e2e-tests/"
  ]
}

Wallaby.js configuration file

process.env.BABEL_ENV = 'test';
process.env.NODE_ENV = 'test';

module.exports = function(wallaby) {
  // Babel, jest-cli and some other modules may be located under
  // react-scripts/node_modules, so need to let node.js know about it
  const path = require('path');

  process.env.NODE_PATH +=
    path.delimiter +
    path.join(__dirname, 'node_modules') +
    path.delimiter +
    require('module').Module._initPaths();

  return {
    files: [
      'src/**/*.+(js|jsx|json|snap|css|less|sass|scss|jpg|jpeg|gif|png|svg)',
      '!src/**/*.test.js?(x)',
      'jest.config.json',
      {
        pattern: 'node_modules/@adsk/bim360-matrix-react-component/**/*.js',
        ignore: true
      }
    ],

    tests: ['src/**/*.test.js?(x)'],

    env: {
      type: 'node',
      runner: 'node'
    },
    compilers: {
      '**/*.js': wallaby.compilers.babel({
        babel: require('@babel/core'),
        presets: ['@babel/preset-env', '@babel/preset-react'],
        plugins: [
          '@babel/plugin-transform-runtime',
          '@babel/plugin-proposal-object-rest-spread',
          'dynamic-import-node-babel-7',
          'require-context-hook'
        ]
      })
    },

    setup: (wallaby) => {
      const jestConfig = require('./jest.config');

      wallaby.testFramework.configure(jestConfig);
    },
    testFramework: 'jest'
  };
};

Code editor or IDE name and version

Visual Studio Code v1.29.1

OS name and version

Windows

ArtemGovorov commented 5 years ago

Jest needs your Babel config in your Wallaby cache folder in order to compile node modules, so you need to add your babel config file to your files list, so something like this should help:

...

module.exports = function(wallaby) {
  ...

  return {
    files: [
      'src/**/*.+(js|jsx|json|snap|css|less|sass|scss|jpg|jpeg|gif|png|svg)',
+     'babel.config.js' // or whatever your babel config name is
      ...
    ],

    ...
  };
};
jzena commented 5 years ago

Hi @ArtemGovorov thanks for the response, I've added the babel config and now I'm getting this issue: As you can see the are errors on files that don't have test extension. Is there any missing on my wallaby configuration? on the other side how can I use the babel.config.js file inside of wallaby.compilers.babel ?

​​[Error] Some long running code has been detected: one of your files is taking more than 5000ms to execute.​​
​​[Error] Execution of the following files has started but has not finished:​​
​​[Error] - src/store.js​​
​​[Error] - src/actions/account.actions.js​​
​​[Error] - src/actions/project.actions.js​​
​​[Error] - src/core/auth.core.js​​
​​[Error] - src/core/index.js​​
​​[Error] - src/reducers/account.reducer.js​​
​​[Error] - src/reducers/index.js​​
​​[Error] - src/components/common/index.js​​
​​[Error] - src/components/common/Header/Header.js​​
​​[Error] - src/components/common/ProjectSelector/ProjectSelector.js​​
​​[Error] - src/components/common/Header/tests/Header.test.js​​
​​[Error] Try commenting out the test or excluding the test file from the `tests` list in your wallaby config,​​
​​[Error] and restarting wallaby to make sure that it is this test/file causing the issue and not something else.​​
​​[Error] Pinging test runner sandbox...​​
​​[Error] The sandbox is not responsive. Check for possibly recently introduced infinite loops.​​
ArtemGovorov commented 5 years ago

As you can see the are errors on files that don't have test extension. Is there any missing on my wallaby configuration?

It's hard to tell, the error says that one of your tests in src/components/common/Header/tests/Header.test.js​​ never ends executing. From the stack of files it looks like the execution freezes somewhere in the src/store.js​​ file. The error message may also pop up if one of you tests takes >5s to execute, so if your test runs succeeds eventually, then you may just ignore the error.

If if hangs forever, then we'll need a sample repo from you to be able to reproduce and investigate the configuration issue.

on the other side how can I use the babel.config.js file inside of wallaby.compilers.babel

It should happen automatically if you just have:

compilers: {
      '**/*.js': wallaby.compilers.babel({})
},