我正尝试使用ts-Jest / Jest和Chai将测试添加到用TypeScript编写的Electron + Vue2应用程序中。但是,当我尝试运行为确保Jest正常运行而编写的简单测试时,我现在遇到了Jest的问题,似乎不喜欢我要导入要测试的组件模板的事实可以在错误中看到。当我尝试搜索与此相关的内容时,我的运气并不好,很多都指向“意外的令牌导入”,这是我已经解决的问题。
如果有人对我如何获得测试的理解或知识有什么见识或知识,那将是令人惊讶的,因为我很沮丧,而且文档也没有指出任何有用的东西。
错误:
$ npm run test
> app@1.0.0 test C:\Users\daniel\Desktop\app\app
> jest --detectOpenHandles
FAIL src/app/features/app-window/app-window.component.spec.ts
● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• 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/en/configuration.html
Details:
C:\Users\daniel\Desktop\app\app\src\app\features\app-application-content\app-application-content.component.html:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){<webview-component v-if="src"
^
SyntaxError: Unexpected token <
2 | import Component from 'vue-class-component'
3 | import { Prop, Watch } from 'vue-property-decorator'
> 4 | import Template from './app-application-content.component.html'
| ^
5 |
6 | import * as qs from 'qs'
7 | import { INameValue } from '../../../contracts/Core'
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (src/app/features/app-application-content/app-application-content.component.ts:4:1)
at Object.<anonymous> (src/app/features/app-content/app-content.component.ts:9:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 12.924s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app@1.0.0 test: `jest --detectOpenHandles`
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-21T14_25_23_427Z-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'],
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,
"noImplicitAny": false,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"esModuleInterop": true,
"lib": ["es2017", "dom"],
"include": [
"**/*",
"../types/**/*",
"../../node_modules/bootstrap-vue/**/*",
"../../node_modules/electron/**/*"
]
}
}
app-window.component.spec.ts
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)
})
})
答案 0 :(得分:1)
新的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'
}
}
注意
transform: {
'^.+\\.html$': '<rootDir>/config/htmlLoader.js'
},
moduleFileExtensions: ['js', 'ts', 'json', 'node', 'html'],
HTML加载程序文件
const htmlLoader = require('html-loader')
module.exports = {
process(src, filename, config, options) {
return htmlLoader(src)
}
}
当我现在收到一个新错误时,这似乎已经起作用了:)