我有一个示例nodejs项目,它是一个Google Pubsub Cloud Functions示例,我正在其中尝试使用Jest作为测试工具。
当我运行npm脚本进行单元测试时,我收到一条错误消息,指示打字稿未被识别。
我只是在做 npm run test:unit ,我已经将它包含在package.json文件中,但是却出现以下错误。
> jest ./test/unit
FAIL test/unit/orders.test.js
● Test suite failed to run
SyntaxError: C:\Development\GCP-Jest\src\index.ts: Unexpected token, expected ";" (1:10)
> 1 | let pgPool: any;
| ^
2 |
3 | export const orders = async (event: any): Promise<void> => {
4 | let msgDataStr: string;
at Parser.raise (node_modules/@babel/parser/lib/index.js:6387:17)
at Parser.unexpected (node_modules/@babel/parser/lib/index.js:7704:16)
at Parser.semicolon (node_modules/@babel/parser/lib/index.js:7686:40)
at Parser.parseVarStatement (node_modules/@babel/parser/lib/index.js:10371:10)
at Parser.parseStatementContent (node_modules/@babel/parser/lib/index.js:9967:21)
at Parser.parseStatement (node_modules/@babel/parser/lib/index.js:9900:17)
at Parser.parseBlockOrModuleBlockBody (node_modules/@babel/parser/lib/index.js:10476:25)
at Parser.parseBlockBody (node_modules/@babel/parser/lib/index.js:10463:10)
at Parser.parseTopLevel (node_modules/@babel/parser/lib/index.js:9829:10)
at Parser.parse (node_modules/@babel/parser/lib/index.js:11341:17)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.795s
Ran all test suites matching /.\\test\\unit/i.
项目结构如下所示。
src / index.js
let pgPool: any;
export const orders = async (event: any): Promise<void> => {
let msgDataStr: string;
msgDataStr = event.data;
pgPool = 1;
console.error("Error in Unmarshalling data from event" + msgDataStr + pgPool);
};
test / unit / order.test.js
const orders = require('../../src');
describe('DataInflow Orders', () => {
let consoleSpy;
beforeEach(function() {
consoleSpy = jest.spyOn(global.console, 'error').mockImplementation();
});
afterEach(function() {
consoleSpy.mockRestore();
});
it('print outs the error message when received JSON is blank', async () => {
const message = '';
const event = {
data: {
data: Buffer.from(message).toString('base64'),
},
attributes: {},
};
await orders.orders(event);
expect(consoleSpy.mock.calls[1][0]).toEqual(expect.stringContaining('Error in Unmarshalling data from event'));
});
});
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2018",
"moduleResolution": "node",
"baseUrl": "./",
"typeRoots": [
"./src/@types",
"./node_modules/@types"
],
"paths": {
"*": [
"src/@types/*"
]
}
},
"compileOnSave": true,
"include": [
"src"
],
"exclude": [
"node_modules",
"lib"
]
}
package.json
{
"name": "poc-cf",
"version": "0.0.1",
"description": "Cloud Functions",
"repository": {
"type": "git",
"url": ""
},
"license": "ISC",
"dependencies": {
"@google-cloud/pubsub": "^0.30.3",
"moment": "^2.24.0",
"pg": "^7.12.0"
},
"main": "lib/index.js",
"types": "lib/@types",
"jest": {
"verbose": true,
"setupFiles": [
"dotenv/config"
],
"roots": [
"<rootDir>/test"
]
},
"scripts": {
"lint": "eslint src/*/**",
"build": "tsc",
"test": "jest",
"test:unit": "jest ./test/unit",
"test:int": "jest ./test/integration",
"test:coverage": "npm run test -- --coverage"
},
"devDependencies": {
"@google-cloud/functions-framework": "^1.2.1",
"@types/jest": "^24.0.17",
"@types/node": "^12.6.8",
"@types/pg": "^7.4.14",
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
"child_process": "^1.0.2",
"dotenv": "^8.0.0",
"eslint": "^6.2.1",
"jest": "^24.8.0",
"typescript": "^3.4.5"
},
"engines": {
"node": ">=10.15.3"
}
}
答案 0 :(得分:0)
安装后,您需要使用.mpd url
。
将以下类似的ts-jest
配置添加到您的preset
配置中。
jest
答案 1 :(得分:0)
每个人都已经注意到,您需要ts-jest
。将其安装为开发依赖项。
另一个重要的配置是testEnvironment
。默认设置为jsdom
,看起来您可能需要将其设置为node
。
我建议您使用jest.config.js
。这是一个工作示例:
const {defaults} = require('jest-config');
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.ts$',
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts'],
roots: [ "<rootDir>/src" ],
};