我正在尝试使用Typescript开玩笑,但出现以下错误。该项目在webpack和ts-node中运行良好。出于某种原因,我无法使其开玩笑。
FAIL src/__tests__/classes/Utils.spec.ts
● Test suite failed to run
Cannot find module 'typescript'
Require stack:
- /Users/myuser/repos/project/node_modules/ts-jest/dist/config/config-set.js
- /Users/myuser/repos/project/node_modules/ts-jest/dist/ts-jest-transformer.js
- /Users/myuser/repos/project/node_modules/ts-jest/dist/index.js
- /Users/myuser/repos/project/node_modules/@jest/transform/build/ScriptTransformer.js
- /Users/myuser/repos/project/node_modules/@jest/transform/build/index.js
- /Users/myuser/repos/project/node_modules/jest-runtime/build/index.js
- /Users/myuser/repos/project/node_modules/@jest/core/build/cli/index.js
- /Users/myuser/repos/project/node_modules/@jest/core/build/jest.js
- /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/build/cli/index.js
- /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/bin/jest.js
- /Users/myuser/repos/project/node_modules/jest/bin/jest.js
- /usr/local/lib/node_modules/jest/node_modules/import-local/index.js
- /usr/local/lib/node_modules/jest/bin/jest.js
这是配置。我已经尝试了roots属性和moduleNameMapper的许多变体,但是无论我在配置中进行了什么更改,错误消息都是完全相同的。
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleDirectories: ['node_modules', 'src'],
moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1',
}
};
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"src/*": ["src/*"],
"tests/*": ["__tests__/*"]
},
"target": "es6",
"module": "commonjs",
"esModuleInterop": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"inlineSourceMap": false,
"outDir": "dist",
"lib": ["es6", "dom", "esnext", "esnext.asynciterable"],
"typeRoots": ["node_modules/@types", "src/typings"]
},
"awesomeTypescriptLoaderOptions": {
"useBabel": true,
"useCache": true,
"useTranspileModule": true
},
"include": ["src/**/*.ts"],
"exclude": ["./node_modules/*"]
}
import { Utils } from 'src/utils/classes/Utils';
test('Generates a 6 digit number'), () => {
expect(Utils.randomNumberGenerator(6).toHaveLength(6))
});
export class Utils {
public static randomNumberGenerator(length: number): number {
const baseNumber: number = Number(1 + '0'.repeat(length - 1));
const randomMultiplier: number = Math.floor(Math.random() * Number('9' + '0'.repeat(length - 1)));
const randomToken: number = baseNumber + randomMultiplier;
return randomToken;
}
}