我是jest测试的新手,我想从另一个文件导入一个函数来测试。我正在用typescript写代码,它将在node.js服务器上执行。
// ../src/services/misc.ts
export const getIpAdress = () => {
return '192.168.0.52';
};
如果没有calc.test.ts
中的导入,并且没有calc.test.ts
中的最后一个describe调用,测试将运行,并且每个测试都通过。如何修复此问题?
如果我就像现在这样进行测试,测试会失败。
// tests/calc.test.ts
import { getIpAdress } from '../src/services/misc';
const add = (x: number, y: number) => {
return x + y;
};
describe('test add function', () => {
it('should return 15 for add(10,5)', () => {
expect(add(10, 5)).toBe(15);
});
});
describe('test IpAdress function', () => {
it('should return ip adress', () => {
expect(getIpAdress()).toBe('192.168.0.52');
});
});
如果我使用npm run test
执行测试,则会出现失败消息
> ticketing@1.0.0 test
> jest
FAIL dist/tests/calc.test.js
● 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:
C:\Users\joshua\Documents\Business\Ticketing\ticketing\dist\tests\calc.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { getIpAdress } from '../src/services/misc';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1422:14)
FAIL tests/calc.test.ts
● Test suite failed to run
src/services/misc.ts:4:34 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.
4 const __filename = fileURLToPath(import.meta.url);
~~~~~~~~~~~
Test Suites: 2 failed, 2 total
Tests: 0 total
Snapshots: 0 total
Time: 3.912 s
Ran all test suites.
我的tsconfig.json
{
"compilerOptions": {
"target": "es2022",
"module": "es2020",
// "module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"sourceMap": true
}
}
我的jest.config.js
/**@type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node'
};
我的package.json
{
"type": "module",
"name": "ticketing",
"version": "1.0.0",
"description": "Ticketing System to sell Tickets for events online and manage these",
"main": "app.js",
"scripts": {
"start": "node ./dist/app.js",
"tsc": "tsc",
"lint": "eslint ./src --ext .ts",
"test": "jest",
"coverage": "jest --coverage"
},
"scriptsComments": {
"tsc": "Builds all the javascript(js) files from the typescript(ts) files."
},
"repository": {
"type": "git",
"url": "git+ssh://git@gitlab.com/cl/ticketing.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gitlab.com/cl/ticketing/issues"
},
"homepage": "https://gitlab.com/cl/ticketing#readme",
"dependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/jsonwebtoken": "^8.5.9",
"@types/nodemailer": "^6.4.6",
"@types/pdfkit": "^0.12.7",
"@types/qrcode": "^1.5.0",
"@types/qrcode-terminal": "^0.12.0",
"axios": "^1.1.2",
"bcryptjs": "^2.4.3",
"cross-fetch": "^3.1.5",
"dotenv": "^16.0.2",
"express": "^4.18.1",
"express-async-errors": "^3.1.1",
"express-validator": "^6.14.2",
"http-status-codes": "^2.2.0",
"jsonwebtoken": "^8.5.1",
"mariadb": "^3.0.1",
"nodemailer": "^6.8.0",
"pdfkit": "^0.13.0",
"qrcode": "^1.5.1",
"qrcode-terminal": "^0.12.0"
},
"devDependencies": {
"@types/express": "^4.17.14",
"@types/jest": "^29.2.0",
"@types/node": "^18.7.23",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"eslint": "^8.24.0",
"jest": "^29.2.2",
"nodemon": "^2.0.20",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
}
}
1条答案
按热度按时间i2byvkas1#
Solved在此处更改了此内容:
第一个
现在我可以导入所有模块,也可以导入 *.test.ts文件中的node_modules。