Failed import with Paste icon in Jest #1589
-
Hey, all! We're running into an import error when running our Jest test suites after having added Paste icon imports to our components. I've tried a handful of different TS/Jest/Babel configurations, but thought I would reach out before going any further down the rabbit hole. Below is the error we're seeing, as well as our Thanks for the help! Error SyntaxError: Cannot use import statement outside a module
1 | import { forwardRef, useRef, useState, ChangeEvent } from 'react';
2 | import { Margin, Width } from '@twilio-paste/core/style-props';
> 3 | import { AttachIcon } from '@twilio-paste/icons/esm/AttachIcon';
| ^
4 | import { CloseIcon } from '@twilio-paste/icons/esm/CloseIcon';
5 | import { CustomIcon } from '@twilio-paste/icons/esm/CustomIcon';
6 |
at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (components/blocks/fileInput/FileInput.tsx:3:1)
{
"compilerOptions": {
"baseUrl": "src",
"outDir": "./dist/",
"sourceMap": true,
"module": "es6",
"target": "es6",
"jsx": "react-jsx",
"allowJs": true,
"preserveConstEnums": true,
"removeComments": true,
"noImplicitAny": true,
"strict": true,
"moduleResolution": "node",
"noUnusedParameters": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"skipLibCheck": true,
"paths": {
"types": ["../types.ts"]
},
"plugins": [
{
"name": "typescript-plugin-css-modules"
}
]
},
"include": ["src/**/*"],
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts",
"typings/index.d.ts",
"dist",
"out",
"vendor"
]
}
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/en/configuration.html
*/
export default {
globals: {
'ts-jest': {
diagnostics: true,
tsconfig: './tsconfig.json',
},
},
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
// An array of directory names to be searched recursively up from the requiring module's location
moduleDirectories: ['node_modules', 'src'],
// An array of file extensions your modules use
moduleFileExtensions: ['js', 'json', 'ts', 'tsx'],
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: {
'\\.(css|less)$': 'identity-obj-proxy',
},
// The root directory that Jest should scan for tests and modules within
rootDir: 'src',
// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ['<rootDir>/setupTests.ts'],
// A map from regular expressions to paths to transformers
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
}; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
Hi @HartiganHM This comes up fairly regularly, we have a small guide on the getting started page, so start there and let us know if that unblocks you https://paste.twilio.design/getting-started/engineering#handling-icons-in-jest |
Beta Was this translation helpful? Give feedback.
-
Re-posting here for more visibility outside of the nested comment thread: Ultimate Solution
What we tried that was unnecessary
|
Beta Was this translation helpful? Give feedback.
Re-posting here for more visibility outside of the nested comment thread:
Ultimate Solution
Built a custom resolver to point any
esm
icon dependencies to the correspondingcjs
directories