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

Issue: Finalize and test TypeScript support #4

Open
natterstefan opened this issue Dec 5, 2020 · 1 comment
Open

Issue: Finalize and test TypeScript support #4

natterstefan opened this issue Dec 5, 2020 · 1 comment
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@natterstefan
Copy link
Owner

natterstefan commented Dec 5, 2020

Bug Report

Relevant information

It was not possible to use the react preset in a NextJS TypeScript project (https://github.com/natterstefan/nextjs-tailwind-template) directly. The current setup with ts-loader did not work. This is most likely because of the current module and target in the tsconfig.json in the NextJS project. There is no way to customize this now. Adding tsconfig in globals.["ts-jest"] did not work.

In the end, creating a babel.config.js (according to https://dev.to/ericdouglas/how-to-configure-jest-on-a-next-js-project-2ic6) and using only jest-preset-ns/presets/react/jest-setup.js helped.

Steps to reproduce

  1. add the preset again https://github.com/natterstefan/nextjs-tailwind-template/blob/63ad8fd335c0dfda1b8be24c12a2c630b88feee2/jest.config.js#L2
  2. run yarn test

It also did not work with the ts-jest preset, but that's maybe a different problem. Maybe adding the preset directly works?

Observed Results

Jest was not able to transpile the files and work with them.

Expected Results

Jest can run the tests

@natterstefan natterstefan added bug Something isn't working enhancement New feature or request labels Dec 5, 2020
@natterstefan natterstefan self-assigned this Dec 5, 2020
@natterstefan
Copy link
Owner Author

Solution

Thanks to vercel/next.js#8663 (comment), I was able to resolve it and I will add it to the README later.

// jest.config.js
module.exports = {
  preset: 'jest-preset-ns/presets/react',
  globals: {
    // we must specify a custom tsconfig for tests because we need the typescript transform
    // to transform jsx into js rather than leaving it jsx such as the next build requires.  you
    // can see this setting in tsconfig.jest.json -> "jsx": "react"
    'ts-jest': {
      tsconfig: 'tsconfig.test.json',
    },
  },
  setupFilesAfterEnv: ['jest-preset-ns/presets/react/jest-setup.js'],
}

This is an example tsconfig.json of nextjs:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": false,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

This is the tsconfig.test.json:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant