Iβm running a test with jest and @moralisweb3 is a dependency of one service.
@moralisweb3 package sits in WalletService where I import
import { EvmChain } from '@moralisweb3/common-evm-utils'
describe('NftService', () => {
let nftService: NftService
let walletService: WalletService
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [NftService, WalletService],
}).compile()
nftService = moduleRef.get<NftService>(NftService)
})
it('should be defined', () => {
expect(nftService).toBeDefined()
})
})```
β Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
β’ If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
β’ If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
β’ To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
β’ If you need a custom transformation specify a "transform" option in your config.
β’ If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/bacis/Development/unbound/apps/server/node_modules/@moralisweb3/common-core/node_modules/axios/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import axios from './lib/axios.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (node_modules/@moralisweb3/common-core/src/controllers/AxiosRetry.ts:1:1)
at Object.<anonymous> (node_modules/@moralisweb3/common-core/src/controllers/RequestController/RequestController.ts:3:1)
at Object.<anonymous> (node_modules/@moralisweb3/common-core/src/controllers/RequestController/index.ts:1:1)
at Object.<anonymous> (node_modules/@moralisweb3/common-core/src/controllers/index.ts:1:1)
at Object.<anonymous> (node_modules/@moralisweb3/common-core/src/index.ts:10:1)
at Object.<anonymous> (node_modules/@moralisweb3/api-utils/src/resolvers2/OperationResolver.ts:1:1)
at Object.<anonymous> (node_modules/@moralisweb3/api-utils/src/resolvers2/index.ts:1:1)
at Object.<anonymous> (node_modules/@moralisweb3/api-utils/src/index.ts:3:1)
at Object.<anonymous> (node_modules/@moralisweb3/streams/src/Streams.ts:1:1)
I tried to transpile moralis packages:
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/dist/', '/dist-test/'],
testTimeout: 20000,
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
'^src/(.*)$': '<rootDir>/src/$1',
'^@axios$': '<rootDir>/src/common/axios',
},
transform: {
'\\.[jt]sx?$': 'babel-jest',
'^.+\\.(ts|tsx)?$': 'ts-jest',
},
}
Hereβs my babel config:
module.exports = {
presets: ['@babel/preset-env', '@babel/preset-typescript'],
plugins: [['@babel/plugin-proposal-decorators', { legacy: true }]],
}