小编Nan*_*nna的帖子

Jest 在测试 Vue 单文件组件时遇到意外标记

我有一个带有单文件组件的 vue 应用程序,我想添加单元测试来测试组件。我尝试使用此处描述的笑话,但我不断收到错误“笑话遇到意外令牌”,并包含以下详细信息:

/some_path/MyRecipe.vue:1
<template>
^

SyntaxError: Unexpected token <

  1 | import { shallowMount } from "@vue/test-utils"
> 2 | import MyRecipe from "../src/components/MyRecipe.vue"
    | ^
  3 | 
  4 | describe('MyRecipe', () => {
  5 |   test('is a Vue instance', () => {

  at Runtime._execModule (node_modules/jest-runtime/build/index.js:1166:56)
  at Object.<anonymous> (__tests__/MyRecipe.test.js:2:1)
Run Code Online (Sandbox Code Playgroud)

经过一些研究(例如,来自此处),我认为这可能是因为需要 .js 文件,但 .vue 单文件组件中包含 html、javascript 和 css,通常由 webpack 和 vue-loader 处理。我尝试遵循各种教程中的 jest 配置,使 jest 使用 vue-jest 来转换 .vue 文件,但错误仍然存​​在。这是我的 package.json 文件(删除了不必要的部分):

{
  "name": "all-recipes ", …
Run Code Online (Sandbox Code Playgroud)

javascript vue.js jestjs

7
推荐指数
1
解决办法
1万
查看次数

如何使用 pytest 模拟 os.environ?

我需要在使用 pytest 框架编写的单元测试中模拟 os.environ 。

这是我要测试的代码的虚拟版本,位于getters.py:

import os

username = os.environ['MY_USER']
password = os.environ['MY_PASS']

def get_data(...):

    access_token = request_access_token(username, password)
    ...
    return data
Run Code Online (Sandbox Code Playgroud)

这是单元测试的示例test_getters.py:

import pytest
import src.getters

class TestGetData(object):


    def test_something(self):

        data = src.getters.get_data(...)

        assert ...
Run Code Online (Sandbox Code Playgroud)

测试收集失败并出现以下错误:

=========================== short test summary info ============================
ERROR tests/test_getters.py - KeyError: 'MY_USER'
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.68s ===============================
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我希望能够在整个测试类中模拟一次。

我可以使用某种装饰器吗?或者其他一些推荐的嘲笑 os.environ 的方法?

python unit-testing mocking pytest

3
推荐指数
1
解决办法
9034
查看次数

标签 统计

javascript ×1

jestjs ×1

mocking ×1

pytest ×1

python ×1

unit-testing ×1

vue.js ×1