添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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
  • Issue
  • Because ts-jest overrides module to commonjs in the tsconfig.json no tests can be written with the new dynamic import('./myModule') syntax. You get either 'import' and 'export' may only appear at the top level or a Syntax error .

  • Expected behavior
  • Dynamic imports should work.

  • Link to a minimal repo that reproduces this issue
  • Just type import('./myModule') in your test or implementation. That's enough.

    @screendriver I've added a test that seems to be passing

    Can you see if the test covers this issue?

    If it does, then I'm not sure why it isn't working for you. Did you test with the latest typescript version?

    I've added a test for dynamic imports with allowSyntheticDefaultImports set to true . If you don't need babel, the solution is to skip it (see my previous comment).

    If you need babel, you might have to add the right plugin and update your .babelrc (this is conjecture - I haven't tested this)

    If if set skipBabel to true I get a lot of like TypeError: Cannot read property 'Nothing' of undefined or jest.fn() value must be a mock function or spy. If I set skipBabel to false everything works but dynamic imports.

    By the way: I don't use Babel in my project. There is only TypeScript (and webpack). Nothing more.

    I'm not sure what's going on. Can you do one of the following:

  • create a minimal repo that reproduces this
  • add a test in the new branch here that reproduces this. I've already added two tests . You can add more to replicate this issue
  • I created for you a minimal repository where you can reproduce the error.

    tsc runs fine (and it works in the browser as well)
    jest fails with errors.

    I added

    "ts-jest": {
      "skipBabel": true
    

    and allowSyntheticDefaultImports to true. If you set skipBabel to false you get the import syntax error.

    yeah, I've just tested this without ts-jest and the way Maybe is imported needs to change for this to work. Here's a repo that demonstrates this without ts-jest. Run 'yarn' and 'yarn test' to see the working version.

    If you replace the import in App.tsx with the kind you have, it'll show Maybe as undefined

    Ah ok. So no matter if I have Node 8.* installed and can use almost every shiny new JavaScript language feature I still have to transpile everything down to ES5? If yes: this results in slower transpilation and runtime. If we could use ES2015 as target for example, TypeScript has to transpile almost nothing and Node could run the code almost as it was written (except the imports and type definitions) => faster transpilation, faster runtime.

    i can confirm that's correct at least as far as i've experienced things....every time i try with it enabled, jest fails...

     FAIL  src/lib/localizations/LocalizationManager.spec.tsx
      ● Test suite failed to run
        /Users/Gabe/Sites/roastlogger-web/src/lib/docs/index.tsx: Unexpected token (6:18)
            4 | const Loader = props => React.createElement('div', props);
            5 | const Docs = loadableVisiblity({
          > 6 |     loader: () => import('./container'),
              |                   ^
            7 |     loading: Loader
            8 | });
            9 | export { actions, actionTypes, KEY, reducer };
              

    I think I ran into the same problem as you guys and I made a minimal repro repo here so that the maintainers can figure out what's wrong. Steps to reproduce:

  • Clone and run yarn.
  • git checkout ea5ea730.
  • Run npm test. Tests fail because of the dynamic import in src/addAndMultiply.ts.
  • git checkout df062207.
  • Run npm test. Tests pass because of this change.
  • Hi @kulshekhar,

    unfortunately not because @huy-nguyen just disable allowSyntheticDefaultImports for it's tests and that's it. That means that your code with third party dependencies (for example import React from 'react') will not compile anymore when you run tsc -p <your-directory-with-the-test-tsconfig>.

    I already have two tsconfig.json files:

  • one for my production application code
  • one for my tests (where I set "module": "commonjs")
  • In my CI build I run tsc and after that tsc -p tests. When I enable allowSyntheticDefaultImports in the first tsconfig.json and disable that in in the test specific second tsconfig.json the build will fail. If I would disable the second build step tsc -p tests and just rely on jest and only would run the jest tests without compilation I get a lot of failing tests because for example

    Warning: React.createElement: type is invalid -- expected a string (for built-in
    components) or a class/function (for composite components) but got:
    undefined. You likely forgot to export your component from the file it's
    defined in, or you might have mixed up default and named imports.

    because TypeScript emits wrong / no files for third party dependencies like import glamorous from 'glamorous';

    Finally I could create a repository that reproduces this issue 🎉

  • If you run yarn tsc everything works.
  • If you run yarn jest you can see the new error coming up when I set allowSyntheticDefaultImports to false in my test tsconfig.json
  • If you run yarn tsc -p test-setup you can see that the workaround above is not a real solution (additionally to the failing tests)
  • I enabled that now (I oversight that flag...) but the tests are now really slow (4 tests are running in 30 seconds 😳) and I'm getting different errors now:

        TypeError: Cannot read property 'text' of undefined
          at transpileViaLanguageServer (node_modules/ts-jest/dist/transpiler.js:61:21)
          at Object.transpileTypescript (node_modules/ts-jest/dist/transpiler.js:10:16)
          at process (node_modules/ts-jest/dist/preprocessor.js:27:41)
          at Object.process (node_modules/ts-jest/index.js:8:51)

    @screendriver I did, but the error persisted.

    But then I removed the enableTsDiagnostics flag, and it works, for some reason?

    Also leaving the enableTsDiagnostics flag, and setting the allowSyntheticDefaultImports to false also makes the tests work, so I'm not sure what's going on.

    I did a pull request and it works now in your repository (it's a different issue as I said 😉)

    noblica/ts-jest-dynamic-imports#1

    Hi i've tried out the above solution with a separate jest tsconfig however i'm still getting the same error.

    The only difference to add is that I'm in a mixed TS project (js enabled). Could this be the cause?

    In case this helps anyone in the future, I got this working by configuring .babelrc to add dynamic import support as indicated in the Jest docs:

    "presets": [["env", {"modules": false}]], "plugins": ["syntax-dynamic-import"], "env": { "test": { "plugins": ["dynamic-import-node"]

    Note you may need to run jest --clearCache to clear your Jest cache to get this to work.

    If you don't use babel for your project normally, you will still need to add a .babelrc file and devDependencies on these two plugins just for this use case.

    Use 100%cpu! work with plotly-extension and 'create a new view of output' of a chart that output by plotly. jupyterlab/jupyterlab#5500

    In case someone still struggles with this problem, what helped for me was changing Jest preset from
    ts-jest/presets/js-with-babel to ts-jest/presets/js-with-ts. I guess it had something to do with the fact that in our codebase we use mix of js and ts files and our tests are written in TS while the tested file with dynamic import was still JS.