lundi 9 décembre 2019

ts-jest not transforming imported package in monorepo

I have a package in my monorepo (yarn workspaces) that when imported, causes my tests to fail.

I've tried including the package in my transformIgnorePatterns but it still gives me an error

error:

/Users/***/***/monorepo/packages/core/dist/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from "./button";
                                                                                             ^^^^^^

    SyntaxError: Unexpected token export

      1 | import { Box, Typography } from "@material-ui/core"
    > 2 | import { Button } from "@monorepo/core"
        | ^
      3 | import * as React from "react"
      4 | 
      5 | export function Container() {

      at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (src/container/container.tsx:2:1)

jest.config.js

module.exports = {
  transform: {
    "^.+\\.(ts|tsx)$": "ts-jest",
  },
  snapshotSerializers: ["enzyme-to-json/serializer"],
  setupFilesAfterEnv: ["<rootDir>/src/setupEnzyme.ts"],
  transformIgnorePatterns: [
    "node_modules/(?!(@monorepo/core))",
  ],
  globals: {
    "ts-jest": {
      isolatedModules: true,
    },
  },
}

component

import { Box, Typography } from "@material-ui/core"
import { Button } from "@monorepo/core"
import * as React from "react"

export function Container() {
  return (
    <Box>
      <Typography>Im a container</Typography>
      <Button />
    </Box>
  )
}

test

import { mount } from "enzyme"
import * as React from "react"

import { Container } from "./container"

describe("Barcode Component", () => {
  it("renders", () => {
    const wrapper = mount(<Container />)
    expect(wrapper).toMatchSnapshot()
  })
})

importing package.json

  "name": "@monorepo/portal",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "build": "webpack",
    "test": "jest --no-cache",
    "start": "webpack-dev-server",
  },
  "dependencies": {
    "@monorepo/core": "^1.0.0"
  }
}

imported package.json

{
  "name": "@monorepo/core",
  "version": "1.0.0",
  "main": "dist/index.js",
  "typings": "src/index.d.ts",
  "license": "MIT",
  "scripts": {
    "test": "jest",
    "build": "rm -rf ./dist && babel src --out-dir ./dist --extensions '.ts,.tsx' --config-file ./babel.config.js --ignore 'src/setupEnzyme.ts'"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "baseUrl": "./",
    "paths": {
      "@core/*": ["packages/core/src/*"],
      "@portal/*": ["packages/portal/src/*"],
      "@homepage/*": ["packages/homepage-designer/src/*"]
    }
  },
  "include": [
    "packages/core/src/index.ts",
    "packages/portal/src/index.tsx",
    "packages/homepage-designer/src/index.tsx",
    "packages/core/src/**/*.test.tsx",
    "packages/portal/src/**/*.test.tsx",
    "packages/core/__tests__/button.test.tsx"
  ],
  "exclude": ["**/node_modules"]
}

Im wondering what is wrong with my configuration that causes the error

Aucun commentaire:

Enregistrer un commentaire