Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

Unexpected token import when using TypeScript and Jest examples #3663

Closed
1 task done
TheRobBrennan opened this issue Feb 2, 2018 · 14 comments
Closed
1 task done

Unexpected token import when using TypeScript and Jest examples #3663

TheRobBrennan opened this issue Feb 2, 2018 · 14 comments

Comments

@TheRobBrennan
Copy link
Contributor

I'm encountering a weird issue with either Babel or Next.js when trying to run TypeScript jest tests.

I can run the Jest test written in plain ol' JavaScript just fine, but the Footer.test.tsx file fails with an unexpected token import message:
`

PASS ./demo.test.js
FAIL components/Layout/Footer/Footer.test.tsx
● Test suite failed to run

/Users/rob/repos/crypto-insights/src/web/components/Layout/Footer/Footer.test.tsx:3
import { shallow } from 'enzyme'
^^^^^^

SyntaxError: Unexpected token import

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)

`

Using the Next.js examples with-jest and with-typescript, I've got the following configuration in place:
// tsconfig.json { "compilerOptions": { "jsx": "react-native", "module": "commonjs", "strict": true, "target": "es2017" } }

// .babelrc { "env": { "development": { "presets": ["next/babel"] }, "production": { "presets": ["next/babel"] }, "test": { "presets": [["next/babel", { "preset-env": { "modules": "commonjs" } }]] } } }

// jest.config.js module.exports = { setupFiles: ['<rootDir>/jest.setup.js'], testPathIgnorePatterns: ['<rootDir>/.next/', '<rootDir>/node_modules/'], testRegex: '\\.test\\.*', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], collectCoverageFrom: [ '!jest.setup.js', '**/*.{js,jsx,ts,tsx}', '!build/**', '!coverage/**' ] }

`// jest.setup.js
import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

configure({ adapter: new Adapter() })

`

  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Shouldn't I be able to run my jest test written in TSX:
`// Footer.test.tsx
import { shallow } from 'enzyme'
import { Footer } from './Footer'

describe('Footer', () => {
it('should render', () => {
const wrapper = shallow(

)
expect(wrapper).toBeDefined()
})
})
`

Current Behavior

I can run the Jest test written in plain ol' JavaScript just fine, but the Footer.test.tsx file fails with an unexpected token import message:
`

PASS ./demo.test.js
FAIL components/Layout/Footer/Footer.test.tsx
● Test suite failed to run

/Users/rob/repos/crypto-insights/src/web/components/Layout/Footer/Footer.test.tsx:3
import { shallow } from 'enzyme'
^^^^^^

SyntaxError: Unexpected token import

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:318:17)

`

Any help would be appreciated. Thanks!!

@lfades
Copy link
Member

lfades commented Feb 5, 2018

Next 5 just went out, can you try it out and the plugin for typescript too ?

@TheRobBrennan
Copy link
Contributor Author

TheRobBrennan commented Feb 10, 2018 via email

@lxe
Copy link

lxe commented Mar 3, 2018

I'm running into an issue that might or might not be related to this.

I'm using mapbox-gl. Usually one would simply import the commonjs entrypoint, but instead I'm importing mapbox-gl/src using another babel-loader to transpile these files. Like so:

import MapboxMap from 'mapbox-gl/src/ui/map';

Since mapbox-gl/src is es6+flow, I configured next.config.js like so:

module.exports = {
  webpack: config => {
    config.module.rules.push({
      test: /mapbox\-gl\/src\/.+\.js$/,
      loader: 'babel-loader',
      options: {
        presets: [
          'babel-preset-flow',          
          'babel-preset-env', 
        ],
        plugins: [
          'babel-plugin-static-fs',
          'babel-plugin-transform-class-properties'
        ]
      }
    });

    return config;
  }
};

running next works, but after triggering build by requesting http://localhost:3000 I get this error:

/Users/aleksey/my-project/node_modules/mapbox-gl/src/ui/map.js:30
import type {LngLatLike} from '../geo/lng_lat';
^^^^^^

SyntaxError: Unexpected token import

These import statements should have been babelified out (as seen by the intermediaries produced inside .cache: gzcat node_modules/.cache/babel-loader/a2b2aa6e1bdb254f0db2d539f7b03e24a389bbb7.json.gz | jq -r .code), but I think some webpack plugin that's loaded by next is ignoring the transpiled file, and using a require() instead.

@timneutkens
Copy link
Member

@TheRobBrennan is this still relevant?

@TheRobBrennan
Copy link
Contributor Author

@timneutkens I don't believe so. Everything seems to be in a great place.

@poc7667
Copy link

poc7667 commented Mar 25, 2018

how closed this issue? I run into the same issue as well.

@13806
Copy link

13806 commented Mar 27, 2018

how closed this issue? I run into the same issue as well.

same

@iilei
Copy link

iilei commented Apr 1, 2018

same here with 6.0.0-canary.2 / node 8.11.1

Spent some time trying different versions (starting with a merge of copy+paste from examples).

e.g:

"dependencies": {
    "next": "6.0.0-canary.2",
    "react": "16.2.0",
    "react-dom": "16.2.0"
  },
  "devDependencies": {
    "@types/next": "2.4.8",
    "@types/react": "16.1.0",
    "@zeit/next-typescript": "0.1.1",
    "enzyme": "3.3.0",
    "enzyme-adapter-react-16": "1.1.1",
    "jest": "22.4.3",
    "react-test-renderer": "16.3.0",
    "typescript": "2.8.1"
  }

No avail ...

@maritz
Copy link

maritz commented Apr 16, 2018

The example project works fine. Trying to get it to work in an actual project always fails.

My theory: There is something wrong with the compile steps and actual compile errors get swallowed. Then afterwards it tries to use the uncompiled .tsx and fails.

@maritz
Copy link

maritz commented Apr 21, 2018

@timneutkens @TheRobBrennan can we get this issue re-opened or should we create a new one?

@TheRobBrennan
Copy link
Contributor Author

Sure - reopened 😊

@TheRobBrennan TheRobBrennan reopened this Apr 21, 2018
@makstr
Copy link

makstr commented Apr 30, 2018

I was using "with-jest-typescript" example and ran into this problem, looks like changing tsconfig.json compilerOptions.jsx from "preserve" to "react" gets the tests running, but breaks the app.

So ended up with custom tsconfig.jest.json

// tsconfig.jest.json
{
  "extends": "./tsconfig",
  "compilerOptions": {
    "jsx": "react"
  }
}

and updated the test.config.js

...
  globals: {
    "ts-jest": {
      useBabelrc: true,
      tsConfigFile: "tsconfig.jest.json"
    }
  },
...

@zxzl
Copy link
Contributor

zxzl commented May 8, 2018

Upon @makstr 's suggestion, if you face trouble resolving modules, try

{
  "extends": "./tsconfig",
  "compilerOptions": {
    "jsx": "react",
    "module": "commonjs"
  }
}

as mentioned in ts-jest doc.
The tsconfig.js 's compilerOptions is set to esnext.

timneutkens pushed a commit that referenced this issue Jun 6, 2018
Hello! I ran into an issue using typescript and jest with next 6.0.0. I was able to work through fixing it and I wanted to share my solution back to next.js, by upgrading the with-jest-typescript example to next 6.0.0.

The steps I followed were:

1. `npx babel-upgrade --write` which added babel-core@^7.0.0-bridge.0 to allow jest's babel 6 to play nice with next's babel 7
2. Remove `ts-jest` and replace with `babel-jest` to use babel to transform the typescript code, as is done when the dev and production builds run
3. Update the babelrc to use commonjs modules in test mode to be compatible with jest

Also, I removed the `NODE_ENV=test` on the jest task, because jest sets the env to test anyways, and I'm on windows where this code is incorrect. The other option is to use `cross-env` but I felt it was simpler to just remove the environment override.

To my knowledge, this PR would help on the following issues:

#3663 #4227 #4531 #4528 #4239
lependu pushed a commit to lependu/next.js that referenced this issue Jun 19, 2018
Hello! I ran into an issue using typescript and jest with next 6.0.0. I was able to work through fixing it and I wanted to share my solution back to next.js, by upgrading the with-jest-typescript example to next 6.0.0.

The steps I followed were:

1. `npx babel-upgrade --write` which added babel-core@^7.0.0-bridge.0 to allow jest's babel 6 to play nice with next's babel 7
2. Remove `ts-jest` and replace with `babel-jest` to use babel to transform the typescript code, as is done when the dev and production builds run
3. Update the babelrc to use commonjs modules in test mode to be compatible with jest

Also, I removed the `NODE_ENV=test` on the jest task, because jest sets the env to test anyways, and I'm on windows where this code is incorrect. The other option is to use `cross-env` but I felt it was simpler to just remove the environment override.

To my knowledge, this PR would help on the following issues:

vercel#3663 vercel#4227 vercel#4531 vercel#4528 vercel#4239
@timneutkens
Copy link
Member

Should be solved with the Next 7 rewrite of next/dynamic.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants