Reputation: 3461
I am trying to configure a Razzle & React & TypeScript project with jest
for TypeScript unit tests, more precisely with ts-jest
but I get the error in the screenshot below although the path mappings are good. I've followed the official docs and I do not understand why I get the error.
The @/server/helpers
directory is mapped to ./src/server/helpers
which is an existing directory that contains an index.tsx
file and other files too.
I tried manually specifying the path mappings as you can see in the following section.
I don't know if it is relevant but this is actually a project with more than one server and with more than one entry-points and the tsconfig.json
file is shared between all of them.
I have followed the instructions on this page.
jest.config.js
const { pathsToModuleNameMapper } = require("ts-jest/utils");
const { compilerOptions } = require("./tsconfig.json");
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
notify: true,
testEnvironment: 'node',
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths)
// moduleNameMapper: {
// "^@/(.*)$": "<rootDir>/src/$1"
// }
};
tsconfig.json
{
"compilerOptions": {
"target": "ES2019",
"lib": [
"dom",
"dom.iterable",
"ES2019"
],
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "CommonJS",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"strictNullChecks": true,
"skipLibCheck": true,
"baseUrl": "src",
"paths": {
"@/*": [
"./*"
]
}
},
"include": [
"src"
]
}
razzle.config.js
'use strict';
const StartServerPlugin = require("razzle-start-server-webpack-plugin");
const WebpackMessages = require("webpack-messages");
module.exports = {
plugins: ['scss'/* , 'eslint' */],
modifyWebpackOptions({ options: { webpackOptions } }) {
webpackOptions.notNodeExternalResMatch = (request) => new RegExp('react-syntax-highlighter|'
+ 'react-virtualized|'
+ 'react-select-virtualized|'
+ 'tiny-slider').test(request);
webpackOptions.babelRule.include = webpackOptions.babelRule.include.concat([
/react-syntax-highlighter/,
/react-virtualized/,
/react-select-virtualized/,
/tiny-slider/,
]);
return webpackOptions;
},
modifyWebpackConfig(opts) {
const config = opts.webpackConfig;
const options = opts.options.webpackOptions;
if (opts.env.target === 'node') {
config.entry.wsserver = ['./src/server/ws.tsx'];
if (opts.env.dev) {
config.entry.wsserver.unshift(
`${require.resolve('webpack/hot/poll')}?300`
);
// Pretty format server errors
config.entry.wsserver.unshift(
require.resolve('razzle-dev-utils/prettyNodeErrors')
);
config.plugins.push(
new StartServerPlugin(
Object.assign(options.startServerOptions, {
entryName: 'wsserver',
verbose: true,
debug: true,
nodeArgs: ['--inspect=127.0.0.1:9231'],
killOnExist: true,
killOnError: true,
signal: 'SIGTERM'
})
)
);
}
}
config.plugins.push(new WebpackMessages({
logger: str => console.log(`>> ${str}`)
}));
return config;
}
};
Upvotes: 2
Views: 582