我正尝试使用ts-Jest / Jest和Chai将测试添加到用TypeScript编写的Electron + Vue2应用程序中。我已经解决了以前的问题,例如无法识别导入和其他问题。当前,我遇到一个问题,当我尝试运行简单的测试时,我会看到:(在输出中查找更多上下文)
TypeError:d不是函数
但是,它的描述性远不如以前,我也不确定它指向的是什么,因为它似乎与我更新此模块时已修复的this issue有关。
当然,当我实际运行我的应用程序时,即使我在以前的文章中对Jest之类的研究越来越多,我也很难找到与此相关的任何东西。
输出
$ npm run test
> app@1.0.0 test C:\Users\daniel\Desktop\app\app
> jest --debug --detectOpenHandles --forceExit --verbose --colors
{
"configs": [
{
"automock": false,
"browser": false,
"cache": true,
"cacheDirectory": "C:\\Users\\daniel\\AppData\\Local\\Temp\\jest",
"clearMocks": false,
"coveragePathIgnorePatterns": [
"\\\\node_modules\\\\"
],
"detectLeaks": false,
"detectOpenHandles": true,
"errorOnDeprecated": false,
"filter": null,
"forceCoverageMatch": [],
"globals": {
"ts-jest": {
"tsConfig": "./src/app/tsconfig.json"
},
"NODE_ENV": "test"
},
"haste": {
"providesModuleNodeModules": []
},
"moduleDirectories": [
"node_modules",
"src"
],
"moduleFileExtensions": [
"js",
"ts",
"json",
"node",
"html"
],
"moduleNameMapper": {},
"modulePathIgnorePatterns": [],
"modulePaths": [
"C:\\Users\\daniel\\Desktop\\app\\app\\src",
"C:\\Users\\daniel\\Desktop\\app\\app\\node_modules"
],
"name": "49c9f95751e84975b9ee85f166b8dbdd",
"prettierPath": "C:\\Users\\daniel\\Desktop\\app\\app\\node_modules\\prettier\\index.js",
"resetMocks": false,
"resetModules": false,
"resolver": null,
"restoreMocks": false,
"rootDir": "C:\\Users\\daniel\\Desktop\\app\\app",
"roots": [
"C:\\Users\\daniel\\Desktop\\app\\app"
],
"runner": "jest-runner",
"setupFiles": [],
"setupTestFrameworkScriptFile": null,
"skipFilter": false,
"snapshotSerializers": [],
"testEnvironment": "C:\\Users\\daniel\\Desktop\\app\\app\\node_modules\\jest-environment-node\\build\\index.js",
"testEnvironmentOptions": {},
"testLocationInResults": false,
"testMatch": [
"**/__tests__/**/*.js?(x)",
"**/?(*.)+(spec|test).js?(x)",
"**/__tests__/**/*.ts?(x)",
"**/?(*.)+(spec|test).ts?(x)"
],
"testPathIgnorePatterns": [
"\\\\build\\\\",
"\\\\config\\\\",
"\\\\data\\\\",
"\\\\dist\\\\",
"\\\\node_modules\\\\",
"\\\\test\\\\",
"\\\\vendor\\\\"
],
"testRegex": "",
"testRunner": "C:\\Users\\daniel\\Desktop\\app\\app\\node_modules\\jest-jasmine2\\build\\index.js",
"testURL": "http://localhost",
"timers": "real",
"transform": [
[
"^.+\\.html$",
"C:\\Users\\daniel\\Desktop\\app\\app\\config\\htmlLoader.js"
],
[
"^.+\\.[tj]sx?$",
"C:\\Users\\daniel\\Desktop\\app\\app\\node_modules\\ts-jest\\dist\\index.js"
]
],
"transformIgnorePatterns": [
"node_modules\\\\(?!(bootstrap-vue)\\\\)"
],
"watchPathIgnorePatterns": []
}
],
"globalConfig": {
"bail": false,
"changedFilesWithAncestor": false,
"collectCoverage": false,
"collectCoverageFrom": null,
"coverageDirectory": "C:\\Users\\daniel\\Desktop\\app\\app\\coverage",
"coverageReporters": [
"json",
"text",
"lcov",
"clover"
],
"coverageThreshold": null,
"detectLeaks": false,
"detectOpenHandles": true,
"errorOnDeprecated": false,
"expand": false,
"filter": null,
"forceExit": true,
"globalSetup": null,
"globalTeardown": null,
"listTests": false,
"maxWorkers": 3,
"noStackTrace": false,
"nonFlagArgs": [],
"notify": false,
"notifyMode": "always",
"passWithNoTests": false,
"projects": null,
"rootDir": "C:\\Users\\daniel\\Desktop\\app\\app",
"runTestsByPath": false,
"skipFilter": false,
"testFailureExitCode": 1,
"testPathPattern": "",
"testResultsProcessor": null,
"updateSnapshot": "new",
"useStderr": false,
"verbose": true,
"watch": false,
"watchman": true
},
"version": "23.6.0"
}
FAIL src/app/tests/app-window.component.spec.ts
● Test suite failed to run
TypeError: d is not a function
3 | import { Prop } from 'vue-property-decorator'
4 | import {
> 5 | ipcMainToRendererAppChannel,
| ^
6 | IpcMainToRendererAppMessage
7 | } from '../../../contracts/appAppMessages'
8 | import {
at Object.<anonymous>.__decorate (src/app/features/webview/webview.component.ts:5:95)
at Object.<anonymous> (src/app/features/webview/webview.component.ts:23:30)
at Object.<anonymous> (src/app/features/app-application-content/app-application-content.component.ts:24:1)
at Object.<anonymous> (src/app/features/app-content/app-content.component.ts:9:1)
console.log src/shared/Log.ts:4
not able to resolve rtc url ReferenceError: fetch is not defined
at Object.getJson (C:\Users\daniel\Desktop\app\app\src\shared\ProtocolRequests.ts:28:5)
at Function.getappConfig (C:\Users\daniel\Desktop\app\app\src\shared\Config.protocol.ts:27:35)
at Object.getConfig (C:\Users\daniel\Desktop\app\app\src\renderer\config.ts:7:57)
at discoverRTCUrl (C:\Users\daniel\Desktop\app\app\src\renderer\rtc.ts:7:35)
at Object.<anonymous> (C:\Users\daniel\Desktop\app\app\src\renderer\rtc.ts:10:49)
at Runtime._execModule (C:\Users\daniel\Desktop\app\app\node_modules\jest-runtime\build\index.js:694:13)
at Runtime.requireModule (C:\Users\daniel\Desktop\app\app\node_modules\jest-runtime\build\index.js:376:14)
at Runtime.requireModuleOrMock (C:\Users\daniel\Desktop\app\app\node_modules\jest-runtime\build\index.js:463:19)
at Object.<anonymous> (C:\Users\daniel\Desktop\app\app\src\app\features\app-application-content\app-application-content.component.ts:15:1)
at Runtime._execModule (C:\Users\daniel\Desktop\app\app\node_modules\jest-runtime\build\index.js:694:13)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.218s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app@1.0.0 test: `jest --debug --detectOpenHandles --forceExit --verbose --colors`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the app@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\daniel\AppData\Roaming\npm-cache\_logs\2018-11-23T15_31_12_028Z-debug.log
jest.config.js
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'node',
verbose: true,
moduleDirectories: ['node_modules', 'src'],
modulePaths: ['<rootDir>/src', '<rootDir>/node_modules'],
moduleFileExtensions: ['js', 'ts', 'json', 'node', 'html'],
transform: {
'^.+\\.html$': '<rootDir>/config/htmlLoader.js'
},
transformIgnorePatterns: ['node_modules/(?!(bootstrap-vue)/)'],
testPathIgnorePatterns: [
'/build/',
'/config/',
'/data/',
'/dist/',
'/node_modules/',
'/test/',
'/vendor/'
],
globals: {
'ts-jest': {
tsConfig: './src/app/tsconfig.json'
},
NODE_ENV: 'test'
}
}
tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"outDir": "./built/",
"sourceMap": true,
"strict": true,
"moduleResolution": "node",
"target": "ES2017",
"experimentalDecorators": true,
"noEmitHelpers": false,
"noImplicitAny": false,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"esModuleInterop": true,
"lib": ["es2017", "dom"],
"include": [
"**/*",
"../types/**/*",
"../../node_modules/bootstrap-vue/**/*",
"../../node_modules/electron/**/*"
]
}
}
测试
import { expect } from 'chai'
import 'jest'
import { appWindowComponent } from './app-window.component'
const appWindowComponent = new appWindowComponent()
describe('Test: Set Title Function', () => {
it('should set the variable title to the passed variable', () => {
const title = 'this is a test'
appWindowComponent.setTitle(title)
expect(appWindowComponent.title).to.equal(title)
})
})
编辑#1
做了更多的事情后,我将导入分隔开,看看玩笑是否给了我错误的指针,它是:
FAIL src/app/tests/app-window.component.spec.ts
● Test suite failed to run
TypeError: d is not a function
3 | import Component from 'vue-class-component'
4 |
> 5 | import { Prop } from 'vue-property-decorator'
| ^
6 |
7 | import { ipcMainToRendererAppChannel,
8 | IpcMainToRendererAppMessage
at Object.<anonymous>.__decorate (src/app/features/webview/webview.component.ts:5:95)
at Object.<anonymous> (src/app/features/webview/webview.component.ts:25:30)
at Object.<anonymous> (src/app/features/app-application-content/app-application-content.component.ts:24:1)
at Object.<anonymous> (src/app/features/app-content/app-content.component.ts:9:1)
编辑#2
使用Jest-Fetch-Mock摆脱了
console.log src/shared/Log.ts:4
not able to resolve rtc url ReferenceError: fetch is not defined
at Object.getJson (C:\Users\daniel\Desktop\app\app\src\shared\ProtocolRequests.ts:28:5)
at Function.getappConfig (C:\Users\daniel\Desktop\app\app\src\shared\Config.protocol.ts:27:35)
at Object.getConfig (C:\Users\daniel\Desktop\app\app\src\renderer\config.ts:7:57)
at discoverRTCUrl (C:\Users\daniel\Desktop\app\app\src\renderer\rtc.ts:7:35)
at Object.<anonymous> (C:\Users\daniel\Desktop\app\app\src\renderer\rtc.ts:10:49)
at Runtime._execModule (C:\Users\daniel\Desktop\app\app\node_modules\jest-runtime\build\index.js:694:13)
at Runtime.requireModule (C:\Users\daniel\Desktop\app\app\node_modules\jest-runtime\build\index.js:376:14)
at Runtime.requireModuleOrMock (C:\Users\daniel\Desktop\app\app\node_modules\jest-runtime\build\index.js:463:19)
at Object.<anonymous> (C:\Users\daniel\Desktop\app\app\src\app\features\app-application-content\app-application-content.component.ts:15:1)
at Runtime._execModule (C:\Users\daniel\Desktop\app\app\node_modules\jest-runtime\build\index.js:694:13)