Jest - 没有测试结果的输出

Jest - No output of test results

自从我尝试开始使用 Jest 以来,我就一直遇到困难。没有测试我尝试 运行 并且我尝试通过 Jest 的选项,我从来没有在控制台中得到 'Pass' / 'Fail' 输出结果。

Jest 总是只输出 'Done'

使用 'Nuxt CLI' 有一个默认测试写为:

import { mount } from '@vue/test-utils'
import Logo from '@/components/Logo.vue'

describe('Logo', () => {
  test('is a Vue instance', () => {
    const wrapper = mount(Logo)
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})

到目前为止我已经尝试过:

yarn test
yarn test --verbose
yarn test --watch
yarn test --watchAll
yarn test --no-watchmen

每一次,结果如下:

yarn run v1.21.1
$ jest
Done in 0.72s.

当前 jest.config.js:

module.exports = {
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/',
    '^~/(.*)$': '<rootDir>/',
    '^vue$': 'vue/dist/vue.common.js'
  },
  moduleFileExtensions: ['js', 'vue', 'json'],
  transform: {
    '^.+\.js$': 'babel-jest',
    '.*\.(vue)$': 'vue-jest'
  },
  collectCoverage: true,
  collectCoverageFrom: [
    '<rootDir>/components/**/*.vue',
    '<rootDir>/pages/**/*.vue'
  ]
}

这似乎是 Nuxt 的默认配置。

如有任何帮助,我们将不胜感激

Jest 查找以 .spec.js.test.js 文件格式结尾的文件。尝试将测试放在以 .spec.js.test.js 文件结尾的文件中。您也可以使用 jest.config.js 文件配置 jest

使用 jest.config.js 文件的一个例子是

const path = require('path')

module.exports = {
    rootDir: path.resolve(__dirname),
    moduleFileExtensions: [
        'js',
        'json',
        'vue',
        'ts'
    ],
    moduleNameMapper: {
        '^@/(.*)$': '<rootDir>/src/'
    },
    transform: {
        ".*\.(vue)$": "<rootDir>/node_modules/vue-jest",
        "^.+\.(js|jsx)?$": "<rootDir>/node_modules/babel-jest",
        "^.+\.ts$": "<rootDir>/node_modules/ts-jest"
    },
    testRegex: "(/__tests__/.*|(\.|/)(test|spec))\.(jsx?|tsx?)$",
    snapshotSerializers: [
        "jest-serializer-vue"
    ],
    testEnvironment: "jsdom",
    setupFiles: [
        "<rootDir>/globals.js"
    ]
}

所以我想通了(有点)。

我的测试 运行 符合以下条件:

yarn test --no-watchman

我不明白为什么 watchman 给我带来这么多问题,但这似乎有帮助。

更多信息: https://github.com/facebook/jest/issues/2219